python / python/typing_extensions

Starred unpack does not equal `Unpack` in Python 3.11

Abierto
#485 1 comentario 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Lenguaje dominante
Python
Estrellas
583
Forks
146
Merge medio
10 h 11 min
PR fusionados (30 d)
5

Descripción

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?

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Empieza por src/typing_extensions.py alrededor de la línea 2473 y reproduce los ejemplos de igualdad de Python 3.11 del issue. Revisa las alternativas propuestas de igualdad, herencia, monkey-patching y documentación, y determina después qué comportamiento quieren los maintainers; se considera terminado cuando el comportamiento o la limitación elegidos estén documentados y verificados.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
python
Área
tooling
Tipo de issue
Error
Dificultad
5/5
Tiempo estimado
Más de una semana
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
25/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.