Title: Endless call cycle between is_callable_compatible and is_protocol_implementation · Issue #21739 · python/mypy · GitHub
Open Graph Title: Endless call cycle between is_callable_compatible and is_protocol_implementation · Issue #21739 · python/mypy
X Title: Endless call cycle between is_callable_compatible and is_protocol_implementation · Issue #21739 · python/mypy
Description: Discovered by @glyph and I see it's not his first rodeo (#18195 for instance). My best interpretation (which could be wrong) is that a generic class which references itself in a method's type annotation, when typed using a protocol, will...
Open Graph Description: Discovered by @glyph and I see it's not his first rodeo (#18195 for instance). My best interpretation (which could be wrong) is that a generic class which references itself in a method's type annot...
X Description: Discovered by @glyph and I see it's not his first rodeo (#18195 for instance). My best interpretation (which could be wrong) is that a generic class which references itself in a method's ty...
Opengraph URL: https://github.com/python/mypy/issues/21739
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Endless call cycle between is_callable_compatible and is_protocol_implementation","articleBody":"Discovered by @glyph and I see it's not his first rodeo (https://github.com/python/mypy/issues/18195 for instance).\n\nMy best interpretation (which could be wrong) is that a generic class which references itself in a method's type annotation, when typed using a protocol, will cause mypy to loop endlessly between matching the protocol and checking the types in the class. I haven't pinpointed the exact mechanism but it kind of makes sense that this could happen.\n\n**To Reproduce**\n\nThe original code is here: https://github.com/glyph/OmniFocusStreamCounter/commit/454d0727363595b770cb150cc08a0018bc04a783\n\nBut this is about as far as I could reduce it:\n\n```python\nfrom dataclasses import dataclass\nfrom typing import Any, Callable, Protocol\n\n\nclass PExp[T](Protocol):\n def EQ(self, other: T) -> PExp[T]: ...\n\n\nclass PRef[T](Protocol):\n def EQ(self, other: T) -> PExp[T]: ...\n\n\n@dataclass\nclass Exp[A, B]:\n _left: A\n _op: str\n _right: B\n\n def EQ(self, other: Any) -> Exp[Exp[A, B], Any]:\n return Exp(self, \"EQ\", other)\n\n\n@dataclass\nclass Ref[T]:\n get: Callable[[], T]\n\n def EQ(self, other: T) -> Exp[Ref[T], T]:\n return Exp(self, \"EQ\", other)\n\n\n@dataclass\nclass Cache:\n def spam(self, name: str) -> PRef[str]:\n return Ref(lambda: \"spam\")\n\n```\n\n**Expected Behavior**\n\nSo, if you make `Exp.EQ` untyped, then mypy will succeed (I fixed some problems in the original code 😉 ) This is obviously the expected behaviour.\n\n**Actual Behavior**\n\nRunning mypy from HEAD, uncompiled, with --show-traceback - it hangs, then on Ctrl-C we see, this, endlessly:\n\n```\n File \"/home/dhd/work/mypy/mypy/subtypes.py\", line 1281, in is_protocol_implementation 07:44:34 [233/1846]\n is_compat = is_subtype( \n subtype, supertype, ignore_pos_arg_names=ignore_names, options=options \n ) \n File \"/home/dhd/work/mypy/mypy/subtypes.py\", line 189, in is_subtype \n return _is_subtype(left, right, subtype_context, proper_subtype=False) \n File \"/home/dhd/work/mypy/mypy/subtypes.py\", line 376, in _is_subtype \n return left.accept(SubtypeVisitor(orig_right, subtype_context, proper_subtype))\n ~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n File \"/home/dhd/work/mypy/mypy/types.py\", line 2361, in accept\n return visitor.visit_callable_type(self)\n ~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^\n File \"/home/dhd/work/mypy/mypy/subtypes.py\", line 740, in visit_callable_type\n return is_callable_compatible(\n left,\n ...\u003c8 lines>...\n ),\n )\n File \"/home/dhd/work/mypy/mypy/subtypes.py\", line 1785, in is_callable_compatible\n if not ignore_return and not is_compat_return(left.ret_type, right.ret_type):\n ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n File \"/home/dhd/work/mypy/mypy/subtypes.py\", line 448, in _is_subtype\n return is_subtype(left, right, subtype_context=self.subtype_context)\n File \"/home/dhd/work/mypy/mypy/subtypes.py\", line 189, in is_subtype\n return _is_subtype(left, right, subtype_context, proper_subtype=False)\n File \"/home/dhd/work/mypy/mypy/subtypes.py\", line 376, in _is_subtype\n return left.accept(SubtypeVisitor(orig_right, subtype_context, proper_subtype))\n ~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n File \"/home/dhd/work/mypy/mypy/types.py\", line 1684, in accept\n return visitor.visit_instance(self)\n ~~~~~~~~~~~~~~~~~~~~~~^^^^^^\n File \"/home/dhd/work/mypy/mypy/subtypes.py\", line 627, in visit_instance\n if right.type.is_protocol and is_protocol_implementation(\n ~~~~~~~~~~~~~~~~~~~~~~~~~~^\n left, right, proper_subtype=self.proper_subtype, options=self.options\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n ):\n ^\n```\n\n**Your Environment**\n\n\u003c!-- Include as many relevant details about the environment you experienced the bug in -->\n\n- Mypy version used: mypy 2.4.0+dev.67eecf07fea3fa4bbaab9d46cd28372a8ded330b (compiled: no)\n- Mypy command-line flags: --show-traceback -v\n- Mypy configuration options from `mypy.ini` (and other config files):\n```toml\n[tool.mypy]\nnamespace_packages = true\nplugins = [\"mypy_zope:plugin\"]\n\n[[tool.mypy.overrides]]\nignore_missing_imports = true\nmodule = [\n \"objc\",\n \"Foundation\",\n \"AppKit\",\n \"ServiceManagement\",\n \"setuptools\",\n \"appscript\",\n]\n```\n- Python version used: 3.14\n \n\u003c!-- You can freely edit this text, please remove all the lines you believe are unnecessary. -->\n","author":{"url":"https://github.com/dhdaines","@type":"Person","name":"dhdaines"},"datePublished":"2026-07-16T11:47:02Z","url":"https://github.com/python/mypy/issues/21739"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:3d3913c8-0abc-b2d7-5b24-dda1444c76a1 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | EC5E:2463C6:2E5F1E:3E8475:6B0026AA |
| html-safe-nonce | 1e29fa9172003289d2d40a6de39ac7e544892f8c6dbee97f4ecbbf5631cccf3c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFQzVFOjI0NjNDNjoyRTVGMUU6M0U4NDc1OjZCMDAyNkFBIiwidmlzaXRvcl9pZCI6IjU2ODQyNjkzNzEyODM3NDQ0MjciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 47d161033ed4e029ed88c43d029a93b563e76d094781ef4e56535157cdbc9ebb |
| hovercard-subject-tag | issue:4901668010 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/mypy/21739/issue_layout |
| twitter:image | https://opengraph.githubassets.com/5df4652f55eddc75f9521e1ba1c87157abe21dd6c68888e4a686175a4b561662/python/mypy/issues/21739 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/5df4652f55eddc75f9521e1ba1c87157abe21dd6c68888e4a686175a4b561662/python/mypy/issues/21739 |
| og:image:alt | Discovered by @glyph and I see it's not his first rodeo (#18195 for instance). My best interpretation (which could be wrong) is that a generic class which references itself in a method's type annot... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | dhdaines |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4eb4cba0d69f045374ac15de9666d22b0961e1d555cc3824f36b7734c00c78e0 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/mypy git https://github.com/python/mypy.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 7053637 |
| octolytics-dimension-repository_nwo | python/mypy |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 7053637 |
| octolytics-dimension-repository_network_root_nwo | python/mypy |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 44c658b24abf00491fb7ae5f56d42c9f38cbeab2 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width