python / python/mypy

Spurious redef warning in an odd situation with type(None) plus TypeAlias plus a later # type: ignore

Aperta
#17,593 0 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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:

  1. Use type instead of TypeAlias as the declared type of abc.
  2. 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.
  3. 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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia riproducendo il problema con lo snippet Python minimo riportato nell’issue usando mypy 1.10.1, quindi confronta il comportamento con e senza TypeAlias, type(None) e l’assegnazione successiva ignorata. Traccia la gestione dell’alias e della riga successiva che produce l’errore; il lavoro è completo quando non viene prodotto alcun errore no-redef spurio e viene fornita una diagnosi invalid-type coerente indipendentemente dagli altri errori.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.