'Cannot call function of unknown type' for sequence of callables with different signatures
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
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
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia eseguendo il riproduttore fornito con mypy e confronta i tipi inferiti per le sequenze con firma omogenea e con firme miste. Traccia i percorsi di inferenza di callable e tuple coinvolti nelle variabili del ciclo, usando l’esempio annotato per circoscrivere il comportamento di assegnazione segnalato. Il lavoro è completato quando l’esempio con firme miste supera il controllo dei tipi come previsto senza introdurre un’accettazione errata di chiamate incompatibili.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- devtools
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100