You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[match-case]: TypedDict interaction with mapping pattern #2185
At 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 tests1, suggesting that some standardization / typing spec guidance may be useful.
fromtypingimportTypedDict, assert_type, assert_neverclassD(TypedDict):
name: strvalue: intdeftest_match_key(x: D|int) ->None:
matchx:
case {"name": _}:
# runtime equivalent to (isinstance(x, Mapping) and "name" in x)assert_type(x, D)
case _:
assert_type(x, int)
deftest_match_key_and_value(x: D|int) ->None:
matchx:
case {"name": str()}:
# runtime equivalent to (isinstance(x, Mapping)# and "name" in x and isinstance(x["name"], str))assert_type(x, D)
case _:
assert_type(x, int)
deftest_non_match(x: D|int) ->None:
matchx:
case {"value": str()}:
# runtime equivalent to (isinstance(x, Mapping)# and "value" in x and isinstance(x["value"], str))assert_never(x)
case _:
assert_type(x, D|int)
Footnotes
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. ↩
changed the title [-][match-case]: `TypedDict` interaction with with mapping pattern[/-][+][match-case]: `TypedDict` interaction with mapping pattern[/+]on Feb 20, 2026
I'm not sure we need to say anything in the spec here. Type checkers should reflect the runtime behavior; if they do not, that's an issue that can be handled by improving the type checker.
Activity
[-][match-case]: `TypedDict` interaction with with mapping pattern[/-][+][match-case]: `TypedDict` interaction with mapping pattern[/+]JelleZijlstra commentedon Feb 20, 2026
I'm not sure we need to say anything in the spec here. Type checkers should reflect the runtime behavior; if they do not, that's an issue that can be handled by improving the type checker.