✕
Skip to content

Segfault / RecursionError when calling an instance whose class declares __call__ as an attribute of its own type #22361

Open

Description

@Choco-princess

Crash Report

mypy crashes when an instance is called and its class declares __call__ as an attribute annotated with the class itself, a Self union, or C | None. Declaring the class is fine. The crash happens on the call expression. Compiled wheels segfault, and a pure-Python build raises RecursionError.

Possibly related to the closed callable-protocol-with-Self crashes (#17314, #16877), though those used protocols with a __call__ method.

Traceback

On master (pure Python):

case.py:5: error: INTERNAL ERROR -- Please try using mypy master on GitHub:
...
  File ".../mypy/expandtype.py", line 654, in expand_self_type
    return expand_type(typ, {var.info.self_type.id: replacement})
  File ".../mypy/expandtype.py", line 74, in expand_type
    return typ.accept(ExpandTypeVisitor(env, normalize_callables))
RecursionError: maximum recursion depth exceeded

Compiled wheels (2.4.0, 2.4.1) print no traceback and exit with Segmentation fault (exit code 139).

To Reproduce

class C:
    __call__: "C"

def f(x: C) -> None:
    x()

Run: mypy --python-version 3.13 --no-incremental case.py

Also crashes with __call__: "C | None", and with from typing import Self plus __call__: Self | None.

mypy-play: https://mypy-play.net/?gist=e7b53eb68b638455a3bac9a39f9f0a79

Expected Behavior

No crash. Either an error about the call or accepting it would be better than a crash.

Your Environment

  • Mypy version used: 2.4.0, 2.4.1 (compiled, segfault) and master 2.5.0+dev.623bef9 (RecursionError)

  • Mypy command-line flags: --python-version 3.13 --no-incremental

  • Mypy configuration options from mypy.ini (and other config files): none

  • Python version used: 3.13.16

  • Operating system and version: Linux

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions