Title: type guarded value is same type as `typing.TypeGuard[T]`? · Issue #10899 · python/mypy · GitHub
Open Graph Title: type guarded value is same type as `typing.TypeGuard[T]`? · Issue #10899 · python/mypy
X Title: type guarded value is same type as `typing.TypeGuard[T]`? · Issue #10899 · python/mypy
Description: Bug Report Okay so, let's say for a second you have this code: from typing_extensions import TypeGuard def type_guard_function(a: object) -> TypeGuard[int]: return isinstance(a, int) reveal_type(type_guard_function) n: object = 5 reveal_...
Open Graph Description: Bug Report Okay so, let's say for a second you have this code: from typing_extensions import TypeGuard def type_guard_function(a: object) -> TypeGuard[int]: return isinstance(a, int) reveal_type(ty...
X Description: Bug Report Okay so, let's say for a second you have this code: from typing_extensions import TypeGuard def type_guard_function(a: object) -> TypeGuard[int]: return isinstance(a, int) reveal_...
Opengraph URL: https://github.com/python/mypy/issues/10899
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"type guarded value is same type as `typing.TypeGuard[T]`?","articleBody":"**Bug Report**\r\n\r\nOkay so, let's say for a second you have this code:\r\n```py\r\nfrom typing_extensions import TypeGuard\r\n\r\ndef type_guard_function(a: object) -> TypeGuard[int]:\r\n return isinstance(a, int)\r\n\r\nreveal_type(type_guard_function)\r\n\r\nn: object = 5\r\nreveal_type(n)\r\n\r\nif type_guard_function(n):\r\n reveal_type(n)\r\nelse:\r\n reveal_type(n)\r\n```\r\n\r\nNow then, it turns out that in this bit:\r\n```py\r\nif type_guard_function(n):\r\n reveal_type(n)\r\n```\r\n\r\nmypy actually says `n` is a `TypeGuard`! If you look at `ConditionalTypeBinder#frames` during the `reveal_type`, you'll note a: `{('Var', \u003cmypy.nodes.Var object at 0x7f7a76e32580>): TypeGuard(builtins.int)}`.\r\n\r\n`ConditionalTypeBinder` stores types. To prove this to yourself, add a `n = '5'` after `n: object = 5`. You'll see a frame now looks like: `{('Var', \u003cmypy.nodes.Var object at 0x7f7a76e32580>): builtins.str}` (note that since it's the same variable, it's the same memory address :P)...\r\n\r\nAnyways, this seems completely off, because in `types.py`'s `TypeStrVisitor`, you have this:\r\n```py\r\n def visit_type_guard_type(self, t: TypeGuardType) -> str:\r\n return 'TypeGuard[{}]'.format(t.type_guard.accept(self))\r\n```\r\n\r\nHere are the relevant lines to the above:\r\n\r\n1) Type narrowing working around `TypeGuardType`:\r\n\r\nhttps://github.com/python/mypy/blob/7edceadf6ef7de2baab498a5d11b794f891854f5/mypy/checkexpr.py#L4188-L4190\r\n\r\nThis is what causes `reveal_type(n)` in the if statement to be `int`.\r\n\r\n2) The implementation that adds `TypeGuardType` to the value in the first place:\r\n\r\nhttps://github.com/python/mypy/blob/7edceadf6ef7de2baab498a5d11b794f891854f5/mypy/checker.py#L4147\r\n\r\n3) `TypeStrVisitor#visit_type_guard_type`\r\n\r\nhttps://github.com/python/mypy/blob/7edceadf6ef7de2baab498a5d11b794f891854f5/mypy/types.py#L2197-L2198\r\n\r\n**Expected Behavior**\r\n\r\nThis doesn't happen. `TypeGuardType` is only for `TypeGuard[T]`. Instead, there's another way to signal to type narrowing that a value became the way it is because of a type guard (necessary because of differences in behavior).\r\n\r\n**Actual Behavior**\r\n\r\nThis does happen. Chaos ensues, causing things like #10647 and #10665. (confirmed this by removing relevant line number 2 and testing both -- no crashes. However, I did this as described in https://github.com/python/mypy/pull/10671#discussion_r654746773 and well, the tests show the problem themselves.)\r\n\r\n**Your Environment**\r\n\r\nThe source code.\r\n\r\n\r\nI was redirected here by the contributing document, as this is a moderate (I think?) effort change, and I have no clue what exactly _should_ change. Like, should type guarded values use their own ProperType? Should there be a bool flag somewhere? No clue.\r\n\r\nFull disclaimer that I probably messed a few things up in my explanation, and that I may have misspoken a few terms. Hopefully I'm wrong and this isn't actually an issue :cold_sweat: .","author":{"url":"https://github.com/A5rocks","@type":"Person","name":"A5rocks"},"datePublished":"2021-08-01T01:04:45Z","url":"https://github.com/python/mypy/issues/10899"}
| 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:7c9e3354-4eb3-2b80-2242-72cf7208817c |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BD5A:133248:2CD727:3D5FD9:6B006A2B |
| html-safe-nonce | 929ceceb9587816a975f0bc6bd81381d78890cbb32d0a616fcd36360345f4416 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCRDVBOjEzMzI0ODoyQ0Q3Mjc6M0Q1RkQ5OjZCMDA2QTJCIiwidmlzaXRvcl9pZCI6IjMzMjExNjIwODI4MDM1NDI1NzEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 65bdfd5c3c098fec2adc53d26db07adf85a976169a91a4e14f627c34076d985b |
| hovercard-subject-tag | issue:957353054 |
| 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/10899/issue_layout |
| twitter:image | https://opengraph.githubassets.com/10f54a54fa752f0dc2dfcc4f6d2250aa72768da24d5a3a3bd4263b7d86e0a043/python/mypy/issues/10899 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/10f54a54fa752f0dc2dfcc4f6d2250aa72768da24d5a3a3bd4263b7d86e0a043/python/mypy/issues/10899 |
| og:image:alt | Bug Report Okay so, let's say for a second you have this code: from typing_extensions import TypeGuard def type_guard_function(a: object) -> TypeGuard[int]: return isinstance(a, int) reveal_type(ty... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | A5rocks |
| hostname | github.com |
| expected-hostname | github.com |
| None | 6527e91c60c0b29d16606ff32ac5df8ecdc37a2a62205ea057a14d7762c870fa |
| 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 | 46f1cd2ae3cd8ae1a81019b5d619742e76f13bbd |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width