Skip to content

typeddict: clarify that .get() with an unknown literal key is not an error#2312

Open
ashishpatel26 wants to merge 2 commits into
python:mainfrom
ashishpatel26:fix/2054-typeddict-get-unknown-literal-key
Open

typeddict: clarify that .get() with an unknown literal key is not an error#2312
ashishpatel26 wants to merge 2 commits into
python:mainfrom
ashishpatel26:fix/2054-typeddict-get-unknown-literal-key

Conversation

@ashishpatel26

Copy link
Copy Markdown

Summary

Fixes #2054.

The spec's .get() exception bullet only specified the return type for the variable-key case ("cannot be determined statically"), leaving two cases undefined:

  • d.get("known_key") — a literal key that exists in the TypedDict
  • d.get("unknown_key") — a literal key not defined in the TypedDict

This PR expands the bullet to cover all three cases explicitly:

  1. Known literal key — return type is T | None for non-required keys; type checkers may return T or T | None for required.
  2. Unknown literal key — NOT an error; return type is the union of all value types combined with None.
  3. Non-literal key — unchanged: union of all value types combined with None.

Conformance test changes

The test had movie.get("other") # E? — the # E? marker is a wildcard that treats errors as optional (neither requiring nor forbidding one). Now that the spec is explicit, this becomes a plain call with no error marker.

pycroscope is the only checker that currently flags movie.get("other") as an error; its TOML is updated to Partial/Fail. All other checker TOMLs are updated for the +2-line shift caused by replacing the "not clear" comments with spec-quoting comments.

Files changed

  • docs/spec/typeddict.rst — expanded .get() bullet with three explicit sub-cases
  • conformance/tests/typeddicts_operations.py — removed # E?, updated comments
  • conformance/results/*/typeddicts_operations.toml — updated line numbers; pycroscope updated to Partial/Fail

Test plan

  • Conformance suite run locally confirms line numbers in all TOMLs are correct
  • Only pycroscope has a conformance_automated = "Fail" result for this test (as expected)
  • validate_results.py passes (all Fail results have explicit conformant field)

…error

The spec's `.get()` bullet only specified the return type for the
variable-key case ("cannot be determined statically"), leaving the
literal-key cases undefined.  Expand the bullet to cover all three
cases:

1. Known literal key — return type is ``T | None`` for non-required
   keys; type checkers may return ``T`` or ``T | None`` for required.
2. Unknown literal key — NOT an error; return type is the union of
   all value types combined with ``None``.
3. Non-literal key — unchanged: union of all value types | ``None``.

The conformance test had ``movie.get("other")  # E?``, which the runner
treats as "optional" (neither requiring nor forbidding an error).  Now
that the spec is explicit, change it to a plain call with no error
marker, and update the surrounding comments to quote the spec.

pycroscope is the only checker that flags ``movie.get("other")`` as an
error; its TOML now shows Partial/Fail reflecting this non-conformance.
All other checker TOMLs updated for the +2-line shift.

Fixes: python#2054
@srittau srittau added the topic: typing spec For improving the typing spec label Jun 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic: typing spec For improving the typing spec

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Discrepancy between spec and tests on indexing TypedDicts with unknown literal keys

2 participants