dropbox / sqlalchemy-stubs

Mypy plugin and stubs for SQLAlchemy
Apache License 2.0
572 stars 102 forks source link

Assigning to a Union of a nullable and non-nullable column fails #240

Open Garrett-R opened 2 years ago

Garrett-R commented 2 years ago

I'm running the latest (at the time of writing) mypy (0.950) and sqlalchemy-stubs (0.4) and hitting this issue:

from sqlalchemy import Column, Integer
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class Dog(Base):
    __tablename__ = 'dogs'

    age = Column(Integer)

class Cat(Base):
    __tablename__ = 'cats'

    age = Column(Integer, nullable=False)

Animal = Dog | Cat

animal: Animal = Cat()

animal.age = 20  # Mypy error, should be fine!

The error I get is:

error: Incompatible types in assignment (expression has type "int", variable has type "Union[Column[Optional[int]], Column[int]]")

but I don't think there should be an error.

My mypy config in my pyproject.toml is just:

[tool.mypy]
plugins = "sqlmypy"