python / python/mypy

tuple element types wrongly mixed as union

Abierto
#20,188 2 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

bug topic-join-v-union
Lenguaje dominante
Python
Estrellas
20.6k
Forks
3.3k
Métricas de merge de PR
Métricas de PR pendientes

Descripción

Bug Report

When a variable conditionally gets one or the other of a set of fixed-length tuples, instead of being inferred as a union of said tuple types, mypy sees an arbibrary-length tuple whose elements are typed with a union of all element types involved. This results false-positive errors being reported.

To Reproduce

https://mypy-play.net/?mypy=latest&python=3.12&gist=4780da0a891d33c46ee7ac0b55a05f24

Expected Behavior

Similar to what pyright reveals, a union of two tuple types with defined length and element types, and no error when assigning :

  /home/user/omaha/omaha/uitest/mypy-test.py:9:17 - information: Type of "item" is "tuple[Literal[1], Literal['a'], list[bool]] | tuple[Literal[1], Literal['a']]"

Actual Behavior

The type is instead inferred to be a tuple of arbitrary length, with all elements allowed to have any of the types seen inside the tuple:

main.py:9: note: Revealed type is "builtins.tuple[builtins.int | builtins.str | builtins.list[builtins.bool], ...]"
main.py:10: error: List item 0 has incompatible type "tuple[int | str | list[bool], ...]"; expected "tuple[int, str] | tuple[int, str, Sequence[bool]]"  [list-item]

Note that when using just item = (1, "a"), removing the conditional part, the tuple is properly identified with fixed types, and the assignment is validated by mypy:

main.py:9: note: Revealed type is "tuple[builtins.int, builtins.str]"

Your Environment

  • Mypy version used: 1.18.2
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.12, 3.14.0rc2

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 ejecutando la reproducción enlazada de mypy-play con mypy 1.18.2 y compara el tipo revelado y el diagnóstico de asignación con el resultado esperado de pyright. Traza el comportamiento de inferencia de tuplas implicado en la asignación condicional; se considera terminado cuando las alternativas de tuplas de longitud fija siguen siendo una unión y la asignación mostrada ya no produce un falso positivo.

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

Evaluación

Stack tecnológico
python
Área
compilers
Tipo de issue
Error
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
42/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.