Skip to content

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

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

Open
8area8 opened this issue Mar 18, 2025 · 0 comments
Open

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

8area8 opened this issue Mar 18, 2025 · 0 comments
Labels
bug Something isn't working

Comments

@8area8
Copy link

8area8 commented Mar 18, 2025

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
@8area8 8area8 added the bug Something isn't working label Mar 18, 2025
@8area8 8area8 changed the title Hack on typing returns error on sqlalchemy where clause Hack on typing returns typing error on sqlalchemy where clause Mar 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant