Skip to content

Crash on a class whose __call__ attribute is its own type, passed where a Callable is expected #22398

Closed as duplicate

Description

@chrikrah

Crash Report

The class from #22361 also crashes mypy when it is passed where a Callable is expected. #22362 fixes the call in check_call and leaves this path: visit_instance looks up __call__ at mypy/subtypes.py:659, gets C back, and checks C against the Callable again at line 661. x: Callable[[], int] = C() crashes the same way.

Traceback

case.py:8: error: INTERNAL ERROR -- Please try using mypy master on GitHub:
https://mypy.readthedocs.io/en/stable/common_issues.html#using-a-development-mypy-build
Please report a bug at https://github.com/python/mypy/issues
version: 2.5.0+dev
...
  File "mypy/checkexpr.py", line 2912, in check_arg
    elif not is_subtype(caller_type, callee_type, options=self.chk.options):
  File "mypy/subtypes.py", line 196, in is_subtype
  File "mypy/subtypes.py", line 383, in _is_subtype
  File "mypy/types.py", line 1719, in accept
  File "mypy/subtypes.py", line 661, in visit_instance
    return self._is_subtype(call, right)
  File "mypy/subtypes.py", line 455, in _is_subtype
  File "mypy/subtypes.py", line 196, in is_subtype
  ... (the five frames above repeat)
  File "mypy/expandtype.py", line 192, in __init__
    super().__init__()
RecursionError: maximum recursion depth exceeded

To Reproduce

from typing import Callable

class C:
    __call__: "C"

def h(cb: Callable[[], int]) -> None: ...

h(C())
$ mypy --show-traceback case.py      # master 5dc2840, pure Python
(the traceback above), exit 2
$ mypy case.py                        # mypy 2.4.0, compiled wheel
exit 139, no output

With def __call__(self) -> "C": ... in place of the attribute, mypy reports Argument 1 to "h" has incompatible type "C"; expected "Callable[[], int]" and does not crash. I'd expect the attribute form to give that error too.

Your Environment

  • Mypy version used: master 5dc2840 (2.5.0+dev), and 2.4.0 compiled
  • Mypy command-line flags: --show-traceback
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.12.12
  • Operating system and version: macOS 26.7.2

Not checked: the C | None and Self variants on this path.

I'd keep #22362 as it is and guard visit_instance in its own change, since this loop never reaches check_call. Would a PR for that be welcome?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions