Title: [match-case]: `TypedDict` interaction with mapping pattern · Issue #2185 · python/typing · GitHub
Open Graph Title: [match-case]: `TypedDict` interaction with mapping pattern · Issue #2185 · python/typing
X Title: [match-case]: `TypedDict` interaction with mapping pattern · Issue #2185 · python/typing
Description: It seems match-cases mapping pattern is predestined to work well with TypedDict, but currently the spec is silent regarding TypedDict subtyping with respect to match case: https://typing.python.org/en/latest/spec/typeddict.html#subtyping...
Open Graph Description: It seems match-cases mapping pattern is predestined to work well with TypedDict, but currently the spec is silent regarding TypedDict subtyping with respect to match case: https://typing.python.org...
X Description: It seems match-cases mapping pattern is predestined to work well with TypedDict, but currently the spec is silent regarding TypedDict subtyping with respect to match case: https://typing.python.org...
Opengraph URL: https://github.com/python/typing/issues/2185
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[match-case]: `TypedDict` interaction with mapping pattern","articleBody":"It seems match-cases [mapping pattern](https://peps.python.org/pep-0634/#mapping-patterns) is predestined to work well with `TypedDict`, but currently the spec is silent regarding `TypedDict` subtyping with respect to match case: https://typing.python.org/en/latest/spec/typeddict.html#subtyping-with-mapping\n\nAt the time of writing, not a single type checker (tested with `mypy=1.19.1`, `pyright=1.1.408`, `ty=0.0.17`, `pyrefly=0.53.0` and `zuban=0.6.0`) can solve the following tests[^1], suggesting that some standardization / typing spec guidance may be useful.\n\n```python\nfrom typing import TypedDict, assert_type, assert_never\n\nclass D(TypedDict):\n name: str\n value: int\n\ndef test_match_key(x: D | int) -> None:\n match x:\n case {\"name\": _}:\n # runtime equivalent to (isinstance(x, Mapping) and \"name\" in x)\n assert_type(x, D)\n case _:\n assert_type(x, int)\n\ndef test_match_key_and_value(x: D | int) -> None:\n match x:\n case {\"name\": str()}:\n # runtime equivalent to (isinstance(x, Mapping)\n # and \"name\" in x and isinstance(x[\"name\"], str))\n assert_type(x, D)\n case _:\n assert_type(x, int)\n\ndef test_non_match(x: D | int) -> None:\n match x:\n case {\"value\": str()}:\n # runtime equivalent to (isinstance(x, Mapping)\n # and \"value\" in x and isinstance(x[\"value\"], str))\n assert_never(x)\n case _:\n assert_type(x, D | int)\n```\n\n[^1]: technically, it is possible to make a shared subclass of `int` and `collections.abc.Mapping`, so the `assert_type` in the `case {...}` are debatable, though the inference in the `case _` branches should not be affected.","author":{"url":"https://github.com/randolf-scholz","@type":"Person","name":"randolf-scholz"},"datePublished":"2026-02-20T15:53:08Z","url":"https://github.com/python/typing/issues/2185"}
| 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:e342df42-009b-07ea-acd4-f8f8821d1e3e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 962C:8FAC9:141109:1A4FB1:6AFF7FE4 |
| html-safe-nonce | 52d7f66206f38ec9b341f35ae970e28ad22114901853431cf66d728e48b04ca7 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NjJDOjhGQUM5OjE0MTEwOToxQTRGQjE6NkFGRjdGRTQiLCJ2aXNpdG9yX2lkIjoiMTU0MDg5NDQ0NDkzOTYwODAzNiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 24a71be9aa6a6f0776e29d53e7aba729f1bcd97c323a6a827f4d0df764bc884e |
| hovercard-subject-tag | issue:3969210529 |
| 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/typing/2185/issue_layout |
| twitter:image | https://opengraph.githubassets.com/1c23c63073c5113a99d59d2346f091c6edca9570388c0e6b5e18a341c64f7066/python/typing/issues/2185 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/1c23c63073c5113a99d59d2346f091c6edca9570388c0e6b5e18a341c64f7066/python/typing/issues/2185 |
| og:image:alt | It seems match-cases mapping pattern is predestined to work well with TypedDict, but currently the spec is silent regarding TypedDict subtyping with respect to match case: https://typing.python.org... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | randolf-scholz |
| hostname | github.com |
| expected-hostname | github.com |
| None | 19488db4031f00b04ea79b4c70b5dd32cfa873a8d20b3877f3896b8e2d195946 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/typing git https://github.com/python/typing.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 24604008 |
| octolytics-dimension-repository_nwo | python/typing |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 24604008 |
| octolytics-dimension-repository_network_root_nwo | python/typing |
| 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 | 7952f5285f4ba685d70c4c7540ad3cc9dc9746b8 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width