Skip to content

Hack on typing returns typing error on sqlalchemy where clause #19

Open
@8area8

Description

@8area8

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

https://github.com/fastapi-users/fastapi-users-db-sqlalchemy/blob/main/fastapi_users_db_sqlalchemy/__init__.py#L25

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]"

Image

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.

Image

Configuration

  • Python version : 3.13
  • FastAPI version : 0.115.8
  • FastAPI Users version : 14.0.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions