[Python] Typing sqlalchemy where clauses

Discussão em 'Python' iniciado por Stack, Maio 20, 2025.

  1. Stack

    Stack Membro Participativo

    Following this doc:

    https://docs.sqlalchemy.org/en/20/orm/extensions/mypy.html

    I tried to type-check my test.py file:

    from sqlalchemy import Column, Integer, String, select
    from sqlalchemy.orm import declarative_base
    from sqlalchemy.dialects import postgresql
    Base = declarative_base()
    class Usr(Base):
    __tablename__ = "usr"
    id = Column(Integer, primary_key=True)
    name = Column(String)
    stmt = select(
    Usr.name
    ).where(
    Usr.id == "test" # comparing an int and an str
    )


    using the following command:

    mypy --strict --config-file mypy.ini test.py


    where mypy.ini contains:

    [mypy]
    plugins = sqlalchemy.ext.mypy.plugin


    Mypy does not raise any error on the where clause (Usr.id == "blabla").

    Is there a way to have mypy complain with some Non-overlapping equality check or some workaround for that case ?

    Continue reading...

Compartilhe esta Página