Skip to content

Incorrect type for column.icontains, should be ColumnElement[bool], is ColumnOperators #9650

Closed
@mjpieters

Description

@mjpieters
Contributor

Ensure stubs packages are not installed

  • No sqlalchemy stub packages is installed (both sqlalchemy-stubs and sqlalchemy2-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

added
requires triageNew issue that requires categorization
typingpep -484 typing issues. independent of "mypy"
on Apr 14, 2023
added
PRs (with tests!) welcomea fix or feature which is appropriate to be implemented by volunteers
and removed
requires triageNew issue that requires categorization
on Apr 14, 2023
added this to the 2.0.x milestone on Apr 14, 2023
zzzeek

zzzeek commented on Apr 14, 2023

@zzzeek
Member

thanks for reporting. as always, with small typing glithces like this we can accept PRs to speed things along.

mjpieters

mjpieters commented on Apr 14, 2023

@mjpieters
ContributorAuthor

Looking into a PR I for names missing that sqlalchemy.sql.operators.ColumnOperators implements, finding the follownig missing:

__lshift__
__rshift__
bitwise_and
bitwise_lshift
bitwise_not
bitwise_or
bitwise_rshift
bitwise_xor
iendswith
isnot_distinct_from
istartswith

I ignored __hash__ given the comment on it. I'll see about defining all of these.

mjpieters

mjpieters commented on Apr 14, 2023

@mjpieters
Author
added 3 commits that reference this issue on Apr 14, 2023
182f9c0
3aff03f
005c568
sqla-tester

sqla-tester commented on Apr 17, 2023

@sqla-tester
Collaborator

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

added
bugSomething isn't working
and removed
PRs (with tests!) welcomea fix or feature which is appropriate to be implemented by volunteers
on Apr 18, 2023

3 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingtypingpep -484 typing issues. independent of "mypy"

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      Participants

      @mjpieters@zzzeek@sqla-tester

      Issue actions

        Incorrect type for column.icontains, should be ColumnElement[bool], is ColumnOperators · Issue #9650 · sqlalchemy/sqlalchemy