Open abravalheri opened 7 months ago
Mypy is able to handle this pattern if you make the following small modification to your code.
v = Enum("v", "LATEST")
LATEST = v.LATEST
This approach is preferable from a static type checking perspective because it allows the type checker to evaluate a more specific (narrower) type for LATEST
: Literal[v.LATEST]
. By contrast, if you rely on the iterable interface to Enum
, the evaluated type will be v
.
Hi @erictraut, thank you very much for the suggestion.
Yes, this is a very good workaround for the bug in the question.
The unnecessary verboseness (and unnecessary introduction of an extra binding in the module namespace) are not really my cup of tea, so for the time being, I will just add an ignore comment until hopefully the problem goes away in some decades down the line 🤞.
Anyway, the error message is very misleading.
Bug Report
The Python docs website document
enum
items as iterables:https://docs.python.org/3/howto/enum.html#functional-api
This definition is also used in typeshed: https://github.com/python/typeshed/blob/a2095002e446bd0e20f8a469a14e271cd6cc6ad9/stdlib/enum.pyi#L105.
However,
mypy
fails to recognise this characteristic when using the functional API.To Reproduce
Note that I am using
Enum
in this example because it is much nicer to work with thanLATEST = object()
, it has a very good__repr__
and if at any point I need to add more special values, I can do that easily. OverallEnum
's functional API is an all-rounder for defining constants.Expected Behavior
Mypy should find no error when checking the example.
Indeed no runtime error can be found, and the program runs as expected:
Actual Behavior
Your Environment
1.8.0
pipx run --python python3.11 'mypy==1.8.0' main.py
)mypy.ini
(and other config files): NonePython 3.11.6 (main, Oct 23 2023, 22:47:21) [GCC 9.4.0]
A related problem (but not identical use case) has been previously closed in the tracker #8009. Please note, however, that the explanations (^1, ^2) for closing that other issue do not apply to this use case:
In summary, the other issue described a situation where an
Enum
was constantly re-defined as a class attribute inside of class'__init__
function. The report was dismissed because the constant re-definition of theEnum
would cause subtle bugs.In this use case however, the
Enum
is only defined once during the evaluation of the module by the import machinery. It is never re-evaluated and theEnum
"class" itself is not assigned as a member of another object.