Inference on simple list comprehension type not working

Abierto
#5,068 4 comentarios 11 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

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

Línea de trabajo

Start by reproducing the test_1/test_2 example with the shown mypy version and compare the revealed type with the expected fixed-length tuple. Trace the inference for the generator expression passed to tuple(); done means the unchanged elements preserve the input tuple's length and test_1 accepts the result, with regression coverage for this example.

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

Descripción

feature priority-2-low

This is a feature request.

from typing import Tuple
def test_1(inp1: Tuple[int, int, int]) -> None:
    pass

def test_2(inp2: Tuple[int, int, int]) -> None:
    test_tuple = tuple(e for e in inp2)
    reveal_type(test_tuple)
    test_1(test_tuple)

The code above, when run with mypy 0.590 (no flags) and Python 3.6, results in:

mypytest.py:7: error: Revealed type is 'builtins.tuple[builtins.int*]'
mypytest.py:8: error: Argument 1 to "test_1" has incompatible type "Tuple[int, ...]"; expected "Tuple[int, int, int]"

I appreciate that the types inside a list comprehension are not trivial to infer, but maybe mypy could at least handle a case where there are no operations on the elements, and I'm looping through all elements, as shown above? The expected type of test_tuple would then be builtins.tuple[builtins.int, builtins.int, builtins.int].

Lenguaje dominante
Python
Estrellas
20.6k
Forks
3.3k
Merge medio
1 d 18 h
PR fusionados (30 d)
54

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.

Más de python/mypy

Todos los issues de python/mypy

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.