Interaction between an `enum` alias and `assert_never()` seems to fail
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Bug Report
If you have defined an enum with some values and an extra value which is an alias of an already defined one, the completeness-check using assert_never(...) does not work as (I) expected.
To Reproduce
# file mypy_enum_alias.py
import enum
from typing import assert_never
class Version(enum.Enum):
V1 = enum.auto()
V2 = enum.auto()
DEFAULT = V2
def print_version(version: Version) -> None:
match version:
case Version.V1:
print("Version 1")
case Version.V2:
print("Version 2")
case _:
assert_never(version)
print(*(repr(v) for v in Version))
$ mypy mypy_enum_alias.py
Expected Behavior
Success: no issues found in 1 source file
Actual Behavior
mypy_enum_alias.py:18: error: Argument 1 to "assert_never" has incompatible type "Literal[Version.DEFAULT]"; expected "Never" [arg-type]
Found 1 error in 1 file (checked 1 source file)
Your Environment
- Mypy version used:
mypy 1.19.1 (compiled: yes) - Mypy command-line flags: (none)
- Mypy configuration options from
mypy.ini(and other config files):# pyproject.toml # ... [tool.mypy] plugins = [ "pydantic.mypy" ] # ... - Python version used:
Python 3.13.9
If you run the script, Python bravely emits only the (two) non-aliased values:
$ python mypy_enum_alias.py
<Version.V1: 1> <Version.V2: 2>
If mypy considers all "names" of an enum, it renders the completeness check useless IMHO.
I could add the following code snippet just before case _: ... as a work-around, but that would just be pathetic:
case Version.DEFAULT:
pass
But for the rest I'm quite happy with mypy. A great helper in early bug detection!
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit dem Reproducer in mypy_enum_alias.py und führe mypy ohne Befehlszeilen-Flags aus, um die fehlerhafte assert_never-Diagnose zu bestätigen. Verfolge, wie mypy Enum-Aliase während der Exhaustivitätsprüfung von match behandelt, und füge dann Tests hinzu oder aktualisiere sie, sodass das Beispiel Erfolg meldet, ohne einen Version.DEFAULT-Fall zu benötigen.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- devtools
- Issue-Typ
- Bug
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Aktivitätsstatus
- Ruhig
- Klarheit
- Klar beschrieben
- Anfängerfreundlichkeit
- 68/100