Title: method descriptors aren't properly checked when assinging to a Protocol · Issue #19336 · python/mypy · GitHub
Open Graph Title: method descriptors aren't properly checked when assinging to a Protocol · Issue #19336 · python/mypy
X Title: method descriptors aren't properly checked when assinging to a Protocol · Issue #19336 · python/mypy
Description: Bug Report decorating methods with descriptors loses MyPy when checking against a Protocol. It ends up refusing the assignment because a method isn't Callable[...] but Descriptor I wasn't able this exact described behaviour in previous b...
Open Graph Description: Bug Report decorating methods with descriptors loses MyPy when checking against a Protocol. It ends up refusing the assignment because a method isn't Callable[...] but Descriptor I wasn't able this...
X Description: Bug Report decorating methods with descriptors loses MyPy when checking against a Protocol. It ends up refusing the assignment because a method isn't Callable[...] but Descriptor I wasn't a...
Opengraph URL: https://github.com/python/mypy/issues/19336
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"method descriptors aren't properly checked when assinging to a Protocol","articleBody":"\u003c!--\nIf you're not sure whether what you're experiencing is a mypy bug, please see the \"Question and Help\" form instead.\nPlease consider:\n\n- checking our common issues page: https://mypy.readthedocs.io/en/stable/common_issues.html\n- searching our issue tracker: https://github.com/python/mypy/issues to see if it's already been reported\n- asking on gitter chat: https://gitter.im/python/typing\n-->\n\n**Bug Report**\n\n\u003c!--\nIf you're reporting a problem with a specific library function, the typeshed tracker is better suited for this report: https://github.com/python/typeshed/issues\n\nIf the project you encountered the issue in is open source, please provide a link to the project.\n-->\n\ndecorating methods with descriptors loses MyPy when checking against a Protocol. It ends up refusing the assignment because a method isn't `Callable[...]` but `Descriptor` \n\nI wasn't able this exact described behaviour in previous bug reports. It does seem like mypy correctly assess the type of the described class fields and method in other cases.\n\n**To Reproduce**\n\n```python\nfrom collections.abc import Callable\nfrom typing import Concatenate, Generic, ParamSpec, Protocol, TypeVar\n\nP = ParamSpec(\"P\")\nOwner = TypeVar(\"Owner\")\nReturn = TypeVar(\"Return\")\n\n\nclass Runner(Generic[Owner, P, Return]):\n def __init__(self, owner: Owner, func: Callable[Concatenate[Owner, P], Return]):\n self.func = func\n self.owner = owner\n\n def __call__(self, *args: P.args, **kwargs: P.kwargs) -> Return:\n return self.func(self.owner, *args, **kwargs)\n\n\nclass MyDescriptor(Generic[Owner, P, Return]):\n def __init__(self, func: Callable[Concatenate[Owner, P], Return]):\n self.func = func\n\n def __get__(self, owner: Owner, type: type[Owner]) -> Runner[Owner, P, Return]:\n return Runner(owner, self.func)\n\n\ndef with_descriptor(func: Callable[Concatenate[Owner, P], Return]) -> MyDescriptor[Owner, P, Return]:\n return MyDescriptor(func)\n\n\nclass MyProtocol(Protocol):\n def my_method(self, i: int) -> str: ...\n\n\nclass MyClass:\n @with_descriptor\n def my_method(self, i: int) -> str:\n return f\"hello {i}\"\n\n\nconcrete_instance = MyClass()\n\nreveal_type(concrete_instance.my_method) # W: Type of \"concrete_instance.my_method\" is \"Runner[MyClass, (i: int), str]\"\nas_callable: Callable[[int], str] = concrete_instance.my_method # successful type checking\n\nmy_instance: MyProtocol = concrete_instance # type checking fails\nreveal_type(my_instance.my_method) # W: Type of \"my_instance.my_method\" is \"(i: int) -> str\"\n\n\nassert my_instance.my_method(8) == \"hello 8\"\n```\n[mypy playground](https://mypy-play.net/?mypy=1.16.1&python=3.12&gist=50b23b1b6948a46a5eb634646f09d1b3)\n\n**Expected Behavior**\n\n\u003c!--\nHow did you expect mypy to behave? It’s fine if you’re not sure your understanding is correct.\nWrite down what you thought would happen. If you expected no errors, delete this section.\n-->\nmypy should detect that the `__get__` method from the descriptor returns a compatible type for `Callable` (it has a compatible `__call__` method) and allow the assignment `my_instance: MyProtocol = MyClass()`\n\n**Actual Behavior**\n\nmypy correctly detects that the `concrete_instance.my_method` type is the one from the return of the `__get__` method from the descriptor. It also correctly asses that the type is compatible with `Callable[[int], str]` like the method of the protocol. \nHowever, it refuses to verify that this method is indeed compatible with the protocol's method when trying to assign the whole instance to a typed variable.\n\n\u003c!-- What went wrong? Paste mypy's output. -->\n\n```\nreproduce.py:42: note: Revealed type is \"reproduce.Runner[reproduce.MyClass, [i: builtins.int], builtins.str]\"\nreproduce.py:45: error: Incompatible types in assignment (expression has type \"MyClass\", variable has type \"MyProtocol\") [assignment]\nreproduce.py:45: note: Following member(s) of \"MyClass\" have conflicts:\nreproduce.py:45: note: my_method: expected \"Callable[[int], str]\", got \"MyDescriptor[MyClass, [int], str]\"\nreproduce.py:46: note: Revealed type is \"def (i: builtins.int) -> builtins.str\"\nFound 1 error in 1 file (checked 1 source file)\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: 1.16.1 (compiled: yes)\n- Mypy command-line flags: none.\n- Mypy configuration options from `mypy.ini` (and other config files): `check_untyped_def=True`\n- Python version used: 3.12.3\n\n\u003c!-- You can freely edit this text, please remove all the lines you believe are unnecessary. -->\n","author":{"url":"https://github.com/Floby","@type":"Person","name":"Floby"},"datePublished":"2025-06-24T19:09:05Z","url":"https://github.com/python/mypy/issues/19336"}
| 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:e7412322-947e-561a-5eec-11ff6d6c848b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A734:3543F6:1006A91:1502BAF:6AFFF947 |
| html-safe-nonce | 7941c2d869006b30854de5f0b92e797ced2fa711ad622ef6e08e2d3165e7ccaf |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNzM0OjM1NDNGNjoxMDA2QTkxOjE1MDJCQUY6NkFGRkY5NDciLCJ2aXNpdG9yX2lkIjoiODA1MTQzMzI0Nzc4NTA4OTM1MSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 5db1edb38ac91033b836534a9a025fcaf50713b2a6da6443b9679f5979557046 |
| hovercard-subject-tag | issue:3173008184 |
| 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/19336/issue_layout |
| twitter:image | https://opengraph.githubassets.com/e38ca107a911dff3d915ef006a976f67a7c3c4e1894feb2d685c43be1ea156f0/python/mypy/issues/19336 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/e38ca107a911dff3d915ef006a976f67a7c3c4e1894feb2d685c43be1ea156f0/python/mypy/issues/19336 |
| og:image:alt | Bug Report decorating methods with descriptors loses MyPy when checking against a Protocol. It ends up refusing the assignment because a method isn't Callable[...] but Descriptor I wasn't able this... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Floby |
| hostname | github.com |
| expected-hostname | github.com |
| None | 0a0d8686a27478df474dd7415776a7a080cde8838b3cd92ea7601187572e5729 |
| 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 | 16a11879678aaf97e2a42c33ee70830aa084fa37 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width