python / python/typeshed

type annotated arguments in zip function lead to wrong return types when using the star operator

Aperta
#9,143 4 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Lingua principale
Python
Stelle
5.1k
Fork
2.1k
Merge medio
1g 19h
PR unite (30g)
82

Descrizione

This issue is a followup to the following issue:
https://github.com/microsoft/pylance-release/issues/3598

Here's the code that is wrongly annotated:

test_list: list[tuple[int, str]] = [(1, 'testa'), (2, 'testb')]

a: tuple[int]
b: tuple[str]

a, b = zip(*test_list)

Pylance warns about an assignment of typle[int | str] to the tuples a and b.
The tuple annotation is being converted to an Iterator annotation, which does not support a sequence of types (see Eric's reply to the previous issue for details). Therefore, the resulting tuples after the unpacking operation are not as precisely typed as they could be.

And here is the typeshed code that leads to the 'wrong' type annotations:

https://github.com/python/typeshed/blob/7b3fff714a48f0413141ba10344f9473e3ec4a18/stdlib/builtins.pyi#L1673-L1715

I'm not sure if it is currently possible to fix this. I had the idea of adding a type hint to the *args keyword, so something like this:

@overload
        def __new__(cls, __iter1: _T2:=Sequence[_T1], *args: _T2, strict: bool = ...) -> zip[tuple[_T1]]: 

I'm unsure if this is valid python syntax and if it would result in the desired behavior. I'm also unsure how python would decide between the proposed constructor and the one that is currently being used during typechecking my example:
https://github.com/python/typeshed/blob/7b3fff714a48f0413141ba10344f9473e3ec4a18/stdlib/builtins.pyi#L1675-L1676

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 riproducendo l'esempio annotato di zip(*test_list) e leggi gli overload di zip in stdlib/builtins.pyi intorno alle righe 1673-1715. Esamina l'issue di typeshed collegata e la precedente issue di Pylance prima di valutare una modifica agli overload. È completato quando le variabili spacchettate vengono inferite come tuple[int] e tuple[str] senza avvisi di assegnazione.

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

Valutazione

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.