Repository navigation
Use literal overloads when inferring generic protocol arguments - #22436
Closed
sakshichitnis27 wants to merge 1 commit into
Closed
sakshichitnis27 wants to merge 1 commit into
sakshichitnis27 wants to merge 1 commit into
Conversation
If a generic protocol argument is implemented with overloads that are selected by literal values (or is a TypedDict and the protocol has __getitem__), infer it in the second pass and keep the literal value of type variables from the first pass. Treat TypedDict __getitem__ as having an overload for each key. If the literal can't be used, inference works as before. Fixes python#22199 Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
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 #22199
Currently mypy infers
objectforRbelow, because a TypedDict is matchedagainst the protocol through its
Mapping[str, object]fallback:The same limitation affects any class whose protocol member is overloaded on
Literalparameters: the first overload item is picked while inferring, sof("b", y)failed with "Cannot infer value of type parameter T".This PR reuses the existing two-pass inference for these cases:
Literal-parameter overloads (or is a TypedDict and the protocol has__getitem__), and a type variable of the protocol is also used by anotherparameter, inference first runs without that argument. If the first pass
gives a literal value for a type variable (e.g.
"b"), and the argument iscompatible with the protocol after substituting it, the argument is used in
the second pass with the literal preserved (
Literal["b"]instead ofstr),so the matching overload item can be selected.
also has
keys() -> Iterable[K], a literal key can't work, and the oldresult is kept.
__getitem__of a TypedDict is treated asone overload per key followed by the usual
(str) -> objectmethod. Becauseof the last item, everything that was accepted before is still accepted.
Things reviewers may want to look at:
def g[K, V](k: K, d: GetX[K, V]) -> tuple[K, V]givestuple[Literal["b"], str]forg("b", x).a test for that), and I did not try to improve union-of-literals keys.
Testing
check-typeddict.testandcheck-protocols.test; they failwithout the change (except the "unchanged" ones, which pass both ways).
python -m pytest -n 4 mypy/test(12361 passed)python -m mypy --config-file mypy_self_check.ini -p mypy -p mypycpackages (attrs, _pytest, pip, virtualenv, ...) and on randomly generated
programs; the only differences were the intended improvements.
I used an LLM assistant while investigating this and drafting the change, and I
have reviewed and tested the code myself.
🤖 Generated with Claude Code