Better error message for incompatible type assignment when using Generics
Personne n'a encore pris cette issue.
- Langage dominant
- Python
- Étoiles
- 20.6k
- Forks
- 3.3k
- Métriques de merge des PR
- Métriques de PR en attente
Description
- Are you reporting a bug, or opening a feature request?
Feature Request
- Please insert below the code you are checking with mypy.
from typing import Generic, TypeVar
T = TypeVar("T", int, float)
class Foo(Generic[T]):
def __init__(self, val: T):
self.data: T = val
def bar(x: T) -> Foo[T]:
return Foo(x)
x = bar(1)
x = bar(2.0) # error thrown by mypy
- What is the actual behavior/output?
mypy throws this error:
error: Argument 1 to "bar" has incompatible type "float"; expected "int"
- What is the behavior/output you expect?
This error message is thrown because of the combination of assigning a different type to x and using Generic. The error message is unclear because it indicates a type error on the argument passed to bar and can be interpreted as bar not accepting arguments of type float.
Having a different error message that mentions "Incompatible types in assignment" would be clearer.
A message to make it clearer that expression has type Foo[float], variable has type Foo[int]
- What are the versions of mypy and Python you are using?
mypy 0.770 and Python 3.7.6
- What are the mypy flags you are using? (For example --strict-optional)
None
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Commencez par reproduire l’exemple Generic avec mypy 0.770 et Python 3.7.6, en vous concentrant sur l’affectation de Foo[int] à Foo[float]. Suivez le diagnostic produit pour l’appel à bar et l’affectation à x ; le travail est terminé lorsque le message identifie clairement les types incompatibles de la variable et de l’expression, plutôt que de laisser entendre que bar refuse les arguments float.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- python
- Domaine
- compilers
- Type d'issue
- Fonctionnalité
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 35/100