python / python/mypy

(🐞) incompatible `Callable`s are incorrectly joined into `builtins.function`

Open
#17,478 0 comments 4 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

from typing import Callable, _T as T
from types import FunctionType

class C:
    def __init__(self, i: int=1): ...

def join(t1: T, t2: T) -> T: ...

c1: Callable[[], object] = C
c2: Callable[[int], object] = C
result = join(c1, c2)
reveal_type(result)  # Revealed type is "builtins.function"
print(isinstance(result, FunctionType)  # False

this is not an instance of builtins.function, it is a type[C]. it should be joined into a union, or something else, either object or Callable[..., object] or something

the same defect is observed with a non-function callable instance:

class C:
    def __call__(self, i: int = 1): ...
  • reraised from #17017 as it wasn't resolved

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 mypy reproducer and compare the revealed type with the runtime value. Trace the callable-joining and type-inference path responsible for producing builtins.function, then establish the intended result for incompatible callable types and add coverage for both the class and callable-instance examples.

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
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.