Skip to content

Enum interpreted as str #114

Open
Open
@qsantos

Description

@qsantos

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 is str even though it is actually an enum (e.g. MyEnum). This is annoying since, when I do want to access its value, mypy fails with error: "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 type MyEnum (really, in this snippet, it will be None, but in real code, it will be a MyEnum).

import enum

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


class MyEnum(enum.Enum):
    A = 'A'
    B = 'B'


Base = declarative_base()


class MyModel(Base):
    state = Column(Enum(MyEnum), nullable=False)


m = MyModel()
m.state.value

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions