Repository navigation
⬆️ Bump ty to 1.0.0 and resolve typing issues #2153
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
a7ec3ad
1b926ca
1cfd59d
12ed157
385a978
47d225e
7a44da6
ea42e4a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -562,13 +562,13 @@ class SQLModelMetaclass(ModelMetaclass, DeclarativeMeta): | |
| model_fields: ClassVar[dict[str, FieldInfo]] | ||
|
|
||
| # Replicate SQLAlchemy | ||
| def __setattr__(cls, name: str, value: Any) -> None: # ty: ignore[invalid-method-override] | ||
| 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] | ||
|
Comment on lines
+565
to
+571
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Base class declares the parameter name as But actually we can rename it to @disjoint_base
class object:
...
def __setattr__(self, name: str, value: Any, /) -> None: ...
def __delattr__(self, name: str, /) -> None: ...Original errors |
||
| if is_table_model_class(cls): | ||
| DeclarativeMeta.__delattr__(cls, name) | ||
| else: | ||
|
|
@@ -624,9 +624,10 @@ def __new__( | |
| **pydantic_annotations, | ||
| **new_cls.__annotations__, | ||
| } | ||
| new_cls_model_config = cast(SQLModelConfig, new_cls.model_config) | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In base class So, when the code below attempts to access To fix this we assume that nobody will re-assign it this way and just |
||
|
|
||
| def get_config(name: str) -> Any: | ||
| config_class_value = new_cls.model_config.get(name, Undefined) | ||
| config_class_value = new_cls_model_config.get(name, Undefined) | ||
| if config_class_value is not Undefined: | ||
| return config_class_value | ||
| kwarg_value = kwargs.get(name, Undefined) | ||
|
|
@@ -637,7 +638,7 @@ def get_config(name: str) -> Any: | |
| config_table = get_config("table") | ||
| if config_table is True: | ||
| # If it was passed by kwargs, ensure it's also set in config | ||
| new_cls.model_config["table"] = config_table | ||
| new_cls_model_config["table"] = config_table | ||
| for k, v in get_model_fields(new_cls).items(): | ||
| col = get_column_from_field(v) | ||
| setattr(new_cls, k, col) | ||
|
|
@@ -646,16 +647,16 @@ def get_config(name: str) -> Any: | |
| # 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] | ||
|
Comment on lines
647
to
+653
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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. |
||
|
|
||
| config_registry = get_config("registry") | ||
| if config_registry is not Undefined: | ||
| config_registry = cast(registry, config_registry) | ||
| # If it was passed by kwargs, ensure it's also set in config | ||
| new_cls.model_config["registry"] = config_table | ||
| new_cls_model_config["registry"] = config_table | ||
| setattr(new_cls, "_sa_registry", config_registry) # noqa: B010 | ||
| setattr(new_cls, "metadata", config_registry.metadata) # noqa: B010 | ||
| setattr(new_cls, "__abstract__", True) # noqa: B010 | ||
|
|
@@ -686,7 +687,7 @@ def __init__( | |
| # Plain forward references, for models not yet defined, are not | ||
| # 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] | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Original error |
||
| relationship_to = get_relationship_to( | ||
| name=rel_name, rel_info=rel_info, annotation=ann | ||
| ) | ||
|
|
@@ -742,7 +743,7 @@ def get_sqlalchemy_type(field: Any) -> Any: | |
| ipaddress.IPv6Address, | ||
| ipaddress.IPv6Network, | ||
| Path, | ||
| EmailStr, | ||
| cast(type, EmailStr), | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With So, they are not types and Original error |
||
| ), | ||
| ): | ||
| max_length = getattr(metadata, "max_length", None) | ||
|
|
@@ -755,7 +756,9 @@ def get_sqlalchemy_type(field: Any) -> Any: | |
| return Boolean | ||
| if issubclass(type_, int): | ||
| return Integer | ||
| if issubclass(type_, (datetime, AwareDatetime, NaiveDatetime)): | ||
| if issubclass( | ||
| type_, (datetime, cast(type, AwareDatetime), cast(type, NaiveDatetime)) | ||
| ): | ||
|
Comment on lines
+759
to
+761
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See comment above |
||
| if issubclass(type_, cast(type, NaiveDatetime)): | ||
| return DateTime(timezone=False) | ||
| return UTCDateTime() | ||
|
|
@@ -909,7 +912,7 @@ def __tablename__(cls) -> str: | |
| return cls.__name__.lower() | ||
|
|
||
| @classmethod | ||
| def model_validate( # ty: ignore[invalid-method-override] | ||
| def model_validate( # ty: ignore[incompatible-method-override] | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| cls: type[_TSQLModel], | ||
| obj: Any, | ||
| *, | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We attempt to narrow the attribute type in child class from
SQLAlchemy.SessiontoSQLModel.Session.tycorrectly argues that it's still possible to assignSQLAlchemy.Sessionto this attribute viaSQLAlchemyinterface. 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
I's not clear from the first sight here, but
type[Session]in type annotation is SQLModel'sSession, but in the error message it's SQLAlchemy'sSession.