Repository navigation
⬆️ Bump ty to 1.0.0 and resolve typing issues - #2153
YuriiMotov wants to merge 8 commits into
Conversation
📝 Docs previewLast commit ea42e4a at: https://2a4aa010.sqlmodel.pages.dev |
| sync_session_class: type[Session] = Session # ty: ignore[incompatible-mutable-override] | ||
| sync_session: Session # ty: ignore[incompatible-mutable-override] |
There was a problem hiding this comment.
We attempt to narrow the attribute type in child class from SQLAlchemy.Session to SQLModel.Session.
ty correctly argues that it's still possible to assign SQLAlchemy.Session to this attribute via SQLAlchemy interface. So, this narrower type can't be enforced.
I think in this case it's fine to just ignore it and assume nobody will do it.
Original typing errors
error[incompatible-mutable-override]: Invalid override of attribute `sync_session_class`
--> sqlmodel/ext/asyncio/session.py:32:5
|
32 | sync_session_class: type[Session] = Session
| ^^^^^^^^^^^^^^^^^^ Override does not accept writes of type `type[Session]`
error[incompatible-mutable-override]: Invalid override of attribute `sync_session`
--> sqlmodel/ext/asyncio/session.py:33:5
|
33 | sync_session: Session
| ^^^^^^^^^^^^ Override does not accept writes of type `Session`
I's not clear from the first sight here, but type[Session] in type annotation is SQLModel's Session, but in the error message it's SQLAlchemy's Session.
| def __setattr__(cls, name: str, value: Any) -> None: # ty: ignore[lost-keyword-for-regular-parameter] | ||
| if is_table_model_class(cls): | ||
| DeclarativeMeta.__setattr__(cls, name, value) | ||
| else: | ||
| super().__setattr__(name, value) | ||
|
|
||
| def __delattr__(cls, name: str) -> None: # ty: ignore[invalid-method-override] | ||
| def __delattr__(cls, name: str) -> None: # ty: ignore[lost-keyword-for-regular-parameter] |
There was a problem hiding this comment.
Base class declares the parameter name as key, but here we use name.
We previously decided to keep the name and just ignore this error: #1806 (comment). So I kept this approach (just fixed the rule name).
But actually we can rename it to key and resolve the issue (I've checked CI passes). Compatibility with Object is not an issue because Object.__setattr__ declares parameters as position-only. So, there will be no name collision here:
@disjoint_base
class object:
...
def __setattr__(self, name: str, value: Any, /) -> None: ...
def __delattr__(self, name: str, /) -> None: ...Original errors
error[lost-keyword-for-regular-parameter]: Override of method `__setattr__` disallows keyword arguments allowed by the method on the base class
--> sqlmodel/main.py:565:9
|
565 | def __setattr__(cls, name: str, value: Any) -> None: # ty: ignore[invalid-method-override]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Definition is incompatible with `_DynamicAttributesType.__setattr__`
|
::: .venv/lib/python3.11/site-packages/sqlalchemy/orm/decl_api.py:138:9
|
138 | def __setattr__(cls, key: str, value: Any) -> None:
| ---------------------------------------------- `_DynamicAttributesType.__setattr__` defined here
info: the parameter named `name` does not match `key` (and can be used as a keyword parameter)
info: This violates the Liskov Substitution Principle
warning[unused-ignore-comment]: Unused `ty: ignore` directive
--> sqlmodel/main.py:565:59
|
565 | def __setattr__(cls, name: str, value: Any) -> None: # ty: ignore[invalid-method-override]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: Remove the unused suppression comment
|
564 | # Replicate SQLAlchemy
- def __setattr__(cls, name: str, value: Any) -> None: # ty: ignore[invalid-method-override]
565 + def __setattr__(cls, name: str, value: Any) -> None:
566 | if is_table_model_class(cls):
|
warning[renamed-rule-in-ignore-comment]: Rule `invalid-method-override` has been renamed to `incompatible-method-override`
--> sqlmodel/main.py:565:72
|
565 | def __setattr__(cls, name: str, value: Any) -> None: # ty: ignore[invalid-method-override]
| ^^^^^^^^^^^^^^^^^^^^^^^
help: Replace `invalid-method-override` with `incompatible-method-override`
|
564 | # Replicate SQLAlchemy
- def __setattr__(cls, name: str, value: Any) -> None: # ty: ignore[invalid-method-override]
565 + def __setattr__(cls, name: str, value: Any) -> None: # ty: ignore[incompatible-method-override]
566 | if is_table_model_class(cls):
|
error[lost-keyword-for-regular-parameter]: Override of method `__delattr__` disallows keyword arguments allowed by the method on the base class
--> sqlmodel/main.py:571:9
|
571 | def __delattr__(cls, name: str) -> None: # ty: ignore[invalid-method-override]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Definition is incompatible with `_DynamicAttributesType.__delattr__`
|
::: .venv/lib/python3.11/site-packages/sqlalchemy/orm/decl_api.py:144:9
|
144 | def __delattr__(cls, key: str) -> None:
| ---------------------------------- `_DynamicAttributesType.__delattr__` defined here
info: the parameter named `name` does not match `key` (and can be used as a keyword parameter)
info: This violates the Liskov Substitution Principle
warning[unused-ignore-comment]: Unused `ty: ignore` directive
--> sqlmodel/main.py:571:47
|
571 | def __delattr__(cls, name: str) -> None: # ty: ignore[invalid-method-override]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: Remove the unused suppression comment
|
570 |
- def __delattr__(cls, name: str) -> None: # ty: ignore[invalid-method-override]
571 + def __delattr__(cls, name: str) -> None:
572 | if is_table_model_class(cls):
|
warning[renamed-rule-in-ignore-comment]: Rule `invalid-method-override` has been renamed to `incompatible-method-override`
--> sqlmodel/main.py:571:60
|
571 | def __delattr__(cls, name: str) -> None: # ty: ignore[invalid-method-override]
| ^^^^^^^^^^^^^^^^^^^^^^^
help: Replace `invalid-method-override` with `incompatible-method-override`
|
570 |
- def __delattr__(cls, name: str) -> None: # ty: ignore[invalid-method-override]
571 + def __delattr__(cls, name: str) -> None: # ty: ignore[incompatible-method-override]
572 | if is_table_model_class(cls):
|
| **pydantic_annotations, | ||
| **new_cls.__annotations__, | ||
| } | ||
| new_cls_model_config = cast(SQLModelConfig, new_cls.model_config) |
There was a problem hiding this comment.
In base class model_config attribute has type Pydantic.ConfigDict. Even if we assign SQLModelConfig in child, the type remains Pydantic.ConfigDict (it's still possible to assign Pydantic.ConfigDict via base class interface).
So, when the code below attempts to access table and registry keys, ty correctly argues (these keys are not declared for Pydantic.ConfigDict).
To fix this we assume that nobody will re-assign it this way and just cast it here for type checking to pass.
| # This could be done by reading new_cls.model_config['table'] in FastAPI, but | ||
| # that's very specific about SQLModel, so let's have another config that | ||
| # other future tools based on Pydantic can use. | ||
| new_cls.model_config["read_from_attributes"] = True # ty: ignore[invalid-key] | ||
| new_cls_model_config["read_from_attributes"] = True # ty: ignore[invalid-typed-dict-key] | ||
| # For compatibility with older versions | ||
| # TODO: remove this in the future | ||
| new_cls.model_config["read_with_orm_mode"] = True # ty: ignore[invalid-key] | ||
| new_cls_model_config["read_with_orm_mode"] = True # ty: ignore[invalid-typed-dict-key] |
There was a problem hiding this comment.
I think we can remove these two assignments - they were needed for compatibility with Pydantic V1 and are not used since we dropped Pydantic V1 support.
| # handled well by SQLAlchemy without Mapped, so, wrap the | ||
| # annotations in Mapped here | ||
| cls.__annotations__[rel_name] = Mapped[ann] | ||
| cls.__annotations__[rel_name] = Mapped[ann] # ty: ignore[invalid-type-form] |
There was a problem hiding this comment.
ty correctly argues on this.
Before switching from mypy to ty we had # type: ignore[valid-type] ignore comment here (see PR). Now ty also started arguing on this, so we need to add this ignore comment back.
Original error
error[invalid-type-form]: Variable `ann` cannot be used as a type
--> sqlmodel/main.py:690:60
|
690 | cls.__annotations__[rel_name] = Mapped[ann]
| ^^^
info: `ann` is not a valid type alias
info: Invalid subscript of object of type `dict[str, Any]` in a type alias definition
--> sqlmodel/main.py:681:27
|
681 | raw_ann = cls.__annotations__[rel_name]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
info: See the following page for a reference on valid type expressions:
info: https://typing.python.org/en/latest/spec/annotations.html#type-and-annotation-expressions
| ipaddress.IPv6Network, | ||
| Path, | ||
| EmailStr, | ||
| cast(type, EmailStr), |
There was a problem hiding this comment.
With TYPE_CHECKING Pydantic declares EmailStr, AwareDatetime and NaiveDatetime as Annotated[str, ...] (ref).
So, they are not types and ty correctly argues that we can pass them to isinstance. But in runtime they are classes.
So, it's Ok to cast them here.
Original error
error[invalid-argument-type]: Argument to function `issubclass` is incorrect
--> sqlmodel/main.py:739:9
|
739 | / (
740 | | str,
741 | | ipaddress.IPv4Address,
742 | | ipaddress.IPv4Network,
743 | | ipaddress.IPv6Address,
744 | | ipaddress.IPv6Network,
745 | | Path,
746 | | EmailStr,
747 | | ),
| |_________^ Expected `_ClassInfo`, found `tuple[<class 'str'>, <class 'IPv4Address'>, <class 'IPv4Network'>, <class 'IPv6Address'>, <class 'IPv6Network'>, <class 'Path'>, <special-form 'typing.Annotated[str,<metadata>]'>]`
info: Function defined here
--> stdlib/builtins.pyi:4193:5
|
4193 | def issubclass(cls: type, class_or_tuple: _ClassInfo, /) -> bool:
| ^^^^^^^^^^ -------------------------- Parameter declared here
error[invalid-argument-type]: Argument to function `issubclass` is incorrect
--> sqlmodel/main.py:760:16
|
760 | type_, (datetime, AwareDatetime, NaiveDatetime)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Expected `_ClassInfo`, found `tuple[<class 'datetime'>, <special-form 'typing.Annotated[datetime, <metadata>]'>, <special-form 'typing.Annotated[datetime, <metadata>]'>]`
info: Function defined here
--> stdlib/builtins.pyi:4193:5
|
4193 | def issubclass(cls: type, class_or_tuple: _ClassInfo, /) -> bool:
| ^^^^^^^^^^ -------------------------- Parameter declared here
| if issubclass( | ||
| type_, (datetime, cast(type, AwareDatetime), cast(type, NaiveDatetime)) | ||
| ): |
|
|
||
| @classmethod | ||
| def model_validate( # ty: ignore[invalid-method-override] | ||
| def model_validate( # ty: ignore[incompatible-method-override] |
Description
Latest versions of
tyintroduced several changes that led to typing errors.This PR upgrades
tyto latest and resolves these typing issues.AI Disclaimer
Used Codex (gpt-astra 6) to investigate and apply fixes. Checked and corrected manually.
Checklist