Spurious redef warning in an odd situation with type(None) plus TypeAlias plus a later # type: ignore
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Really bizarre behavior here that I can't figure out. Probably not high priority as it's likely an extreme edge case.
This code:
from typing import TypeAlias
abc: TypeAlias = type(None)
x = y # type: ignore
generates the following error in mypy 1.10.1 running on Python 3.11.8:
tt.py:3: error: Name "abc" already defined on line 3 [no-redef]
Found 1 error in 1 file (checked 1 source file)
This is obviously wrong, as abc is not a redefinition (and you can rename it to whatever you want, you get the same error).
This error does NOT happen if you do any of the following:
- Use
typeinstead ofTypeAliasas the declared type of abc. - Use any value other than type(None) as the value of the expression (if you use type(5), for example, you get the slightly more reasonable although still inaccurate error message that the expression isn't a valid type; I think it's reasonable to expect type literals only rather than at-runtime types though, so this is understandable.
- If you don't include the last line which generates a second mypy error (in that case, you get NO error on the abc line, not even the "not a valid type" error you'd get for assigning to type(5)).
The presence of the # type: ignore comment is optional as it turns out, it doesn't matter whether the second error is generated or suppressed, just that there is a second error in the file.
What should happen? IDK fix the underlying bug here. Probably we should get a "not a valid type" error similar to the type(5) situation, and of course that should happen regardless of whether there are or aren't errors elsewhere in the file.
Workaround: import NoneType from the types module is probably cleanest.
I'll mention here for others who might find this looking for how to use NoneType as a type annotation that you'll get a warning from mypy if you use NoneType as a type hint, saying you should use None instead (and yo should probably do that). The code that inspired this used NoneType not as a type hint but for a runtime isinstance check.
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 damit, das Problem mit dem minimalen Python-Snippet im Bericht unter mypy 1.10.1 zu reproduzieren, und vergleiche dann das Verhalten mit und ohne TypeAlias, type(None) und der späteren ignorierten Zuweisung. Verfolge die Verarbeitung des Alias und der späteren fehlererzeugenden Zeile; abgeschlossen ist die Aufgabe, wenn kein überflüssiger no-redef-Fehler auftritt und unabhängig von anderen Fehlern eine konsistente invalid-type-Diagnose ausgegeben wird.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- devtools
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 25/100