sqlalchemy2-stubs icon indicating copy to clipboard operation
sqlalchemy2-stubs copied to clipboard

Re-using sqlalchemy.dialects.postgresql.UUID instance generates mypy violation

Open mtvx opened this issue 3 years ago • 1 comments

Describe the bug

Storing an instance of sqlalchemy.dialects.postgresql.UUID to a variable and re-using that in the column declarations is seen as NullType.

Expected behavior

Type should be detected as Column[UUID]?

To Reproduce

from typing import Optional
from uuid import UUID

from sqlalchemy import Column
from sqlalchemy.dialects.postgresql import UUID as PostgresUUID
from sqlalchemy.orm import declarative_base

PgUUID = PostgresUUID(as_uuid=True)
Base = declarative_base()


class Foobar(Base):
    __tablename__ = "foobar"

    works: Optional[UUID] = Column(PostgresUUID(as_uuid=True))
    doesnt: Optional[UUID] = Column(PgUUID)

Error

Running with sqlalchemy.ext.mypy.plugin enabled in mypy configs:

$ mypy foobar.py
foobar.py:15: error: Incompatible types in assignment (expression has type "Column[NullType]", variable has type "UUID")  [assignment]
Found 1 error in 1 file (checked 1 source file)

Versions.

  • OS: Linux
  • Python: 3.9.9
  • SQLAlchemy: 1.4.39
  • mypy: mypy 0.971
  • SQLAlchemy2-stubs: 0.0.2a25

mtvx avatar Aug 25 '22 13:08 mtvx

Also construction gives violation:

Foobar(
    works=uuid.uuid4(),
    doesnt=uuid.uuid4(),
)
$ mypy foobar.py
...
foobar.py:20: error: Unexpected keyword argument "doesnt" for "Foobar"  [call-arg]
...

mtvx avatar Aug 25 '22 13:08 mtvx