False positive for function that takes a `Callable` as a parameter and returns a `Tuple` of `Union`

Abierto
#10,281 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

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

Línea de trabajo

Comienza ejecutando el fragmento de Python proporcionado con mypy 0.812 y confirma el error en la llamada de desempaquetado. Rastrea la inferencia de variables de tipo para Callable, Tuple y Union; se considera terminado cuando el reproductor pasa la comprobación de tipos sin el falso positivo informado y la cobertura de regresión protege este comportamiento.

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

Descripción

bug

Bug Report

The following code

from typing import (
    Callable,
    TypeVar,
    Union,
    Tuple,
)


T = TypeVar("T")


def test(pred: Callable[[T], bool]) -> Tuple[Union[T, int]]:
    return (1,)


def main(pred: Callable[[T], bool]) -> None:
    (val,) = test(pred)  # the error appears here


def f(x: int) -> bool:
    return x > 1


main(f)

triggers the mypy error

Argument 1 to "test" has incompatible type "Callable[[T], bool]"; expected "Callable[[Union[T, int]], bool]"

at the line that I marked with a comment. I believe this is a bug as this correction makes no sense to me (of couse, I could be wrong).
I'm aware that this code is meaningless, but it's the simplest sample I was able to produce that causes the error.
From the tests that I've done it seems to depend both on the type variable being used and on the function test returning a Tuple[Union[...]]. So if I replace T with a regular type of if I return either Union[T, int] or Tuple[int] instead of Tuple[Union[T, int]] the error goes away. It also disappears by not upacking the the result of test(pred).

To Reproduce

Take the code above and check it with mypy. That's it.

Expected Behavior

No error triggered.

Actual Behavior

Mypy detects the error reported above.

Your Environment

  • Mypy version used: 0.812
  • Mypy command-line flags: --ignore-missing-imports --follow-imports=silent --show-column-numbers
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.9.2
  • Operating system and version: Linux x64
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.