Title: Strange interaction between `typing.Protocol` and unrelated `isinstance()` checks · Issue #105144 · python/cpython · GitHub
Open Graph Title: Strange interaction between `typing.Protocol` and unrelated `isinstance()` checks · Issue #105144 · python/cpython
X Title: Strange interaction between `typing.Protocol` and unrelated `isinstance()` checks · Issue #105144 · python/cpython
Description: On main, an isinstance() check against Sized works just the same as it does on Python 3.11: >>> from typing import Sized >>> isinstance(1, Sized) False However! If you first subclass Sized like this, TypeError is raised on that isinstanc...
Open Graph Description: On main, an isinstance() check against Sized works just the same as it does on Python 3.11: >>> from typing import Sized >>> isinstance(1, Sized) False However! If you first subclass Sized like thi...
X Description: On main, an isinstance() check against Sized works just the same as it does on Python 3.11: >>> from typing import Sized >>> isinstance(1, Sized) False However! If you first subcl...
Opengraph URL: https://github.com/python/cpython/issues/105144
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Strange interaction between `typing.Protocol` and unrelated `isinstance()` checks","articleBody":"On `main`, an `isinstance()` check against `Sized` works just the same as it does on Python 3.11:\r\n\r\n```pycon\r\n\u003e\u003e\u003e from typing import Sized\r\n\u003e\u003e\u003e isinstance(1, Sized)\r\nFalse\r\n```\r\n\r\nHowever! If you first subclass `Sized` like this, `TypeError` is raised on that `isinstance()` check!\r\n\r\n```pycon\r\n\u003e\u003e\u003e from typing import Sized, Protocol\r\n\u003e\u003e\u003e class Foo(Sized, Protocol): pass\r\n...\r\n\u003e\u003e\u003e isinstance(1, Sized)\r\nTraceback (most recent call last):\r\n File \"\u003cstdin\u003e\", line 1, in \u003cmodule\u003e\r\n File \"C:\\Users\\alexw\\coding\\cpython\\Lib\\typing.py\", line 1153, in __instancecheck__\r\n return self.__subclasscheck__(type(obj))\r\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\r\n File \"C:\\Users\\alexw\\coding\\cpython\\Lib\\typing.py\", line 1428, in __subclasscheck__\r\n return issubclass(cls, self.__origin__)\r\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\r\n File \"\u003cfrozen abc\u003e\", line 123, in __subclasscheck__\r\n File \"C:\\Users\\alexw\\coding\\cpython\\Lib\\typing.py\", line 1793, in __subclasscheck__\r\n return super().__subclasscheck__(other)\r\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\r\n File \"\u003cfrozen abc\u003e\", line 123, in __subclasscheck__\r\n File \"C:\\Users\\alexw\\coding\\cpython\\Lib\\typing.py\", line 1876, in _proto_hook\r\n raise TypeError(\"Instance and class checks can only be used with\"\r\nTypeError: Instance and class checks can only be used with @runtime_checkable protocols\r\n```\r\n\r\nThis was originally reported by @vnmabus in https://github.com/python/typing_extensions/issues/207.\r\n\r\nNote that (because of the `abc`-module cache), this _doesn't_ reproduce if you do an `isinstance()` check _before_ subclassing `typing.Sized`:\r\n\r\n```pycon\r\n\u003e\u003e\u003e import typing\r\n\u003e\u003e\u003e isinstance(1, typing.Sized)\r\nFalse\r\n\u003e\u003e\u003e class Foo(typing.Sized, typing.Protocol): pass\r\n...\r\n\u003e\u003e\u003e isinstance(1, typing.Sized)\r\nFalse\r\n```\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-105152\n* gh-105159\n* gh-105160\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/AlexWaygood","@type":"Person","name":"AlexWaygood"},"datePublished":"2023-05-31T14:30:55.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":4},"url":"https://github.com/105144/cpython/issues/105144"}
| 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:86bc7b32-40bf-6cbf-ac75-31c9ceb635c8 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8AAE:2B0240:7CBB5:A56CA:6969CA7A |
| html-safe-nonce | e52936955ddea094cbc6d48273f52abecff1afb66a7694a8d23f121b095a4906 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4QUFFOjJCMDI0MDo3Q0JCNTpBNTZDQTo2OTY5Q0E3QSIsInZpc2l0b3JfaWQiOiI3MjA3MzczNzA3Njc0MjQxNjU4IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 90c447122f29acbce49e6a8d96427b261df110176d5292362960fe37c3512eef |
| hovercard-subject-tag | issue:1734369501 |
| 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/cpython/105144/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ea447773547ce534228aff91b28e208ef2b6a1224cdb6461b6b41932da4b2265/python/cpython/issues/105144 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ea447773547ce534228aff91b28e208ef2b6a1224cdb6461b6b41932da4b2265/python/cpython/issues/105144 |
| og:image:alt | On main, an isinstance() check against Sized works just the same as it does on Python 3.11: >>> from typing import Sized >>> isinstance(1, Sized) False However! If you first subclass Sized like thi... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | AlexWaygood |
| hostname | github.com |
| expected-hostname | github.com |
| None | acedec8b5f975d9e3d494ddd8f949b0b8a0de59d393901e26f73df9dcba80056 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| 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 | 83c08c21cdda978090dc44364b71aa5bc6dcea79 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width