-
-
Notifications
You must be signed in to change notification settings - Fork 900
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
Type error when calling db.Model subclass constructor with parameters #1312
Comments
I'm facing the same issue. Additionally, As a workaround, adding Alternatively: class Base(DeclarativeBase, MappedAsDataclass):
pass
class ProperlyTypedSQLAlchemy(SQLAlchemy):
"""Temporary type hinting workaround for Flask SQLAlchemy.
This is a temporary workaround for the following issue:
https://github.com/pallets-eco/flask-sqlalchemy/issues/1312
This workaround may not be correct.
"""
Model: Type[Base]
db = SQLAlchemy(model_class=Base)
db = cast(ProperlyTypedSQLAlchemy, db) |
This comment was marked as outdated.
This comment was marked as outdated.
I spot another typehint issue just now. See #1318 Hopefully, these two issues can be solved together. To my understanding. class SQLAlchemy(Generic[_FSA_MCT]):
def __init__(..., model_class: _FSA_MCT, ...): ...
@property
def Model(self) -> _FSA_MCT: ... In current implementation, the flask-sqlalchemy/src/flask_sqlalchemy/extension.py Lines 164 to 175 in 42a36a3
If the typehints can be corrected, the dirty workaround |
Happy to review a PR |
I am trying to make a PR now. However, I am sorry that this issue is far more complicated than I thought.
|
I think I got an idea to fix it. But I am not sure whether this fixture will cause side effects. I am working on #1318 now. If it has been finalized, I will try to submit another PR for this. from sqlalchemy.util import typing as compat_typing
@compat_typing.dataclass_transform(
field_specifiers=(
sa_orm.MappedColumn,
sa_orm.RelationshipProperty,
sa_orm.Composite,
sa_orm.Synonym,
sa_orm.mapped_column,
sa_orm.relationship,
sa_orm.composite,
sa_orm.synonym,
sa_orm.deferred,
),
)
class _FSAModel(Model):
metadata: sa.MetaData After changing this, I found the But this change still needs some improvement. It should take effect only when provided |
1. Provide a descriptor `ModelGetter` for solving the `db.Model` type from the `db` type dynamically. 2. Add `t.Type[sa_orm.MappedAsDataclass]` to `_FSA_MCT`. 3. Let `SQLAlchemy(...)` annotated by the provided `model_class` type.
@davidism I am glad to tell you that I finally found a good solution (see #1321). Although my PR still has few remained issues. It has tackled the type errors in most cases. I also attach codes for testing is and attach another part for explaining my idea. Even if you reject this PR, I still hope that it can suggest a possible direction for solving this issue. At least, this PR works well for me. |
Pylance / Pyright reports a type error when instantiating a Model subclass with parameters:
Minimal example:
I expected there to be no type errors, since this operation is supported within SQLAlchemy.
Environment:
The text was updated successfully, but these errors were encountered: