Skip to content

Enum imported from another module raises "Incompatible types error" #153

Open
@abprime

Description

@abprime

The following code runs fine without any mypy error

# file_type.py
from enum import Enum
from sqlalchemy.dialects.postgresql import ENUM

class FileType(str, Enum):
    CSV = "CSV"
    XLSX = "XLSX"

file_type_db_enum = ENUM(FileType, name="file_type")
# input_file.py
from file_type import FileType
from sqlalchemy import Column
from sqlalchemy.dialects.postgresql import ENUM
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()
class InputFile(Base):
    file_type: FileType = Column(ENUM(FileType, name="file_type"))

But changing input file to following throws mypy error

# input_file.py
from file_type import file_type_db_enum
from sqlalchemy import Column
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()
class InputFile(Base):
    file_type: FileType = Column(file_type_db_enum)
input_file.py:7: error: Incompatible types in assignment (expression has type "Column[NullType]", variable has type "FileType")

Note : I am importing the file_type_db_enum, as it is being reused by multiple other models, and also, because graphene-sqlalchemy wants it to be declared this way, Otherwise it generates conflicts for multiple objectType with the same name.

Expected behavior
This should not raise any errors.

To Reproduce

# file_type.py
from enum import Enum
from sqlalchemy.dialects.postgresql import ENUM

class FileType(str, Enum):
    CSV = "CSV"
    XLSX = "XLSX"

file_type_db_enum = ENUM(FileType, name="file_type")

# input_file.py
from file_type import file_type_db_enum
from sqlalchemy import Column
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()
class InputFile(Base):
    file_type: FileType = Column(file_type_db_enum)

Error

input_file.py:7: error: Incompatible types in assignment (expression has type "Column[NullType]", variable has type "FileType")

Versions.

  • OS: Windows 10
  • Python: 3.8.10
  • SQLAlchemy: 1.4.22
  • mypy: 0.910
  • SQLAlchemy2-stubs: 0.0.2a8

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingmypy pluginsomething that has to do with the sqlalchemy mypy plugin

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions