[match-case]: `TypedDict` interaction with mapping pattern

Open
#2,185 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
python
Domain
devtools

Research direction

Start with the TypedDict subtyping-with-mapping section of the typing specification and PEP 634's mapping-pattern rules. Run the three examples against the listed type checkers to compare current behavior. Done means reaching agreement on the intended narrowing and documenting standardized guidance, with corresponding validation cases.

Written by the indexing model from the issue text.

Description

topic: feature

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-with-mapping

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 tests[^1], suggesting that some standardization / typing spec guidance may be useful.

from typing import TypedDict, assert_type, assert_never

class D(TypedDict):
    name: str
    value: int

def test_match_key(x: D | int) -> None:
    match x:
        case {"name": _}:
            # runtime equivalent to (isinstance(x, Mapping) and "name" in x)
            assert_type(x, D)
        case _:
            assert_type(x, int)

def test_match_key_and_value(x: D | int) -> None:
    match x:
        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)

def test_non_match(x: D | int) -> None:
    match x:
        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)

[^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.

Dominant language
Python
Stars
1.8k
Forks
302
Avg merge
23h
Merged PRs (30d)
8

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from python/typing

All issues in python/typing

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.