-
Notifications
You must be signed in to change notification settings - Fork 100
Enum interpreted as str #114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This is because SQLAlchemy enum predates PEP 435, so it is actually possible to pass a list of strings to the We can try to support this by making |
I see, thanks for the information. |
Is there a work around here to allow the type checker to treat this as an enum? Currently I'm just using |
Something like this should work I think: if TYPE_CHECKING:
from sqlalchemy.sql.type_api import TypeEngine
class Enum(TypeEngine[T]):
def __init__(self, enum: Type[T]) -> None: ...
else:
from sqlalchemy import Enum |
@ilevkivskyi Thank you! That seems to fix the problem for me |
I've reached this thread from Google and am trying to implement the answer @ilevkivskyi provided. I've figured out that it requires |
@helgridly T here is a TypeVar, from typing import TypeVar, Type, TYPE_CHECKING
if TYPE_CHECKING:
from sqlalchemy.sql.type_api import TypeEngine
T = TypeVar('T')
class Enum(TypeEngine[T]):
def __init__(self, enum: Type[T]) -> None: ...
else:
from sqlalchemy import Enum |
Thank you! |
Still not 100% correct. Enum can take some kwargs.
Probably would be better to make all the possible kwargs checked. |
Tried this solution but still get error state = Column(Enum(MyEnum), nullable=False)
# error: Need type annotation for 'state' [var-annotated] |
I have an SQLAlchemy model that makes use of the
Enum
column type. When accessing the field of an instance of this model, mypy believes that the type of the field isstr
even though it is actually an enum (e.g.MyEnum
). This is annoying since, when I do want to access its value, mypy fails witherror: "str" has no attribute "value"
.Although it cannot be run as such, the following snippet demonstrates the behavior when run through mypy. I would expect mypy to expect
m.state
should be of typeMyEnum
(really, in this snippet, it will beNone
, but in real code, it will be aMyEnum
).The text was updated successfully, but these errors were encountered: