tortoise-orm
tortoise-orm copied to clipboard
PEP 557 support
To improve editor's autocompletion for, for example, new rows creation:
User(na<press tab here for autocompletion>)
Above action should show name as one of autocompletion options, but now it isn't working.
For example of library with working autocompletion I can sugest SQLModel by tiangolo
What's the magic?
As I can understand, SQLModel uses in development standard supported by pyright.
That's good
So, maybe, tortoise-orm can use it too?
That's welcome if you could make a PR
This requires the use of PEP 681 This is still a draft.
You can simply make it yourself
from typing import Any, Callable, TypeVar, Tuple, Union
from datetime import datetime
from tortoise import fields
from tortoise.models import Model
_T = TypeVar('_T')
def __dataclass_transform__(
*,
eq_default: bool = True,
order_default: bool = False,
kw_only_default: bool = False,
field_descriptors: Tuple[Union[type, Callable[..., Any]], ...] = (()),
) -> Callable[[_T], _T]:
return lambda a: a
@__dataclass_transform__()
class SqlModel(Model):
pass
class Event(SqlModel):
id: int = fields.IntField(pk=True)
name: str = fields.TextField()
datetime: datetime = fields.DatetimeField(null=True)
Event(<autocompletion>)
Of course you can also use PEP 557 to attach to each model
from datetime import datetime
from tortoise import fields
from tortoise.models import Model
@dataclass
class Event(Model):
id: int = fields.IntField(pk=True)
name: str = fields.TextField()
datetime: datetime = fields.DatetimeField(null=True)