Title: Combining two type checks with `and` deduces `Any` if the first is `Any` · Issue #17100 · python/mypy · GitHub
Open Graph Title: Combining two type checks with `and` deduces `Any` if the first is `Any` · Issue #17100 · python/mypy
X Title: Combining two type checks with `and` deduces `Any` if the first is `Any` · Issue #17100 · python/mypy
Description: Combining two isinstance checks with and where the first one deduces the type to Any ignores the second one. To Reproduce from typing import Any, reveal_type class A: pass class B(A): pass value: Any if isinstance(value, A) and not isins...
Open Graph Description: Combining two isinstance checks with and where the first one deduces the type to Any ignores the second one. To Reproduce from typing import Any, reveal_type class A: pass class B(A): pass value: A...
X Description: Combining two isinstance checks with and where the first one deduces the type to Any ignores the second one. To Reproduce from typing import Any, reveal_type class A: pass class B(A): pass value: A...
Opengraph URL: https://github.com/python/mypy/issues/17100
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Combining two type checks with `and` deduces `Any` if the first is `Any`","articleBody":"Combining two `isinstance` checks with `and` where the first one deduces the type to `Any` ignores the second one.\r\n\r\n**To Reproduce**\r\n\r\n```python\r\nfrom typing import Any, reveal_type\r\n\r\nclass A: pass\r\nclass B(A): pass \r\nvalue: Any\r\n\r\nif isinstance(value, A) and not isinstance(value, B):\r\n reveal_type(value)\r\nif not isinstance(value, B) and isinstance(value, A):\r\n reveal_type(value)\r\n```\r\n\r\nhttps://mypy-play.net/?mypy=latest&python=3.12&gist=643066dcf62c1c2b88bb4714032fa8b5\r\n\r\n**Expected Behavior**\r\n\r\nMypy should deduce the type as `A` in both cases:\r\n\r\n```\r\nmain.py:8: note: Revealed type is \"__main__.A\"\r\nmain.py:10: note: Revealed type is \"__main__.A\"\r\n```\r\n\r\n**Actual Behavior**\r\n\r\n```\r\nmain.py:8: note: Revealed type is \"__main__.A\"\r\nmain.py:10: note: Revealed type is \"Any\"\r\n```\r\n\r\n**Background**\r\n\r\nThis worked before mypy 1.7 and is a regression caused by #16237. The direct reason is that `Any` is now preferred in some circumstances in `and_conditional_maps()` (the `isinstance` in the `if`):\r\n\r\nhttps://github.com/python/mypy/blob/80190101f68b52e960c22572ed6cc814de078b9c/mypy/checker.py#L7609-L7618\r\n\r\nI'm not sure why this is the case or why it is needed to prefer `Any` here. Intuitively, it should be the other way around. The additional `Any` doesn't add any new information but it shouldn't destroy the information already known from other sources.\r\n\r\nWhen I remove the `or isinstance(get_proper_type(m1[n1]), AnyType)` in the above code, only one testcase fails (in `testNarrowingWithAnyOps`) because it explicitly tests for this `Any` result, like this:\r\n\r\n```\r\nclass C: ...\r\nclass D(C): ...\r\ntp: Any\r\n...\r\n\r\nc1: C\r\nif isinstance(c1, tp) and isinstance(c1, D):\r\n reveal_type(c1) # N: Revealed type is \"Any\"\r\n```\r\n\r\nWith the changed code this deduces the type as `D` instead.\r\n\r\n","author":{"url":"https://github.com/sth","@type":"Person","name":"sth"},"datePublished":"2024-04-05T08:48:46Z","url":"https://github.com/python/mypy/issues/17100"}
| 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:eee97037-83aa-d4db-4a3a-d4647b0bc17f |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8876:18E7F5:D594D2:1168E85:6AFFF7E5 |
| html-safe-nonce | 74e4ab1c0a233e3260a4da152c6dd252367b3622d589f7a572d78b72f0192019 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4ODc2OjE4RTdGNTpENTk0RDI6MTE2OEU4NTo2QUZGRjdFNSIsInZpc2l0b3JfaWQiOiIyNDgyMDI0NjA5NjAwMjM5NTg5IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | d96e7c25551ce6fd1b3f43685faeffc096530101943dba5fbfcc976d6c649f74 |
| hovercard-subject-tag | issue:2227418444 |
| 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/17100/issue_layout |
| twitter:image | https://opengraph.githubassets.com/a4fcb9093b9cffb7e7b0ae4f88f2bca459adeb79a25a1c8000b0231b1b5ed4d1/python/mypy/issues/17100 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/a4fcb9093b9cffb7e7b0ae4f88f2bca459adeb79a25a1c8000b0231b1b5ed4d1/python/mypy/issues/17100 |
| og:image:alt | Combining two isinstance checks with and where the first one deduces the type to Any ignores the second one. To Reproduce from typing import Any, reveal_type class A: pass class B(A): pass value: A... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | sth |
| 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