typeddict: clarify that .get() with an unknown literal key is not an error#2312
Open
ashishpatel26 wants to merge 2 commits into
Open
typeddict: clarify that .get() with an unknown literal key is not an error#2312ashishpatel26 wants to merge 2 commits into
ashishpatel26 wants to merge 2 commits into
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 TypedDictd.get("unknown_key")— a literal key not defined in the TypedDictThis PR expands the bullet to cover all three cases explicitly:
T | Nonefor non-required keys; type checkers may returnTorT | Nonefor required.None.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.pycroscopeis the only checker that currently flagsmovie.get("other")as an error; its TOML is updated toPartial/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-casesconformance/tests/typeddicts_operations.py— removed# E?, updated commentsconformance/results/*/typeddicts_operations.toml— updated line numbers; pycroscope updated to Partial/FailTest plan
conformance_automated = "Fail"result for this test (as expected)validate_results.pypasses (all Fail results have explicitconformantfield)