Closed
Description
Ensure stubs packages are not installed
- No sqlalchemy stub packages is installed (both
sqlalchemy-stubs
andsqlalchemy2-stubs
are 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
Confirm this is not Mypy dataclasses issue 13856
- This is not related to Mypy issue 13856
Describe the typing issue
It appears icontains
was accidentally ommitted from the sqlalchemy.sql.elements.SQLCoreOperations
type hints, as it has type ColumnOperators
and not the expected ColumnElement[bool]
type.
This means that type checkers won't let you use column.contains()
in, say, a WHERE
clause.
To Reproduce
# pyright: strict
from sqlalchemy import orm, sql
class Base(orm.DeclarativeBase):
pass
class User(Base):
__tablename__ = "users"
name: orm.Mapped[str]
contains = User.name.contains("foo") # ColumnElement[bool]
sql.select(User).where(contains) # works
icontains = User.name.icontains("foo") # ColumOperators
sql.select(User).where(icontains) # error
Error
Argument of type "ColumnOperators" cannot be assigned to parameter "whereclause" of type "_ColumnExpressionArgument[bool]" in function "where"
Type "ColumnOperators" cannot be assigned to type "_ColumnExpressionArgument[bool]"
"ColumnOperators" is incompatible with "ColumnElement[bool]"
"ColumnOperators" is incompatible with protocol "_HasClauseElement"
"__clause_element__" is not present
"ColumnOperators" is incompatible with "SQLCoreOperations[bool]"
"ColumnOperators" is incompatible with "ExpressionElementRole[bool]"
Type "ColumnOperators" cannot be assigned to type "() -> ColumnElement[bool]"
"ColumnOperators" is incompatible with "LambdaElement"
Versions
- OS: MacOS 12.6.3
- Python: 3.11.2
- SQLAlchemy: 2.0.9
- Type checker (eg: mypy 0.991, pyright 1.1.290, etc): pyright 1.1.303
Additional context
No response
Activity
zzzeek commentedon Apr 14, 2023
thanks for reporting. as always, with small typing glithces like this we can accept PRs to speed things along.
mjpieters commentedon Apr 14, 2023
Looking into a PR I for names missing that
sqlalchemy.sql.operators.ColumnOperators
implements, finding the follownig missing:I ignored
__hash__
given the comment on it. I'll see about defining all of these.mjpieters commentedon Apr 14, 2023
Define type hints for remaining column operators
Define type hints for remaining column operators
Define type hints for remaining column operators
sqla-tester commentedon Apr 17, 2023
Martijn Pieters has proposed a fix for this issue in the main branch:
Define type hints for remaining column operators https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/4573
3 remaining items