python / python/mypy

`attr.ib(converter=)` appears to cause mypy to lose generic types for `__init__` function.

Aperta
#8,417 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug topic-attrs
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

$ 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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia eseguendo l'esempio attr_test.py fornito con mypy 0.760 e confronta i tipi init rivelati per Foo, Bar e Raz. Ricostruisci il motivo per cui converter=tuple produce Iterable[_T_co] e converter=str produce object. Il lavoro è completo quando l'esempio supera correttamente il controllo dei tipi e Bar.init risulta accettare Iterable[str].

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
tooling
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.