python / python/mypy

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

Ouverte
#17,593 0 commentaires 1 réaction 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

bug
Langage dominant
Python
Étoiles
20.6k
Forks
3.3k
Métriques de merge des PR
Métriques de PR en attente

Description

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.

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par reproduire le problème avec l’extrait minimal de Python du rapport sous mypy 1.10.1, puis comparez le comportement avec et sans TypeAlias, type(None) et l’affectation ultérieure ignorée. Suivez le traitement de l’alias et de la ligne ultérieure qui produit l’erreur ; le travail est terminé lorsqu’aucune erreur no-redef parasite ne se produit et qu’un diagnostic invalid-type cohérent est produit indépendamment des autres erreurs.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
python
Domaine
devtools
Type d'issue
Bug
Difficulté
4/5
Temps estimé
3-5 jours
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
25/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.