python / python/mypy

Redefinition using the same type should not be an error

Ouverte
#15,664 0 commentaires 0 réactions 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

Possibly related to #10563.

Bug Report

Consider the following:

# a.py

x: int = 5
# b.py

from a import *

x: int = 7 # error: Name "x" already defined (possibly by an import)  [no-redef]

I would not call it an error. Sure, it's already defined, but it was an int originally.

If you do this:

# c.py

from a import *

x: float = 7.5 # error: Name "x" already defined (possibly by an import)  [no-redef]

You get the same error, but in this case, I'd say that's correct, because maybe I didn't realize it was an int.

And this:

# d.py

from a import *

x = 'boo' # error: Incompatible types in assignment (expression has type "str", variable has type "int")  [assignment]

This I also agree with.

Expected Behavior
I would argue that I should not get an error in b.py, whereas the errors in c.py and d.py are correct.

Typehints to me are as much a documentation tool as they are indispensable to finding bugs. Not being able to reiterate that something is an int means that I cannot document it in the code. I believe this is particularly important with star-imports because by just reading the current file, you have no idea what's being imported.

Here's something else to consider:

# a.py

x: float = 3.14
# b.py

from a import *

x = 5 # no error, because 5 is a float.

Do we know that the author new that x is a float and thus 5 is OK, or did they really mean to write x: int = 5? If I were allowed to write x: float = 5, we would have a definitive answer.

  • Mypy version used: 1.3.0
  • Mypy configuration options from mypy.ini (and other config files): ignore_missing_imports = True
  • Python version used: 3.8.10

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 les exemples de a.py, b.py, c.py et d.py avec la configuration mypy et la version de Python signalées. Comparez les diagnostics pour la redéfinition avec le même type, les annotations différentes et l’affectation incompatible. Le travail est terminé lorsque les cas attendus sont distingués de manière cohérente et qu’une couverture de régression du comportement signalé est présente.

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

Évaluation

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

Recevez les nouvelles issues par e-mail

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