python / python/typing_extensions

Starred unpack does not equal `Unpack` in Python 3.11

Aperta
#485 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Lingua principale
Python
Stelle
583
Fork
146
Merge medio
10h 11m
PR unite (30g)
5

Descrizione

Equivalence between typing and typing_extensions is something I do not expect, however I think the following example is an exception as the non-equivalence comes as a surprise:

# python 3.11
import typing
from typing_extensions import Unpack, TypeVarTuple, get_type_hints
Ts = TypeVarTuple("Ts")

def foo(*x: *Ts): ...

print(get_type_hints(foo)['x'] == Unpack[Ts])  # <--- False
print(get_type_hints(foo)['x'] == typing.Unpack[Ts])  # <--- True

# or minimal example:
print(next(iter(Ts)) == Unpack[Ts])  # False

Why does this happen?

The 3.11+ backport of TypeVarTuple returns a patched instance tvt = typing.TypeVarTuple, which in turn will unpack to typing.Unpack[Ts] when __iter__ is used. (Unpack is backported until 3.11)

https://github.com/python/typing_extensions/blob/17d3a37635bad3902c4e913a48d969cbebfb08c3/src/typing_extensions.py#L2473

Possible Fixes

  1. Likely bad idea: overwrite tvt.__iter__ to return typing_extensions.Unpack; might cause same problem at other places.

  2. Add __eq__ to typing_extensions.Unpack to equal with typing.Unpack. BUT, this will only be valid for the left-hand-side.

    print(Unpack[Ts] == get_type_hints(foo)['x'])  # <--- True
    print(get_type_hints(foo)['x'] == Unpack[Ts])  # <--- False
    

    Fix for the right-hand-side:
    a) typing_extensions._UnpackAlias must inherit from typing._UnpackGenericAlias for right-hand side priority.
    b) add typing._UnpackGenericAlias.__eq__ via monkey-patch

    Sideeffects: All Unpack and *-unpacks will be equivalent

  3. do not fix, but add a limitation/warning to the documentation that in 3.11

    typing_extensions.Unpack[Ts] != *Ts == typing.Unpack[Ts]  # pseudocode
    

I already have the necessary code for 2.a, but I am not sure what your stance is on this. Should equality be introduced, and if so is subclassing acceptable?

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 da src/typing_extensions.py intorno alla riga 2473 e riproduci gli esempi di uguaglianza di Python 3.11 riportati nell’issue. Esamina le alternative proposte per l’uguaglianza, la sottoclasse, il monkey-patching e la documentazione, quindi stabilisci quale comportamento desiderano i maintainer; il lavoro è completo quando il comportamento o la limitazione scelti sono documentati e verificati.

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

Valutazione

Stack tecnologico
python
Ambito
tooling
Tipo di issue
Bug
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.