python / python/mypy

'Cannot call function of unknown type' for sequence of callables with different signatures

Abierto
#9,527 9 comentarios 2 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

bug
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 working with sequences of callables, if all callables in the sequence do not have the same signature mypy will raise false positives when trying to access and call the callables.

To Reproduce

def identity(x: int) -> int:
    return x

def double(x: int) -> int:
    return x * 2

def add(x: int, y: int) -> int:
    return x + y

calls_same_signatures = (
    (identity, (1,)),
    (double, (2,)),
)

calls_different_signatures = (
    (identity, (1,)),
    (add, (1, 2)),
)

for f1, args1 in calls_same_signatures:
    f1(*args1) # type checks

for f2, args2 in calls_different_signatures:
    f2(*args2) # mypy says: Cannot call function of unknown type

Trying to fix this with annotations results in what may be a more revealing error?

from typing import Callable, Sequence, Tuple

def identity(x: int) -> int:
    return x

def double(x: int) -> int:
    return x * 2

def add(x: int, y: int) -> int:
    return x + y

calls_same_signatures: Sequence[Tuple[Callable[..., int], Tuple[int, ...]]]
calls_same_signatures = (
    (identity, (1,)),
    (double, (2,)),
)

calls_different_signatures: Sequence[Tuple[Callable[..., int], Tuple[int, ...]]]
calls_different_signatures = (
    (identity, (1,)),
    (add, (1, 2)),
)

f1: Callable[..., int]
for f1, args1 in calls_same_signatures: # type checks
    f1(*args1)

f2: Callable[..., int]
for f2, args2 in calls_different_signatures:  # mypy says: Incompatible types in assignment (expression has type "function", variable has type "Callable[..., int]")
    f2(*args2)

Expected Behavior

I'd expect this to type check.
I'm not sure if it might be a contravariant vs. covariant thing? That is, does this issue stem from the question over whether the function is a Callable[[int], int] or a Callable[..., int] when it comes out of the sequence?

Actual Behavior

Mypy raises an error when attempting to call functions in calls_different_signatures,
Cannot call function of unknown type in the first example, Incompatible types in assignment (expression has type "function", variable has type "Callable[..., int]") in the second.

Your Environment

  • Mypy version used: 0.782
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.6.5
  • Operating system and version: OS X 10.15.7

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 el reproductor proporcionado con mypy y compara los tipos inferidos para las secuencias de firma homogénea y de firmas mixtas. Sigue las rutas de inferencia de callables y tuplas involucradas en las variables de bucle, usando el ejemplo anotado para acotar el comportamiento de asignación informado. Se considera terminado cuando el ejemplo de firmas mixtas pasa la comprobación de tipos como se espera sin introducir una aceptación incorrecta de llamadas incompatibles.

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

Evaluación

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

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.