You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Inconsistent errors with custom enum base auto items from installed library #15382
Hi, I've defined a custom enum base to copy the names of the enum items as values when using auto(). Mypy curiously throws an assignment error about incompatible types when using the items as e.g. default arguments in a function. However, this only happens if the custom base is imported from another library (in site packages, using another local package is fine).
The above is fine and produces no errors. However, when using the import instead, we get
Incompatible default for argument "item" (default has type "auto", argument has type "Items") [assignment]
I would assume that this is a bug, but if you have any other thoughts or debugging suggestions, let me know! I found another issue about the same private method in Enum in #7591, but the issue doesn't seem to be exactly related.
I believe _generate_next_value_ should be decorated with @staticmethod. But that crashes in python 3.8 and 3.9 . Not sure if that can be fixed on the type-definition side (see python/typeshed#10428)
I have a very similar case. We define a custom StrEnum class in a library A. If we reference it in library B (with B depends on A), mypy complains about the typing of any enum using this StrEnum as its parent.
Here are the definitions in library A
#library_a.enumsfromenumimportEnumfromtypingimportList, cast# We originally use a custom `BaseEnum` with a custom metaclass, # but using a simplified standard `enum.Enum` class also leads to typing errors. classStrEnum(str, Enum):
def__str__(self) ->str:
returnself.value@classmethoddefvalues(cls) ->List[str]:
return [cast(str, c.value) forcincls]
Here are the subclass definitions and use in library B
# library_b.mainfromlibrary_a.enumsimportStrEnumclassMyEnum(StrEnum):
A="a"B="b"defdummy_func(e: MyEnum) ->None:
pass# Raises Errordummy_func(MyEnum.A)
# error: Argument 1 to "dummy_func" has incompatible type "str"; expected "MyEnum" [arg-type]# dummy_func(MyEnum.A)
This type check failure is not raised if the StrEnum is defined in library B
I have expanded my tests and it seems that any Enum classes (subclassing any builtin enum class) defined in an installed / dependent library will trip up mypy
Here are definitions in library_a.enums
fromenumimportEnum, StrEnumclassDepStrEnumBuiltin(StrEnum):
defextra_method(self) ->str:
returnf"extra method called on {self}"classDepStrEnumCustom(str, Enum):
defextra_method(self) ->str:
returnf"extra method called on {self}"classDepEnum(Enum):
defextra_method(self) ->str:
returnf"extra method called on {self}"
Then in library_b.enums
fromenumimportEnum, StrEnumclassLocalStrEnumCustom(str, Enum):
defextra_method(self) ->str:
returnf"extra method called on {self}"classLocalStrEnumBuiltin(StrEnum):
defextra_method(self) ->str:
returnf"extra method called on {self}"
From these files the following mypy errors are raised:
> $ mypy ./src/library_b/main.py
src/library_b/main.py:57: error: "str" has no attribute "extra_method" [attr-defined]
MyDepEnum.A.extra_method()
^~~~~~~~~~~~~~~~~~~~~~~~
src/library_b/main.py:58: error: "str" has no attribute "extra_method" [attr-defined]
MyDepStrEnumCustom.A.extra_method()
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/library_b/main.py:59: error: "str" has no attribute "extra_method" [attr-defined]
MyDepStrEnumBuiltin.A.extra_method()
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/library_b/main.py:63: error: Argument 1 to "func_dep_enum" has incompatible type "str"; expected "MyDepEnum" [arg-type]
func_dep_enum(MyDepEnum.A)
^~~~~~~~~~~
src/library_b/main.py:64: error: Argument 1 to "func_dep_str_enum_custom" has incompatible type "str"; expected "MyDepStrEnumCustom" [arg-type]
func_dep_str_enum_custom(MyDepStrEnumCustom.A)
^~~~~~~~~~~~~~~~~~~~
src/library_b/main.py:65: error: Argument 1 to "func_dep_str_enum_builtin" has incompatible type "str"; expected "MyDepStrEnumBuiltin" [arg-type]
func_dep_str_enum_builtin(MyDepStrEnumBuiltin.A)
^~~~~~~~~~~~~~~~~~~~~
src/gcs_core/utils/deleteme.py:70: note: Revealed type is "builtins.str"
src/gcs_core/utils/deleteme.py:71: note: Revealed type is "builtins.int"
src/gcs_core/utils/deleteme.py:72: note: Revealed type is "builtins.bool"
src/gcs_core/utils/deleteme.py:73: note: Revealed type is "builtins.str"
src/gcs_core/utils/deleteme.py:74: note: Revealed type is "builtins.str"
src/gcs_core/utils/deleteme.py:75: note: Revealed type is "Literal[gcs_core.utils.deleteme.MyLocalStrEnumCustom.A]?"
src/gcs_core/utils/deleteme.py:76: note: Revealed type is "Literal[gcs_core.utils.deleteme.MyLocalStrEnumBuiltin.A]?"
Found 6 errors in 1 file (checked 1 source file)
Observations
mypy cannot identify any enum parent classes as being enums if they are defined in an installed library
reveal_type shows that the types of enum values in a class inheriting from installed package (MyDep*) are inferred as the plain type of the value on the right side of =
These enums work as expected at runtime.
Reacted by Andrew Mitchell
Reacted by Felix Hildén, Jordan Moldow, Zet, YossiMicrosoft and Jacek Rybak
I believe _generate_next_value_ should be decorated with @staticmethod. But that crashes in python 3.8 and 3.9 . Not sure if that can be fixed on the type-definition side (see python/typeshed#10428)
Activity
Avasam commentedon Aug 14, 2023
I believe
_generate_next_value_should be decorated with@staticmethod. But that crashes in python 3.8 and 3.9 . Not sure if that can be fixed on the type-definition side (see python/typeshed#10428)rpmcginty commentedon Oct 16, 2024
I have a very similar case. We define a custom
StrEnumclass in a library A. If we reference it in library B (with B depends on A), mypy complains about the typing of any enum using thisStrEnumas its parent.Here are the definitions in library A
Here are the subclass definitions and use in library B
This type check failure is not raised if the
StrEnumis defined in library BEnvironment
rpmcginty commentedon Oct 17, 2024
I have expanded my tests and it seems that any Enum classes (subclassing any builtin enum class) defined in an installed / dependent library will trip up mypy
Here are definitions in
library_a.enumsThen in
library_b.enumsthen in
library_b.mainFrom these files the following mypy errors are raised:
Observations
reveal_typeshows that the types of enum values in a class inheriting from installed package (MyDep*) are inferred as the plain type of the value on the right side of=Avasam commentedon May 18, 2026
mypy 1.17 has dropped support for Python 3.8:
and mypy 2.0 has dropped support for Python 3.9:
--python-version 3.9#21243So this should now be solvable by always decorating
_generate_next_value_with@staticmethod