Title: TypedDict 'in' narrowing w/o @final · Issue #15697 · python/mypy · GitHub
Open Graph Title: TypedDict 'in' narrowing w/o @final · Issue #15697 · python/mypy
X Title: TypedDict 'in' narrowing w/o @final · Issue #15697 · python/mypy
Description: We shouldn't require @final decoration for TypedDicts to narrow them based on the 'in' operator. Why? Basically @erictraut's comment. In #13838, we've added "key in Union[TypedDict, ...]" narrowing for TypedDicts that are marked @final. ...
Open Graph Description: We shouldn't require @final decoration for TypedDicts to narrow them based on the 'in' operator. Why? Basically @erictraut's comment. In #13838, we've added "key in Union[TypedDict, ...]" narrowing...
X Description: We shouldn't require @final decoration for TypedDicts to narrow them based on the 'in' operator. Why? Basically @erictraut's comment. In #13838, we've added "key in Union[T...
Opengraph URL: https://github.com/python/mypy/issues/15697
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"TypedDict 'in' narrowing w/o @final","articleBody":"We shouldn't require `@final` decoration for TypedDicts to narrow them based on the 'in' operator.\r\n\r\n## Why?\r\n\r\nBasically @erictraut's [comment](https://github.com/python/mypy/issues/15695#issuecomment-1638955963).\r\n\r\nIn #13838, we've added \"key in Union[TypedDict, ...]\" narrowing for TypedDicts that are marked `@final`. The reason was to prevent this:\r\n\r\n```python\r\nclass Mammal(TypedDict):\r\n mammary_glands: int\r\n\r\nclass Bird(TypedDict):\r\n eggs: int\r\n\r\nclass Echidna(Mammal):\r\n eggs: int\r\n\r\nanimal: Mammal | Bird\r\nif 'eggs' in animal:\r\n assert_type(animal, Bird) # WRONG! Could still be a Mammal (a Echidna)\r\nif 'eggs' in animal and 'mammary_glands' in animal:\r\n assert_never(animal) # WRONG! Could still be a Mammal (a Echidna)\r\n```\r\n\r\nHowever, per @erictraut's [comment](https://github.com/python/mypy/issues/15695#issuecomment-1638955963), due to TypedDict being a structural type, we shouldn't consider the class hierarchy when type-matching.\r\n\r\nThis will be consistent with [pyright](https://github.com/microsoft/pyright/commit/6a25a7bf0b5cb3721a06d0e0d6245b2ebfbf053b) and TypeScript ([Playground Link](https://www.typescriptlang.org/play?&q=337#code/JYOwLgpgTgZghgYwgAgLJwLYbgG2QbwChllss4oBPAfQHMc4QATAZwC5kQBXDAI2kIBfQoVCRYiFACFgUJgWLIItWu049+UISLHR4SZAFEEAC2BMQcJQA9IzFmkzY8REstUdufAcMIA3CmRGYGcOdHI8AB9kGTlkAF5kAHleACsIBDAACgBKURhkLIBydxYi5FAgkBDcHIUSBAB7EBYwZF5ZJg5Y+UTg5xIAekH2rjamrhx5fiCjU3NLAEJtIA)).","author":{"url":"https://github.com/ikonst","@type":"Person","name":"ikonst"},"datePublished":"2023-07-18T03:23:57Z","url":"https://github.com/python/mypy/issues/15697"}
| 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:3dfc121c-9809-ce9d-ce3c-1ede8a04be70 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DE02:3DAD59:1DA0DBC:2732E14:6B003844 |
| html-safe-nonce | 680afe370e8abe022b3910eb6c2107ee0bd0e6d352777d848b3720f834862dd7 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJERTAyOjNEQUQ1OToxREEwREJDOjI3MzJFMTQ6NkIwMDM4NDQiLCJ2aXNpdG9yX2lkIjoiMTU1ODMyMDY2ODEyOTUwNTk2IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 279b4dabc5625899e44643b98034080383e91b03bb7665d44330d7bb3bd2a534 |
| hovercard-subject-tag | issue:1809008450 |
| 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/15697/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f40420ea66fcf0ea71dbcbceca6f3bd68d13b324bf5efb8fc8f7e48f84777eaf/python/mypy/issues/15697 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f40420ea66fcf0ea71dbcbceca6f3bd68d13b324bf5efb8fc8f7e48f84777eaf/python/mypy/issues/15697 |
| og:image:alt | We shouldn't require @final decoration for TypedDicts to narrow them based on the 'in' operator. Why? Basically @erictraut's comment. In #13838, we've added "key in Union[TypedDict, ...]" narrowing... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ikonst |
| 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