3 ms·
I'm curious how the type hints for the `__init__` method are derived from the properties in the class without a type checking plugin Nothing stuck out to me wh
by emptysea 5y ago
I'm curious how the type hints for the `__init__` method are derived from the properties in the class without a type checking plugin
Nothing stuck out to me when perusing through the code
Edit: seems pylance can figure it out but mypy can't, maybe pylance is special casing something?
from typing import Optional
from sqlmodel import Field, SQLModel
class Hero(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
name: str
secret_name: str
age: Optional[int] = None
reveal_type(Hero.__init__)
# pylance: Type of "Hero.__init__" is "(self: Hero, *, id: int | None = Field(default=None, primary_key=True), name: str, secret_name: str, age: int | None = None) -> None"
# mypy: main.py:18:13: note: Revealed type is "def (__pydantic_self__: sqlmodel.main.SQLModel, **data: Any)"
- jkrubin 5y agoHow does your test work with just pydantic basemodels?
- Xavdidtheshadow 5y ago> maybe pylance is special casing something? Anecdata, but when I was digging into how Dataclasses worked, Pylance had great completion, but only if I imported from the dataclass module itself. If I pasted the whole implementation into a local file and used that, I didn't get the hints. This makes me think that Pylance _does_ have some secret sauce.
- tiangolo 5y agoYep, but not secret, it's the in-progress dataclass_transform draft spec, already implemented in (open source) Pyright, and Pylance re-uses Pyright, so it gets that support. https://github.com/microsoft/pyright/blob/main/specs/dataclass_transforms.md https://github.com/microsoft/pyright/blob/main/specs/datacla...
- Xavdidtheshadow 5y agoThere you go! Not secret at all. Thanks for linking that!
- tiangolo 5y agoYep it uses the in-progress draft-spec dataclass_transforms https://github.com/microsoft/pyright/blob/main/specs/dataclass_transforms.md https://github.com/microsoft/pyright/blob/main/specs/datacla... As Pyright already supports it, and Pylance is built on Pyright, they can use it directly. Hopefully more editors will use it and hopefully it will be part of the standard Python (in a PEP, with typing.dataclass_transform, and with mypy support). And just in case you're wondering, there's no downside to having the dataclass_transform, anything that doesn't support it is unaffected, and nothing else would have completion either way.