Bad self type inference with Generics and inheritance?
Ouverte
Personne n'a encore pris cette issue.
bug
priority-2-low
- 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?
Reporting a bug
- Please insert below the code you are checking with mypy
from typing import TypeVar, Generic
T = TypeVar("T")
class Foo(Generic[T]):
def __init__(self): ...
class Bar(Generic[T]): ...
class FooChild(Foo[Bar[T]]):
def __init__(self, blah: T):
Foo.__init__(self)
- What is the actual behavior/output?
$ mypy foo.py
foo.py:18: error: Argument 1 to "__init__" has incompatible type "FooChild[T]"; expected "Foo[T]"
Found 1 error in 1 file (checked 1 source file)
- What is the behavior/output you expect?
Success: no issues found in 1 source file
- What are the versions of mypy and Python you are using?
mypy 0.750
Python 3.7.3
- Do you see the same issue after installing mypy from Git master?
Yes, successfully reproduced with mypy 0.790+dev.df6894b5a9bb1a913e66df87138cd7a240c09fc1
- What are the mypy flags you are using? (For example --strict-optional)
No flags.
- If mypy crashed with a traceback, please paste
the full traceback below.
No crash
- Additional information:
The following versions typecheck successfully:
from typing import TypeVar, Generic
T = TypeVar("T")
class Foo(Generic[T]):
def __init__(self): ...
class Bar(Generic[T]): ...
class FooChild(Foo[T]): # Removed one level of nesting here
def __init__(self, blah: T):
Foo.__init__(self)
from typing import TypeVar, Generic
T = TypeVar("T")
class Foo(Generic[T]):
def __init__(self): ...
class Bar(Generic[T]): ...
class FooChild(Foo[Bar[T]]):
def __init__(self): # Removed the argument here
Foo.__init__(self)
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 le reproducteur foo.py fourni et exécutez-le avec les versions signalées de mypy, en confirmant le type self incorrect pour FooChild[T]. Suivez l’héritage générique et l’appel au constructeur impliqués dans l’inférence de types de mypy, puis ajoutez une couverture montrant que le cas imbriqué Bar[T] réussit sans erreur.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- python
- Domaine
- compilers
- Type d'issue
- Bug
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 35/100