Skip to content

Handle expanding recursive protocol returns - #21787

Closed
Rishav23av wants to merge 1 commit into
python:masterfrom
Rishav23av:fix/21739-protocol-recursion
Closed

Handle expanding recursive protocol returns#21787
Rishav23av wants to merge 1 commit into
python:masterfrom
Rishav23av:fix/21739-protocol-recursion

Conversation

@Rishav23av

Copy link
Copy Markdown

Fixes #21739.

The recursive protocol assumption stack currently closes only when the exact
candidate and protocol instances repeat. That does not happen when a callable
returns a strictly larger instance of its own generic class, so the subtype
check keeps alternating between callable compatibility and protocol
implementation until it raises RecursionError (or segfaults in a compiled
build).

This adds a deliberately narrow cycle closure for the safe case where:

  • the exact protocol instance has already been seen with the same candidate class
  • there is one non-settable, non-overloaded callable member
  • both sides return themselves directly
  • the candidate's non-return signature does not depend on its class type variables

Those conditions keep every non-return obligation unchanged while the candidate
return grows. Broader recursive shapes continue through the existing path.

The tests include the reported expanding-return case and a negative case where
the protocol changes from P[int] to P[object] before reaching a fixed point.
The latter ensures the new shortcut does not skip a changing argument
obligation.

Validation:

  • python runtests.py self lint
  • complete check-protocols.test: 202 passed, 1 skipped
  • focused recursive-protocol group: 8 passed
  • original reduced CLI reproduction: clean

Disclosure: I developed this change with assistance from Claude Code and Codex.
I reviewed the implementation, challenged several unsafe approaches with
counterexamples, and ran the checks listed above.

@github-actions

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@glyph

glyph commented Jul 31, 2026

Copy link
Copy Markdown

I don't think this fixes the issue. I upgraded via pip install 'git+https://github.com/Rishav23av/mypy@fix/21739-protocol-recursion' and then ran mypy --show-traceback ofchangewatcher.py on the same git revision and repo that #21739 suggests. I still get:

  File "/Users/glyph/.virtualenvs/OmniFocusStreamCounter/lib/python3.13/site-packages/mypy/subtypes.py", line 1878, in is_callable_compatible
    unified = unify_generic_callable(left, right, ignore_return=ignore_return)
  File "/Users/glyph/.virtualenvs/OmniFocusStreamCounter/lib/python3.13/site-packages/mypy/subtypes.py", line 2221, in unify_generic_callable
    c = mypy.constraints.infer_constraints(
        type.ret_type, target.ret_type, return_constraint_direction
    )
  File "/Users/glyph/.virtualenvs/OmniFocusStreamCounter/lib/python3.13/site-packages/mypy/constraints.py", line 320, in infer_constraints
    res = _infer_constraints(template, actual, direction, skip_neg_op, erase_types)
  File "/Users/glyph/.virtualenvs/OmniFocusStreamCounter/lib/python3.13/site-packages/mypy/constraints.py", line 432, in _infer_constraints
    return template.accept(ConstraintBuilderVisitor(actual, direction, skip_neg_op, erase_types))
           ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/glyph/.virtualenvs/OmniFocusStreamCounter/lib/python3.13/site-packages/mypy/types.py", line 1684, in accept
    return visitor.visit_instance(self)
           ~~~~~~~~~~~~~~~~~~~~~~^^^^^^
  File "/Users/glyph/.virtualenvs/OmniFocusStreamCounter/lib/python3.13/site-packages/mypy/constraints.py", line 973, in visit_instance
    and mypy.subtypes.is_protocol_implementation(erased, instance, skip=["__call__"])
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/glyph/.virtualenvs/OmniFocusStreamCounter/lib/python3.13/site-packages/mypy/subtypes.py", line 1290, in is_protocol_implementation
    is_compat = is_subtype(
        subtype, supertype, ignore_pos_arg_names=ignore_names, options=options
    )
  File "/Users/glyph/.virtualenvs/OmniFocusStreamCounter/lib/python3.13/site-packages/mypy/subtypes.py", line 190, in is_subtype
    return _is_subtype(left, right, subtype_context, proper_subtype=False)
  File "/Users/glyph/.virtualenvs/OmniFocusStreamCounter/lib/python3.13/site-packages/mypy/subtypes.py", line 377, in _is_subtype
    return left.accept(SubtypeVisitor(orig_right, subtype_context, proper_subtype))
           ~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/glyph/.virtualenvs/OmniFocusStreamCounter/lib/python3.13/site-packages/mypy/types.py", line 2361, in accept
    return visitor.visit_callable_type(self)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
  File "/Users/glyph/.virtualenvs/OmniFocusStreamCounter/lib/python3.13/site-packages/mypy/subtypes.py", line 741, in visit_callable_type
    return is_callable_compatible(
        left,
    ...<8 lines>...
        ),
    )
  File "/Users/glyph/.virtualenvs/OmniFocusStreamCounter/lib/python3.13/site-packages/mypy/subtypes.py", line 1878, in is_callable_compatible
    unified = unify_generic_callable(left, right, ignore_return=ignore_return)
  File "/Users/glyph/.virtualenvs/OmniFocusStreamCounter/lib/python3.13/site-packages/mypy/subtypes.py", line 2221, in unify_generic_callable
    c = mypy.constraints.infer_constraints(
        type.ret_type, target.ret_type, return_constraint_direction
    )
  File "/Users/glyph/.virtualenvs/OmniFocusStreamCounter/lib/python3.13/site-packages/mypy/constraints.py", line 307, in infer_constraints
    if any(
       ~~~^
        get_proper_type(template) == get_proper_type(t)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        and get_proper_type(actual) == get_proper_type(a)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        for (t, a) in reversed(type_state.inferring)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ):
    ^
  File "/Users/glyph/.virtualenvs/OmniFocusStreamCounter/lib/python3.13/site-packages/mypy/constraints.py", line 308, in <genexpr>
    get_proper_type(template) == get_proper_type(t)
  File "/Users/glyph/.virtualenvs/OmniFocusStreamCounter/lib/python3.13/site-packages/mypy/types.py", line 1696, in __eq__
    and self.args == other.args
        ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/glyph/.virtualenvs/OmniFocusStreamCounter/lib/python3.13/site-packages/mypy/types.py", line 1696, in __eq__
    and self.args == other.args
        ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/glyph/.virtualenvs/OmniFocusStreamCounter/lib/python3.13/site-packages/mypy/types.py", line 1696, in __eq__
    and self.args == other.args
        ^^^^^^^^^^^^^^^^^^^^^^^
  [Previous line repeated 262 more times]
  File "/Users/glyph/.virtualenvs/OmniFocusStreamCounter/lib/python3.13/site-packages/mypy/types.py", line 1691, in __eq__
    def __eq__(self, other: object) -> bool:
    
KeyboardInterrupt
Interrupted

@ilevkivskyi

Copy link
Copy Markdown
Member

As per our policy we don't accept LLM generated PRs from new contributors.

@ilevkivskyi ilevkivskyi closed this Aug 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Endless call cycle between is_callable_compatible and is_protocol_implementation

3 participants