Title: False-positive errors when using `type[T]` (or `Type[T]`) in a PEP 604 type alias · Issue #12392 · python/mypy · GitHub
Open Graph Title: False-positive errors when using `type[T]` (or `Type[T]`) in a PEP 604 type alias · Issue #12392 · python/mypy
X Title: False-positive errors when using `type[T]` (or `Type[T]`) in a PEP 604 type alias · Issue #12392 · python/mypy
Description: Bug Report The following two snippets (one using builtins.type, the other using typing.Type) both work fine at runtime on 3.10+. They should be accepted as valid type alias definitions on Python 3.10+ (and should be accepted in stub file...
Open Graph Description: Bug Report The following two snippets (one using builtins.type, the other using typing.Type) both work fine at runtime on 3.10+. They should be accepted as valid type alias definitions on Python 3....
X Description: Bug Report The following two snippets (one using builtins.type, the other using typing.Type) both work fine at runtime on 3.10+. They should be accepted as valid type alias definitions on Python 3....
Opengraph URL: https://github.com/python/mypy/issues/12392
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"False-positive errors when using `type[T]` (or `Type[T]`) in a PEP 604 type alias","articleBody":"**Bug Report**\r\n\r\nThe following two snippets (one using `builtins.type`, the other using `typing.Type`) both work fine at runtime on 3.10+. They should be accepted as valid type alias definitions on Python 3.10+ (and should be accepted in stub files regardless of Python version). However, mypy raises false-positive errors for both. The errors do not go away if you use `typing.TypeAlias`:\r\n\r\n```python\r\nfrom typing import Type, TypeAlias\r\n\r\nA = type[int] | str # error: Value of type \"Type[type]\" is not indexable\r\nB = Type[int] | str # error: Unsupported left operand type for | (\"object\")\r\nC: TypeAlias = type[int] | str # error: Value of type \"Type[type]\" is not indexable\r\nD: TypeAlias = Type[int] | str # error: Unsupported left operand type for | (\"object\")\r\n```\r\n\r\nThe order of the types in the union makes no difference when using `type[T]`: the following two variants still produce errors:\r\n\r\n```python\r\nE = str | type[int] # error: Value of type \"Type[type]\" is not indexable\r\nF: TypeAlias = str | type[int] # error: Value of type \"Type[type]\" is not indexable\r\n```\r\n\r\nHowever, the order of the types in the union _does_ matter if you're using `typing.Type[T]`:\r\n\r\n```python\r\nB = Type[int] | str # error: Unsupported left operand type for | (\"object\")\r\nD: TypeAlias = Type[int] | str # error: Unsupported left operand type for | (\"object\")\r\nG = str | Type[int] # no error\r\n```\r\n\r\nInterestingly, the bug can only be reproduced if `type` or `Type` is the outermost object in the type alias. Neither of the following two snippets causes mypy errors:\r\n\r\n```python\r\nE = list[type[int] | str]\r\nF = type[int | str]\r\n```\r\n\r\nMypy is also happy with `type[T]` or `Type[T]` being used in old-style `typing.Union` type aliases:\r\n\r\n```python\r\nfrom typing import Union\r\nG = Union[type[int], str]\r\n```\r\n\r\n**To Reproduce**\r\n\r\nMypy playground link [here](https://mypy-play.net/?mypy=latest&python=3.10&gist=490eed5cf7e5fd60b7b13f3eb31d6003).\r\n\r\n**Expected Behavior**\r\n\r\nNo error should be reported.\r\n\r\nCc. @JelleZijlstra","author":{"url":"https://github.com/AlexWaygood","@type":"Person","name":"AlexWaygood"},"datePublished":"2022-03-20T16:52:44Z","url":"https://github.com/python/mypy/issues/12392"}
| 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:9590a325-2ac7-2869-ddde-a94c0775c11e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A12E:3AE712:470696:5F9FA7:6B002D79 |
| html-safe-nonce | 89c00a2ba1e811768f0d29018daef2e31b7bcb5d7e2a1bdae29fccf7ef87c6b9 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBMTJFOjNBRTcxMjo0NzA2OTY6NUY5RkE3OjZCMDAyRDc5IiwidmlzaXRvcl9pZCI6Ijg3NTMyOTAwMDY5OTY0NjI5NjkiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | bff95251afd23c8cd0bb1ce336e4a3dcb31e31fcb37bf68ef6835aad20238ba9 |
| hovercard-subject-tag | issue:1174611682 |
| 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/12392/issue_layout |
| twitter:image | https://opengraph.githubassets.com/bec0f97dc63569ce0eb265db7767321bd0d3bb1c212af5ae4b9288751cc3fa68/python/mypy/issues/12392 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/bec0f97dc63569ce0eb265db7767321bd0d3bb1c212af5ae4b9288751cc3fa68/python/mypy/issues/12392 |
| og:image:alt | Bug Report The following two snippets (one using builtins.type, the other using typing.Type) both work fine at runtime on 3.10+. They should be accepted as valid type alias definitions on Python 3.... |
| 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 | 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