Title: isinstance + class indirectly deriving from SupportsInt results in "TypeError: Protocols cannot be used with isinstance()" · Issue #297 · python/typing · GitHub
Open Graph Title: isinstance + class indirectly deriving from SupportsInt results in "TypeError: Protocols cannot be used with isinstance()" · Issue #297 · python/typing
X Title: isinstance + class indirectly deriving from SupportsInt results in "TypeError: Protocols cannot be used with isinstance()" · Issue #297 · python/typing
Description: The following assumes mypy 0.4.5 and CPython 3.5.2. Let's have the code: # code3.py class C1: def __int__(self) -> int: return 42 class C2(C1): pass c = C2() print(int(c)) print(isinstance(c, C1)) The code works: % python code3.py 42 Tru...
Open Graph Description: The following assumes mypy 0.4.5 and CPython 3.5.2. Let's have the code: # code3.py class C1: def __int__(self) -> int: return 42 class C2(C1): pass c = C2() print(int(c)) print(isinstance(c, C1)) ...
X Description: The following assumes mypy 0.4.5 and CPython 3.5.2. Let's have the code: # code3.py class C1: def __int__(self) -> int: return 42 class C2(C1): pass c = C2() print(int(c)) print(isinstance(c...
Opengraph URL: https://github.com/python/typing/issues/297
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"isinstance + class indirectly deriving from SupportsInt results in \"TypeError: Protocols cannot be used with isinstance()\"","articleBody":"The following assumes mypy 0.4.5 and CPython 3.5.2.\n\nLet's have the code:\n\n``` python\n# code3.py\nclass C1:\n\n def __int__(self) -> int:\n return 42\n\n\nclass C2(C1):\n pass\n\n\nc = C2()\nprint(int(c))\nprint(isinstance(c, C1))\n```\n\nThe code works:\n\n```\n% python code3.py \n42\nTrue\n```\n\nmypy, however, will complain (as expected):\n\n```\n% mypy code3.py \ncode3.py:12: error: Argument 1 to \"int\" has incompatible type \"C2\"; expected \"Union[SupportsInt, str, bytes]\"\n```\n\nLet's try to make mypy happy:\n\n``` python\n# code3.py\nfrom typing import SupportsInt\n\n\nclass C1(SupportsInt):\n\n def __int__(self) -> int:\n return 42\n\n\nclass C2(C1):\n pass\n\n\nc = C2()\nprint(int(c))\nprint(isinstance(c, C1))\n```\n\nNow mypy is happy but the code fails at runtime:\n\n```\n% python code3.py\n42\nTraceback (most recent call last):\n File \"code3.py\", line 16, in \u003cmodule>\n print(isinstance(c, C1))\n File \"/usr/local/Cellar/python3/3.5.2_2/Frameworks/Python.framework/Versions/3.5/lib/python3.5/typing.py\", line 1269, in __instancecheck__\n raise TypeError(\"Protocols cannot be used with isinstance().\")\nTypeError: Protocols cannot be used with isinstance().\n```\n\nIt seems that I'm put in a situation where I have to choose between static type safety and having the code actually work, am I missing something here? If not - what's the recommended solution to a problem like this?\n","author":{"url":"https://github.com/jstasiak","@type":"Person","name":"jstasiak"},"datePublished":"2016-10-12T17:17:37Z","url":"https://github.com/python/typing/issues/297"}
| 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:27aa8ec7-bb69-1265-1552-e45d0de65ef8 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E82C:1CF166:20C377D:2C85F33:6B002115 |
| html-safe-nonce | fa4bd8acc98e3940fdcb7889e6c7fc786cb9722769d40ae869c36ca8a2202e93 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFODJDOjFDRjE2NjoyMEMzNzdEOjJDODVGMzM6NkIwMDIxMTUiLCJ2aXNpdG9yX2lkIjoiNjk3OTk5MTc2MDAwMDg2MjkzIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | b0f902466fcebd1c0de95bca444c57ae9b757f658851d67865033dccbaf44366 |
| hovercard-subject-tag | issue:182582839 |
| 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/297/issue_layout |
| twitter:image | https://opengraph.githubassets.com/722df08b46e6505f7c518c13b99b93896abb0610af83f6d81fc5acbc08b0628c/python/typing/issues/297 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/722df08b46e6505f7c518c13b99b93896abb0610af83f6d81fc5acbc08b0628c/python/typing/issues/297 |
| og:image:alt | The following assumes mypy 0.4.5 and CPython 3.5.2. Let's have the code: # code3.py class C1: def __int__(self) -> int: return 42 class C2(C1): pass c = C2() print(int(c)) print(isinstance(c, C1)) ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jstasiak |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4eb4cba0d69f045374ac15de9666d22b0961e1d555cc3824f36b7734c00c78e0 |
| 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 | 44c658b24abf00491fb7ae5f56d42c9f38cbeab2 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width