Skip to content

Type checkers do not allow using isnot in an AND clause #9148

Closed
@emontnemery

Description

@emontnemery

Describe the typing issue

Type checkers do not allow using isnot in an AND clause

To Reproduce

from sqlalchemy import Integer
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
from sqlalchemy.sql.elements import ColumnElement
from sqlalchemy.sql.expression import and_

class Base(DeclarativeBase):
    ...

class MyTable(Base):
    state_id: Mapped[int | None] = mapped_column(Integer)

def test() -> ColumnElement[bool]:
    return and_(MyTable.state_id.isnot(None))

Error

Mypy:

"ColumnOperators"; expected "Union[Literal[True], Union[ColumnElement[bool], _HasClauseElement, SQLCoreOperations[bool], ExpressionElementRole[bool], Callable[[], ColumnElement[bool]], LambdaElement]]"  [arg-type])

Pyright:

error: Argument of type "ColumnOperators" cannot be assigned to parameter "clauses" of type "_ColumnExpressionArgument[bool]" in function "and_"
    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" (reportGeneralTypeIssues)

Versions

  • OS:
  • Python:
  • SQLAlchemy: 2.0.0rc3
  • Type checkers:
    • mypy 0.991
    • pyright 1.1.290

Additional context

No response

Activity

added
requires triageNew issue that requires categorization
typingpep -484 typing issues. independent of "mypy"
on Jan 26, 2023
CaselIT

CaselIT commented on Jan 26, 2023

@CaselIT
Member

Hi,

have you tried using is_not?
isnot is "soft" deprecated since 1.4

added
awaiting infowaiting for the submitter to give more information
and removed
requires triageNew issue that requires categorization
on Jan 26, 2023
emontnemery

emontnemery commented on Jan 26, 2023

@emontnemery
Author

That does indeed work with both mypy and pyright 👍
Thanks a lot @CaselIT !

CaselIT

CaselIT commented on Jan 26, 2023

@CaselIT
Member

I'm not sure if this behaviour is intended or not. Let's hear mike opition @zzzeek

removed
awaiting infowaiting for the submitter to give more information
on Jan 26, 2023
zzzeek

zzzeek commented on Jan 26, 2023

@zzzeek
Member

if isnot is there it should be typed also. I guess it's there like an assignment right now and needs to be a full method ? Not at my computer to look right now

zzzeek

zzzeek commented on Jan 26, 2023

@zzzeek
Member

yeah none of it is typed, doing it now

added this to the 2.0 final milestone on Jan 26, 2023
sqla-tester

sqla-tester commented on Jan 26, 2023

@sqla-tester
Collaborator

Mike Bayer has proposed a fix for this issue in the main branch:

add typing to legacy operators https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/4392

CaselIT

CaselIT commented on Jan 26, 2023

@CaselIT
Member

if isnot is there it should be typed also. I guess it's there like an assignment right now and needs to be a full method ? Not at my computer to look right now

The types are in a TYPE_CHECKING block so we can do whatever there

1 remaining item

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

      No branches or pull requests

        Participants

        @zzzeek@sqla-tester@emontnemery@CaselIT

        Issue actions

          Type checkers do not allow using `isnot` in an `AND` clause · Issue #9148 · sqlalchemy/sqlalchemy