Open
Description
Describe the bug
Currently the code hacks the handling of User
attributes with a condition on TYPE_CHECKING
. This hack should no longer exist because mypy now handles sqlalchemy field types correctly.
The hack in the source code
class SQLAlchemyBaseUserTable(Generic[ID]):
"""Base SQLAlchemy users table definition."""
__tablename__ = "user"
if TYPE_CHECKING: # pragma: no cover
id: ID
email: str
hashed_password: str
is_active: bool
is_superuser: bool
is_verified: bool
else:
email: Mapped[str] = mapped_column(
String(length=320), unique=True, index=True, nullable=False
)
hashed_password: Mapped[str] = mapped_column(
String(length=1024), nullable=False
)
is_active: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False)
is_superuser: Mapped[bool] = mapped_column(
Boolean, default=False, nullable=False
)
is_verified: Mapped[bool] = mapped_column(
Boolean, default=False, nullable=False
)
The typing error in the use of the User model.
Cannot assign argument of type "bool" to parameter "whereclause" of type "_ColumnExpressionArgument[bool]" in function "where"
Type "bool" is not assignable to type "_ColumnExpressionArgument[bool]"
"bool" is not assignable to "ColumnElement[bool]"
"bool" is incompatible with protocol "_HasClauseElement[bool]"
"__clause_element__" is not present
"bool" is not assignable to "SQLCoreOperations[bool]"
"bool" is not assignable to "ExpressionElementRole[bool]"
"bool" is not assignable to "TypedColumnsClauseRole[bool]"
Type "bool" is not assignable to type "() -> ColumnElement[bool]"
To Reproduce
- Opens a code editor that supports Python type checking.
- Write a where clause with the User model.
Expected behavior
Normally Pylance or mypy should not be in error in where
clauses.
Configuration
- Python version : 3.13
- FastAPI version : 0.115.8
- FastAPI Users version : 14.0.1