I'm trying to use mypy with SQLAlchemy.
In order to validate/modify specific column value (email
in this case), SQLAlchemy official document provides hybrid_property
decorator.
The problem is, mypy
doesn't recognize EmailAddress
class constructor properly, it gives:
email_address.py:31: error: Unexpected keyword argument "email" for "EmailAddress"; did you mean "_email"?
How can I tell mypy to recognize these columns?
from typing import TYPE_CHECKING
from sqlalchemy import Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
# I don't even like the following statements just for setter
if TYPE_CHECKING:
hybrid_property = property
else:
from sqlalchemy.ext.hybrid import hybrid_property
Base = declarative_base()
class EmailAddress(Base):
__tablename__ = "email_address"
id = Column(Integer, primary_key=True)
_email = Column("email", String)
@hybrid_property
def email(self):
return self._email
@email.setter
def email(self, email):
self._email = email
EmailAddress(email="foobar@example.com")
# email_address.py:31: error: Unexpected keyword argument "email" for "EmailAddress"; did you mean "_email"?
I'm using following packages:
SQLAlchemy==1.4.35
mypy==0.942
mypy-extensions==0.4.3
sqlalchemy2-stubs==0.0.2a22