Incompatible types in assignment for Enum #9941
-
Ensure stubs packages are not installed
Verify if the api is typed
Describe the typing issueUsing a custom enum, mypy raises an error to annotate a column To Reproduceimport enum
from sqlalchemy import Column, Enum
from sqlalchemy.orm import declarative_base
ModelBase = declarative_base()
# also tried with:
# from sqlalchemy.orm import DeclarativeBase
# class ModelBase(DeclarativeBase):
# ...
# Same result.
class BanReason(enum.Enum):
AUTHENTICATION_ATTEMPTS = "authentication-attempts"
class IpAddress(ModelBase):
ban_reason = Column(Enum(BanReason), nullable=True)
ip = IpAddress()
ip.ban_reason = BanReason.AUTHENTICATION_ATTEMPTS
reveal_type(ip.ban_reason) Error$ mypy repro.py
repro.py:22: error: Need type annotation for "ban_reason" [var-annotated]
repro.py:25: error: Incompatible types in assignment (expression has type "BanReason", variable has type "Column[Any]") [assignment]
repro.py:27: note: Revealed type is "sqlalchemy.sql.schema.Column[Any]"
Found 2 errors in 1 file (checked 1 source file) Versions
Additional contextMore full-featured stack and more type-related error messages can be found on pypi/warehouse#13929 (specifically https://github.com/pypi/warehouse/actions/runs/5246828369/jobs/9476223258?pr=13929 ) I've tried adding annotations to the enum: class BanReason(str, enum.Enum):
AUTHENTICATION_ATTEMPTS = "authentication-attempts" Same result. Adding annotation directly to the Column: class IpAddress(ModelBase):
ban_reason: str = Column(Enum(BanReason), nullable=True) (
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
Hi, All those are expected. Mypy is supported using the new style declarative. I'm not 100% sure what's accepted by the mypy plugin, but that's deprecated. Please see the documentation regarding the migration: https://docs.sqlalchemy.org/en/20/changelog/whatsnew_20.html#orm-declarative-models |
Beta Was this translation helpful? Give feedback.
Hi,
All those are expected. Mypy is supported using the new style declarative. I'm not 100% sure what's accepted by the mypy plugin, but that's deprecated.
Please see the documentation regarding the migration: https://docs.sqlalchemy.org/en/20/changelog/whatsnew_20.html#orm-declarative-models