Title: Relaxing usage of `type` with `typing.Protocol` in typing spec · Issue #2306 · python/typing · GitHub
Open Graph Title: Relaxing usage of `type` with `typing.Protocol` in typing spec · Issue #2306 · python/typing
X Title: Relaxing usage of `type` with `typing.Protocol` in typing spec · Issue #2306 · python/typing
Description: Some time ago I opened a thread on the Python discourse about implementing an AbstractType semantic that would allow functions to receive non-instantiable types so to try and conform a bit the situation presented in the the thread. Consi...
Open Graph Description: Some time ago I opened a thread on the Python discourse about implementing an AbstractType semantic that would allow functions to receive non-instantiable types so to try and conform a bit the situ...
X Description: Some time ago I opened a thread on the Python discourse about implementing an AbstractType semantic that would allow functions to receive non-instantiable types so to try and conform a bit the situ...
Opengraph URL: https://github.com/python/typing/issues/2306
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Relaxing usage of `type` with `typing.Protocol` in typing spec","articleBody":"Some time ago I opened [a thread](https://discuss.python.org/t/abstracttype-take-2-or-allowing-typeform-to-be-used-in-isinstance/107093) on the Python discourse about implementing an `AbstractType` semantic that would allow functions to receive non-instantiable types so to try and conform a bit the situation presented in the the thread.\n\nConsider the snippet:\n\n```py\nfrom abc import abstractmethod\nfrom typing import runtime_checkable, Protocol, TypeVar, TypeGuard\n\n@runtime_checkable\nclass MyProtocol(Protocol):\n @abstractmethod\n def my_method(self) -> str:\n ...\n\nclass MyClass:\n def my_method(self) -> str:\n return \"Hello from MyClass!\"\n \nclass MyOtherClass:\n def another_method(self) -> str:\n return \"Hello from MyOtherClass!\"\n \n\nP = TypeVar('P', bound=MyProtocol)\n \ndef check_protocol(obj: object, protocol: type[P]) -> TypeGuard[P]:\n return isinstance(obj, protocol)\n\n# Example usage\nmy_obj = MyClass()\nmy_other_obj = MyOtherClass()\nif check_protocol(my_obj, MyProtocol):\n print(my_obj.my_method())\nif check_protocol(my_other_obj, MyProtocol):\n print(my_other_obj.my_method())\nelse:\n print(\"Object does not implement MyProtocol.\")\n```\n\nOutput:\n\n```\nHello from MyClass!\nmy_obj does not implement MyOtherClass protocol.\n```\n\nAt runtime, this works as intended. At type checking time, `mypy` complains that `check_protocol` cannot accept a non-instantiable type, while `pyright` and `ty` are accepting this behavior.\n\nAccording to the typing specification, `mypy` is technically correct (the best kind of correct), but this code is perfectly acceptable. There are other examples referenced in python/mypy#4717 (which is the 3rd most upvoted issue by 👍 by the way).\n\nI was hoping for more discussion points presented in the thread but not many replied. I didn't want to monopolize it, so I'm trying my luck here.\n\n@JelleZijlstra (sorry for the ping) did point out that it might be worthwhile to change the spec to allow this behavior but I'm guessing there should be some discussion on how to deal with it first. Jelle suggested: \"If you want it to be instantiable, use `Callable[]` instead.\"\n\nI do agree it makes sense, but this snippet:\n\n```py\nfrom typing import Protocol\n\nclass MyProtocol(Protocol):\n def method(self) -> str:\n ...\nprint(callable(MyProtocol)) # prints True\n```\n\nconfuses me.\n\nSo my point is: could the spec allow for `type` to accept non-instantiable types, since it seems to be a pretty common pattern? Is there a way to effectively deal with the separation between instantiable and non-instantiable types?\n\nI tried adding the \"typing spec\" label but couldn't. At any rate, thank you for the consideration.","author":{"url":"https://github.com/jacopoabramo","@type":"Person","name":"jacopoabramo"},"datePublished":"2026-06-18T09:10:16Z","url":"https://github.com/python/typing/issues/2306"}
| 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:bf7dd689-0278-ba96-8706-4006f70061f1 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 853E:2EC689:69508A:8DCBDA:6AFF8C72 |
| html-safe-nonce | e17833a51c9c15dfc3db8b52a1324c3d2fb5f63f2d0567901121f5ddeb59655a |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4NTNFOjJFQzY4OTo2OTUwOEE6OERDQkRBOjZBRkY4QzcyIiwidmlzaXRvcl9pZCI6IjczOTg2NTI0NDcwNTAxNDA3ODYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | c214829187ce76a96e5dba8218551f7ac5b2bd502b02486904808e532ae58fc5 |
| hovercard-subject-tag | issue:4690895431 |
| 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/typing/2306/issue_layout |
| twitter:image | https://opengraph.githubassets.com/eb35a51af6a8abeade074468db3528c7ab128b89bbd9c14b32fdad82527d37ce/python/typing/issues/2306 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/eb35a51af6a8abeade074468db3528c7ab128b89bbd9c14b32fdad82527d37ce/python/typing/issues/2306 |
| og:image:alt | Some time ago I opened a thread on the Python discourse about implementing an AbstractType semantic that would allow functions to receive non-instantiable types so to try and conform a bit the situ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jacopoabramo |
| hostname | github.com |
| expected-hostname | github.com |
| None | 587ad99a3e443b2e1cb0d3df059ec49ab54389861f13310766dfdeb615fed90d |
| turbo-cache-control | no-preview |
| go-import | github.com/python/typing git https://github.com/python/typing.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 24604008 |
| octolytics-dimension-repository_nwo | python/typing |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 24604008 |
| octolytics-dimension-repository_network_root_nwo | python/typing |
| 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 | 5bb0fb68d64881d1344f5863d809515d496801e5 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width