Repository navigation
Use string values when checking enum TypedDict keys - #21732
Open
snoopuppy582 wants to merge 2 commits into
Open
snoopuppy582 wants to merge 2 commits into
snoopuppy582 wants to merge 2 commits into
Conversation
TypedDict indexing treated any string-valued LiteralType as a string literal, including ordinary enum members whose literal value stores the member name. Require the literal fallback to be a subtype of str, while preserving StrEnum and string-mixin enum keys. Fixes python#21722.
This comment has been minimized.
This comment has been minimized.
snoopuppy582
marked this pull request as ready for review
September 6, 2026 00:21
Contributor
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
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.
Fixes #21722.
TypedDict indexing treats an Enum literal's stored member name as a string key. This accepts plain Enum members that raise
KeyErrorat runtime. For string enums, it can also check the wrong field:name = "value"indexes"value", not"name".Require a string subtype, then resolve string-enum values through the existing Enum attribute plugin before checking the key. Nested enum values are resolved the same way. Using the Enum descriptor avoids following an overridden
.valueproperty; custom constructors with unknown string values and recursive stub declarations are rejected as nonliteral keys.The regressions cover ordinary Enum reads and writes, both string-enum forms, mismatched names and values, literal unions, nested values, overridden properties and unsupported constructors. The four new cases fail on the previous PR head and pass with this change.
Validation on Windows, Python 3.13.12:
git diff --check: passed.KeyErrorcontrols.