Fix Resolve TypedDict keys in go-to-definition - #4398
Open
aodihis wants to merge 2 commits into
Open
Conversation
TypedDict string keys currently fall through to operator navigation and resolve to dict.__getitem__. Resolve literal keys against the inferred TypedDict class fields and MRO before that fallback, covering class-based, inherited, and functional declarations while preserving ordinary subscript behavior.
Contributor
|
This pull request has been imported. If you are a Meta employee, you can view this in D114472920. (Because this pull request was imported automatically, there will not be any future comments.) |
This comment has been minimized.
This comment has been minimized.
Distinguish declared TypedDict key access from ordinary subscripts before resolving the field. This lets missing fields return no definition instead of falling through to __getitem__, while keeping the field lookup itself optional and covering both class and functional declarations.
|
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.
Summary
Fixes #4360
Go-to-definition did not handle TypedDict subscript keys. In
person["name"],"name"is a string literal, so no identifier is found and the request enters theNonecase. It then falls back to operator navigation and incorrectly resolves todict.__getitem__.This adds TypedDict handling before the operator fallback. It resolves keys using the inferred TypedDict fields and MRO, supporting class-based, inherited, and functional TypedDicts.
If the TypedDict does not contain the key, go-to-definition now returns no definition instead of falling back to
__getitem__.Test Plan
Added:
typed_dict_key_testfor class-based, inherited, and functional TypedDicts.typed_dict_missing_key_testto verify missing keys return no definition.