Repository navigation
Fall back to argument-only inference when literal context is too narrow - #22351
Draft
edgarrmondragon wants to merge 3 commits into
Draft
edgarrmondragon wants to merge 3 commits into
edgarrmondragon wants to merge 3 commits into
Conversation
When the return type is a bare type variable (or an optional one) and the type context is a union mixing literals with other types, the context was used to solve the type variable before looking at the arguments. This could produce a spurious error if an argument was wider than the context: ```python def none_if_one[T](value: T | Literal[1]) -> T | None: ... x: str | Literal[1] | None = 1 x = none_if_one(x) # error: expected "str | Literal[1]" ``` Now, if using the literal context produces errors, retry inference from the arguments only, and use that if it type checks and the result is compatible with the context. Fixes python#22334 Signed-off-by: Edgar Ramírez Mondragón <edgarrm358@gmail.com>
Signed-off-by: Edgar Ramírez Mondragón <edgarrm358@gmail.com>
This comment has been minimized.
This comment has been minimized.
1 similar comment
This comment has been minimized.
This comment has been minimized.
Signed-off-by: Edgar Ramírez Mondragón <edgarrm358@gmail.com>
Member
|
Sorry, but I think this is (very) wrong. IMO this issue should be solved by removing (FWIW I verified my idea locally and it works, but there is also primer...) |
Contributor
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
Contributor
Author
Ok, thanks for taking a look 🙇♂️. Removing the |
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.
When the return type is a bare type variable (or an optional one) and the type context is a union mixing literals (or sentinels, or enums) with other types, the context was used to solve the type variable before looking at the arguments. This could produce a spurious error if an argument was wider than the context:
Now, if using the literal context produces errors, retry inference from the arguments only, and use that if it type checks and the result is compatible with the context.
Fixes #22334