-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Enum
created via functional API should be iterable
#16936
Comments
Enum
object created via functional API should be iterableEnum
created via functional API should be iterable
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 |
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. |
* fix minijinja.py to mimic what we do on other templates so that any regitered callable can access the scope ie _transform_state is in the wrong spot * added a flash function and its pending get_flashes one to be used in templates * can't use StrEnum * can't use StrEnum in tests as well * remove StrEnum entirely * enum not iterable ? * silence mypy on this one, python/mypy#16936 related ? * same * TIL about ScopeState * Silence another mypy error that works in test_temmplate... * semi-ugly "fix" * another way of looking at minjinja implementation * Yes I debug with print sometimes * still fails because get_flashes is not decorted properly in minijinja implementation, there should be a way to detect * another "working" option, no preference vs other options * another "working" option, no preference vs other options * Removed the transformed kwrd, would be breaking probably * Removed debug * Removed docstring leftover * Mypy * Demonstrated superior-mypy-foo-skills by using type ignore * Removed FLashCategory from plugin, shouldn't the app_init get it passed in the config ?? * docs: add plugin docs for flash messages * docs: add plugin docs for flash messages * Update docs/usage/index.rst * fix: update file path * fix: make sphinx gods happy * docs: add more documentation * docs: move api doc plugins into directory * revert pdm.lock to it's original state * living the dangerous life of not testing code * typo --------- Co-authored-by: Janek Nouvertné <[email protected]> Co-authored-by: Jacob Coffee <[email protected]>
* fix minijinja.py to mimic what we do on other templates so that any regitered callable can access the scope ie _transform_state is in the wrong spot * added a flash function and its pending get_flashes one to be used in templates * can't use StrEnum * can't use StrEnum in tests as well * remove StrEnum entirely * enum not iterable ? * silence mypy on this one, python/mypy#16936 related ? * same * TIL about ScopeState * Silence another mypy error that works in test_temmplate... * semi-ugly "fix" * another way of looking at minjinja implementation * Yes I debug with print sometimes * still fails because get_flashes is not decorted properly in minijinja implementation, there should be a way to detect * another "working" option, no preference vs other options * another "working" option, no preference vs other options * Removed the transformed kwrd, would be breaking probably * Removed debug * Removed docstring leftover * Mypy * Demonstrated superior-mypy-foo-skills by using type ignore * Removed FLashCategory from plugin, shouldn't the app_init get it passed in the config ?? * docs: add plugin docs for flash messages * docs: add plugin docs for flash messages * Update docs/usage/index.rst * fix: update file path * fix: make sphinx gods happy * docs: add more documentation * docs: move api doc plugins into directory * revert pdm.lock to it's original state * living the dangerous life of not testing code * typo --------- Co-authored-by: Janek Nouvertné <[email protected]> Co-authored-by: Jacob Coffee <[email protected]>
* fix minijinja.py to mimic what we do on other templates so that any regitered callable can access the scope ie _transform_state is in the wrong spot * added a flash function and its pending get_flashes one to be used in templates * can't use StrEnum * can't use StrEnum in tests as well * remove StrEnum entirely * enum not iterable ? * silence mypy on this one, python/mypy#16936 related ? * same * TIL about ScopeState * Silence another mypy error that works in test_temmplate... * semi-ugly "fix" * another way of looking at minjinja implementation * Yes I debug with print sometimes * still fails because get_flashes is not decorted properly in minijinja implementation, there should be a way to detect * another "working" option, no preference vs other options * another "working" option, no preference vs other options * Removed the transformed kwrd, would be breaking probably * Removed debug * Removed docstring leftover * Mypy * Demonstrated superior-mypy-foo-skills by using type ignore * Removed FLashCategory from plugin, shouldn't the app_init get it passed in the config ?? * docs: add plugin docs for flash messages * docs: add plugin docs for flash messages * Update docs/usage/index.rst * fix: update file path * fix: make sphinx gods happy * docs: add more documentation * docs: move api doc plugins into directory * revert pdm.lock to it's original state * living the dangerous life of not testing code * typo --------- Co-authored-by: Janek Nouvertné <[email protected]> Co-authored-by: Jacob Coffee <[email protected]>
* fix minijinja.py to mimic what we do on other templates so that any regitered callable can access the scope ie _transform_state is in the wrong spot * added a flash function and its pending get_flashes one to be used in templates * can't use StrEnum * can't use StrEnum in tests as well * remove StrEnum entirely * enum not iterable ? * silence mypy on this one, python/mypy#16936 related ? * same * TIL about ScopeState * Silence another mypy error that works in test_temmplate... * semi-ugly "fix" * another way of looking at minjinja implementation * Yes I debug with print sometimes * still fails because get_flashes is not decorted properly in minijinja implementation, there should be a way to detect * another "working" option, no preference vs other options * another "working" option, no preference vs other options * Removed the transformed kwrd, would be breaking probably * Removed debug * Removed docstring leftover * Mypy * Demonstrated superior-mypy-foo-skills by using type ignore * Removed FLashCategory from plugin, shouldn't the app_init get it passed in the config ?? * docs: add plugin docs for flash messages * docs: add plugin docs for flash messages * Update docs/usage/index.rst * fix: update file path * fix: make sphinx gods happy * docs: add more documentation * docs: move api doc plugins into directory * revert pdm.lock to it's original state * living the dangerous life of not testing code * typo --------- Co-authored-by: Janek Nouvertné <[email protected]> Co-authored-by: Jacob Coffee <[email protected]>
* fix minijinja.py to mimic what we do on other templates so that any regitered callable can access the scope ie _transform_state is in the wrong spot * added a flash function and its pending get_flashes one to be used in templates * can't use StrEnum * can't use StrEnum in tests as well * remove StrEnum entirely * enum not iterable ? * silence mypy on this one, python/mypy#16936 related ? * same * TIL about ScopeState * Silence another mypy error that works in test_temmplate... * semi-ugly "fix" * another way of looking at minjinja implementation * Yes I debug with print sometimes * still fails because get_flashes is not decorted properly in minijinja implementation, there should be a way to detect * another "working" option, no preference vs other options * another "working" option, no preference vs other options * Removed the transformed kwrd, would be breaking probably * Removed debug * Removed docstring leftover * Mypy * Demonstrated superior-mypy-foo-skills by using type ignore * Removed FLashCategory from plugin, shouldn't the app_init get it passed in the config ?? * docs: add plugin docs for flash messages * docs: add plugin docs for flash messages * Update docs/usage/index.rst * fix: update file path * fix: make sphinx gods happy * docs: add more documentation * docs: move api doc plugins into directory * revert pdm.lock to it's original state * living the dangerous life of not testing code * typo --------- Co-authored-by: Janek Nouvertné <[email protected]> Co-authored-by: Jacob Coffee <[email protected]>
* fix minijinja.py to mimic what we do on other templates so that any regitered callable can access the scope ie _transform_state is in the wrong spot * added a flash function and its pending get_flashes one to be used in templates * can't use StrEnum * can't use StrEnum in tests as well * remove StrEnum entirely * enum not iterable ? * silence mypy on this one, python/mypy#16936 related ? * same * TIL about ScopeState * Silence another mypy error that works in test_temmplate... * semi-ugly "fix" * another way of looking at minjinja implementation * Yes I debug with print sometimes * still fails because get_flashes is not decorted properly in minijinja implementation, there should be a way to detect * another "working" option, no preference vs other options * another "working" option, no preference vs other options * Removed the transformed kwrd, would be breaking probably * Removed debug * Removed docstring leftover * Mypy * Demonstrated superior-mypy-foo-skills by using type ignore * Removed FLashCategory from plugin, shouldn't the app_init get it passed in the config ?? * docs: add plugin docs for flash messages * docs: add plugin docs for flash messages * Update docs/usage/index.rst * fix: update file path * fix: make sphinx gods happy * docs: add more documentation * docs: move api doc plugins into directory * revert pdm.lock to it's original state * living the dangerous life of not testing code * typo --------- Co-authored-by: Janek Nouvertné <[email protected]> Co-authored-by: Jacob Coffee <[email protected]>
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.Footnotes
https://github.com/python/mypy/issues/8009#issuecomment-558327474 ↩
https://github.com/python/mypy/issues/8009#issuecomment-558335186 ↩
The text was updated successfully, but these errors were encountered: