tortoise-orm icon indicating copy to clipboard operation
tortoise-orm copied to clipboard

PEP 557 support

Open Ae-Mc opened this issue 3 years ago • 6 comments

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

Ae-Mc avatar Jan 24 '22 11:01 Ae-Mc

What's the magic?

long2ice avatar Feb 06 '22 11:02 long2ice

As I can understand, SQLModel uses in development standard supported by pyright.

Ae-Mc avatar Feb 13 '22 20:02 Ae-Mc

That's good

long2ice avatar Feb 14 '22 02:02 long2ice

So, maybe, tortoise-orm can use it too?

Ae-Mc avatar Feb 14 '22 19:02 Ae-Mc

That's welcome if you could make a PR

long2ice avatar Feb 15 '22 00:02 long2ice

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)

maou-shonen avatar Mar 21 '22 17:03 maou-shonen