Title: assert isinstance check for a non-subtype confuses mypy · Issue #2776 · python/mypy · GitHub
Open Graph Title: assert isinstance check for a non-subtype confuses mypy · Issue #2776 · python/mypy
X Title: assert isinstance check for a non-subtype confuses mypy · Issue #2776 · python/mypy
Description: Consider a coding pattern where one chooses (for some reason) to type a function as accepting a broad argument type, but in the body to assert isinstance a narrower type, like this: class A(object): pass class B(A): pass def foo(x): # ty...
Open Graph Description: Consider a coding pattern where one chooses (for some reason) to type a function as accepting a broad argument type, but in the body to assert isinstance a narrower type, like this: class A(object)...
X Description: Consider a coding pattern where one chooses (for some reason) to type a function as accepting a broad argument type, but in the body to assert isinstance a narrower type, like this: class A(object)...
Opengraph URL: https://github.com/python/mypy/issues/2776
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"assert isinstance check for a non-subtype confuses mypy","articleBody":"Consider a coding pattern where one chooses (for some reason) to type a function as accepting a broad argument type, but in the body to `assert isinstance` a narrower type, like this:\r\n```\r\nclass A(object): pass\r\n\r\nclass B(A): pass\r\n\r\ndef foo(x):\r\n # type: (A) -> None\r\n reveal_type(x)\r\n assert isinstance(x, B)\r\n reveal_type(x)\r\n```\r\n\r\nMypy recognizes the assert and narrows the type of `x` for the second `reveal_type`, as expected:\r\n```\r\n$ mypy --python-version 2.7 mypytest.py\r\nmypytest.py:7: error: Revealed type is 'mypytest.A'\r\nmypytest.py:9: error: Revealed type is 'mypytest.B'\r\n```\r\n\r\nBut, suppose one makes a mistake, and either uses the wrong class, or forgets to inherit from `A`, like this:\r\n```\r\nclass A(object): pass\r\n\r\nclass C(object): pass\r\n\r\ndef bar(x):\r\n # type: (A) -> None\r\n reveal_type(x)\r\n assert isinstance(x, C)\r\n reveal_type(x)\r\n```\r\n\r\nNow, mypy appears to just ignore the assert and the second `reveal_type`:\r\n```\r\n$ mypy --python-version 2.7 mypytest2.py\r\nmypytest2.py:7: error: Revealed type is 'mypytest2.A'\r\n```\r\n\r\nI recognize that multiple inheritance in Python means that mypy would need intersection types in order to type `x` precisely in the second example, but I am very surprised that it just completely ignores the `reveal_type`.","author":{"url":"https://github.com/axch","@type":"Person","name":"axch"},"datePublished":"2017-01-30T23:44:07Z","url":"https://github.com/python/mypy/issues/2776"}
| 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:b09d13bc-a16d-bf61-f596-3a21c7168782 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A126:DCDA8:30B8930:4049C0E:6B0029C4 |
| html-safe-nonce | 1eee40a477a55ced642a85cdf893d658efdeb91e35d930dce93cb7c87fa735bd |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBMTI2OkRDREE4OjMwQjg5MzA6NDA0OUMwRTo2QjAwMjlDNCIsInZpc2l0b3JfaWQiOiI2MTQwNzQxMDk4MjUyODA2NTk2IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 41758cb92b87fb7b9dcb979c0b7f1b6b244d13a9e2407eb9a43ea2424bbdf135 |
| hovercard-subject-tag | issue:204164420 |
| 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/2776/issue_layout |
| twitter:image | https://opengraph.githubassets.com/db9d2b8df78edaa81a0c4e2433479e05cb0e6dbb62688e051c7d8c0e5122fa64/python/mypy/issues/2776 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/db9d2b8df78edaa81a0c4e2433479e05cb0e6dbb62688e051c7d8c0e5122fa64/python/mypy/issues/2776 |
| og:image:alt | Consider a coding pattern where one chooses (for some reason) to type a function as accepting a broad argument type, but in the body to assert isinstance a narrower type, like this: class A(object)... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | axch |
| hostname | github.com |
| expected-hostname | github.com |
| None | 7d0829bbde9e48b2486cb5dd6d147896eb01d32134627091f818958a935ef3f0 |
| 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 | f3f3a43ca095cdea3abbb394112e971f9fdb94fa |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width