`Query.statement` infers `Select[Unknown]` for a bare-`Column` query
Author: cmp0xffCreated Sep 26, 2026Updated Sep 26, 2026
Labelsbugtyping
Ensure stubs packages are not installed
- No sqlalchemy stub packages is installed (both
sqlalchemy-stubsandsqlalchemy2-stubsare not compatible with v2)
Verify if the api is typed
- The api is not in a module listed in #6810 so it should pass type checking
Describe the typing issue
In SQLAlchemy 2.1.x, Query.statement is annotated Select[_T] | FromStatement[_T] | UpdateBase, and for a session.query(<Column>) the _T type variable never resolves, so pyright strict mode reports the member as partially unknown (reportUnknownMemberType).
To Reproduce
import sqlalchemy
import sqlalchemy.orm
class Base(sqlalchemy.orm.DeclarativeBase):
pass
class Temp(Base):
__tablename__ = "part"
quantity = sqlalchemy.Column(sqlalchemy.Integer)
engine = sqlalchemy.create_engine("sqlite://")
Session = sqlalchemy.orm.sessionmaker(engine)
with Session() as session:
q = session.query(Temp.quantity)
stmt = q.statement
reveal_type(stmt)Error
/tmp/repro_statement.py:17:5 - error: Type of "q" is partially unknown
Type of "q" is "Query[Unknown]" (reportUnknownVariableType)
/tmp/repro_statement.py:18:5 - error: Type of "stmt" is partially unknown
Type of "stmt" is "Select[Unknown] | FromStatement[Unknown] | UpdateBase" (reportUnknownVariableType)
/tmp/repro_statement.py:18:12 - error: Type of "statement" is partially unknown
Type of "statement" is "Select[Unknown] | FromStatement[Unknown] | UpdateBase" (reportUnknownMemberType)Versions
- OS: mac
- Python: 3.14
- SQLAlchemy: 2.1.1
- Type checker (eg: mypy 0.991, pyright 1.1.290, etc): pyright 1.1.411 (strict mode)
Additional context
deepseek-v4-pro helped me identify the issue which arose in pandas-dev/pandas-stubs#1953
Source: sqlalchemy/sqlalchemy