Title: Support typing.Union/typing.Optional in isinstance check (/support old-style unions) · Issue #17680 · python/mypy · GitHub
Open Graph Title: Support typing.Union/typing.Optional in isinstance check (/support old-style unions) · Issue #17680 · python/mypy
X Title: Support typing.Union/typing.Optional in isinstance check (/support old-style unions) · Issue #17680 · python/mypy
Description: Preamble This issue may already exists but I was unable to find an exact match. There are plenty of issues related to the PEP 604 syntax but none that are really about using typing.Union/typing.Optional/... Support for PEP 604 syntax was...
Open Graph Description: Preamble This issue may already exists but I was unable to find an exact match. There are plenty of issues related to the PEP 604 syntax but none that are really about using typing.Union/typing.Opt...
X Description: Preamble This issue may already exists but I was unable to find an exact match. There are plenty of issues related to the PEP 604 syntax but none that are really about using typing.Union/typing.Opt...
Opengraph URL: https://github.com/python/mypy/issues/17680
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Support typing.Union/typing.Optional in isinstance check (/support old-style unions)","articleBody":"**Preamble**\r\n\r\n- This issue may already exists but I was unable to find an exact match. There are plenty of issues related to the PEP 604 syntax but none that are really about using `typing.Union`/`typing.Optional`/...\r\n- Support for PEP 604 syntax was recently added in #11673 / #17371 \r\n- Old style unions were briefly mentioned in https://github.com/python/mypy/pull/17371#pullrequestreview-2115445793\r\n\r\n**Feature**\r\n\r\nExample code\r\n\r\n```python\r\nfrom typing import Any, Union, Optional\r\n\r\nfoo: Any = None\r\n\r\nif isinstance(foo, int | None):\r\n reveal_type(foo)\r\n\r\nif isinstance(foo, Union[int, None]):\r\n reveal_type(foo)\r\n\r\nif isinstance(foo, Optional[int]):\r\n reveal_type(foo)\r\n\r\nbar = int | None\r\nbaz = Optional[int]\r\nif isinstance(foo, bar):\r\n reveal_type(foo)\r\n\r\nif isinstance(foo, baz):\r\n reveal_type(foo)\r\n```\r\n\r\nOutput for the above with mypy v1.11.1:\r\n```\r\nsrc/union.py:6: note: Revealed type is \"Union[builtins.int, None]\"\r\nsrc/union.py:8: error: Argument 2 to \"isinstance\" has incompatible type \"\u003ctyping special form>\"; expected \"_ClassInfo\" [arg-type]\r\nsrc/union.py:9: note: Revealed type is \"Any\"\r\nsrc/union.py:11: error: Argument 2 to \"isinstance\" has incompatible type \"\u003ctyping special form>\"; expected \"_ClassInfo\" [arg-type]\r\nsrc/union.py:12: note: Revealed type is \"Any\"\r\nsrc/union.py:17: note: Revealed type is \"Union[builtins.int, None]\"\r\nsrc/union.py:19: error: Parameterized generics cannot be used with class or instance checks [misc]\r\nsrc/union.py:19: error: Argument 2 to \"isinstance\" has incompatible type \"\u003ctyping special form>\"; expected \"_ClassInfo\" [arg-type]\r\nsrc/union.py:20: note: Revealed type is \"Any\"\r\nFound 4 errors in 1 file (checked 1 source file)\r\n```\r\n\r\nRunning the above with pyright:\r\n```\r\n.../src/union.py\r\n .../src/union.py:6:17 - information: Type of \"foo\" is \"int | None\"\r\n .../src/union.py:9:17 - information: Type of \"foo\" is \"int | None\"\r\n .../src/union.py:12:17 - information: Type of \"foo\" is \"int | None\"\r\n .../src/union.py:17:17 - information: Type of \"foo\" is \"int | None\"\r\n .../src/union.py:20:17 - information: Type of \"foo\" is \"int | None\"\r\n0 errors, 0 warnings, 5 informations\r\n```\r\n\r\nSummary:\r\n- when using the PEP 604-syntax the type is correctly narrowed to `Union[builtins.int, None]`\r\n- when using the `typing.Union` and/or `typing.Optional` syntax the type is not narrowed and remains `Any` (and errors are shown)\r\n","author":{"url":"https://github.com/bram-tv","@type":"Person","name":"bram-tv"},"datePublished":"2024-08-15T14:17:43Z","url":"https://github.com/python/mypy/issues/17680"}
| 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:e5c08b6e-1e44-d1b6-d098-5c022c4dd152 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8C42:1BCD46:E1E8F0:12FA1AC:6B005825 |
| html-safe-nonce | 5e83ef49825816af35bfd27821aa987b7f398fb6b420c17e77aa1b2e2bef24cd |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4QzQyOjFCQ0Q0NjpFMUU4RjA6MTJGQTFBQzo2QjAwNTgyNSIsInZpc2l0b3JfaWQiOiIzMTQxNTkyMzk0OTY1NzM1NDYxIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 27a894648c48b345088b14ee8468acb8deb579eac50d36bd24ab3e6020cbf676 |
| hovercard-subject-tag | issue:2468140685 |
| 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/17680/issue_layout |
| twitter:image | https://opengraph.githubassets.com/aa257af569dfcafa3cef53f6ec4d34b8bf0519d0e712a71c9fad0fbc35b4922f/python/mypy/issues/17680 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/aa257af569dfcafa3cef53f6ec4d34b8bf0519d0e712a71c9fad0fbc35b4922f/python/mypy/issues/17680 |
| og:image:alt | Preamble This issue may already exists but I was unable to find an exact match. There are plenty of issues related to the PEP 604 syntax but none that are really about using typing.Union/typing.Opt... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | bram-tv |
| hostname | github.com |
| expected-hostname | github.com |
| None | 29be975efff6507a5d135de9c7d634cf7ff8f0c55222a64386ab3202209aaf73 |
| 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 | eacd927ba6112e3b01ba99c762bd699a4a4f5df7 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width