python / python/mypy

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

Open
#9,527 9 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the supplied reproducer with mypy and compare the inferred types for the homogeneous and mixed-signature sequences. Trace the callable and tuple inference paths involved in the loop variables, using the annotated example to narrow the reported assignment behavior. Done means the mixed-signature example type checks as expected without introducing incorrect acceptance of incompatible calls.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.