`attr.ib(converter=)` appears to cause mypy to lose generic types for `__init__` function.
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
$ mypy --version
mypy 0.760
attr_test.py:
import attr
from typing import Sequence
@attr.s(auto_attribs=True)
class Foo:
things: Sequence[str]
@attr.s(auto_attribs=True)
class Bar:
things: Sequence[str] = attr.ib(converter=tuple)
@attr.s(auto_attribs=True)
class Raz:
thing: str = attr.ib(converter=str)
Foo(['hello', 'world'])
Raz('hello')
Bar(['hello', 'world'])
reveal_type(Foo.__init__)
reveal_type(Bar.__init__)
reveal_type(Raz.__init__)
Output:
$ mypy attr_test.py
attr_test.py:18: error: List item 0 has incompatible type "str"; expected "_T_co"
attr_test.py:18: error: List item 1 has incompatible type "str"; expected "_T_co"
attr_test.py:19: note: Revealed type is 'def (self: attr_test.Foo, things: typing.Sequence[builtins.str])'
attr_test.py:20: note: Revealed type is 'def (self: attr_test.Bar, things: typing.Iterable[_T_co`1])'
attr_test.py:21: note: Revealed type is 'def (self: attr_test.Raz, thing: builtins.object)'
I would expect attr_test.py to typecheck cleanly and for the signature of Bar.__init__ to be def (self: attr_test.Bar, things: typing.Iterable[str]) . I'm guessing mypy created a new type _T_co`1 that's something like TypeVar('T', bound=str, covariant=True) but it doesn't seem like that's being used later.
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 exécuter l’exemple attr_test.py fourni avec mypy 0.760 et comparez les types init révélés pour Foo, Bar et Raz. Déterminez pourquoi converter=tuple produit Iterable[_T_co] et converter=str produit object. C’est terminé lorsque l’exemple passe la vérification de types sans erreur et que Bar.init est révélé comme acceptant Iterable[str].
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
- 35/100