`Enum` created via functional API should be iterable
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
Bug Report
The Python docs website document enum items as iterables:
>>> Animal = Enum('Animal', 'ANT BEE CAT DOG')
>>> Animal
<enum 'Animal'>
>>> Animal.ANT
<Animal.ANT: 1>
>>> list(Animal)
[<Animal.ANT: 1>, <Animal.BEE: 2>, <Animal.CAT: 3>, <Animal.DOG: 4>]
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
# Gist URL: https://gist.github.com/mypy-play/d41bcc40b2df9dbfb6f5a0cff7c18681
# Playground URL: https://mypy-play.net/?mypy=latest&python=3.12&gist=d41bcc40b2df9dbfb6f5a0cff7c18681
from enum import Enum
(LATEST,) = Enum("v", "LATEST")
DOWNLOADS = {
"file1.txt": LATEST,
"file2.html": 7,
"file3.css": 3,
"file4.txt": 5,
"file5.txt": LATEST,
}
def download_all() -> None:
for file, version in DOWNLOADS.items():
if version is LATEST:
url = f"http://latest.example.url/files/{file}"
else:
url = f"http://example.url/files/version/{version}/{file}"
_download(url)
def _download(url: str) -> None:
print(f"... pretend to be downloading {url} ...")
if __name__ == "__main__":
download_all()
Note that I am using Enum in this example because it is much nicer to work with than LATEST = object(), it has a very good __repr__ and if at any point I need to add more special values, I can do that easily. Overall Enum'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:
$ python3.11 main.py
... pretend to be downloading http://latest.example.url/files/file1.txt ...
... pretend to be downloading http://example.url/files/version/7/file2.html ...
... pretend to be downloading http://example.url/files/version/3/file3.css ...
... pretend to be downloading http://example.url/files/version/5/file4.txt ...
... pretend to be downloading http://latest.example.url/files/file5.txt ...
Actual Behavior
main.py:3: error: "Enum" object is not iterable [misc]
main.py:6: error: Cannot determine type of "LATEST" [has-type]
main.py:10: error: Cannot determine type of "LATEST" [has-type]
main.py:15: error: Cannot determine type of "LATEST" [has-type]
Found 4 errors in 1 file (checked 1 source file)
Your Environment
- Mypy version used:
1.8.0 - Mypy command-line flags: None (this is how I run
pipx run --python python3.11 'mypy==1.8.0' main.py) - Mypy configuration options from
mypy.ini(and other config files): None - Python version used:
Python 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 the Enum 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 the Enum "class" itself is not assigned as a member of another object.
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia eseguendo mypy sulla riproduzione di main.py e verifica che l’assegnazione funzionale di Enum produca gli errori segnalati. Traccia il modo in cui viene dedotta l’API funzionale di Enum, quindi aggiungi la copertura per l’iterazione e il membro LATEST; il lavoro è completo quando la riproduzione supera il controllo dei tipi senza errori, preservando al contempo il comportamento documentato a runtime.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- devtools
- Tipo di issue
- Bug
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 45/100