Make optional type narrowing actually optional in tuples_type_compat.py#1981
Merged
erictraut merged 1 commit intopython:mainfrom Apr 22, 2025
Merged
Conversation
Collaborator
|
I agree these should be marked with "E?" rather than "E" because they are optional in the spec. Thanks for the fix. |
erictraut
approved these changes
Apr 22, 2025
| if len(val) == 1: | ||
| # Type can be narrowed to tuple[int]. | ||
| assert_type(val, tuple[int]) # tuple[int] | ||
| assert_type(val, tuple[int]) # E?: tuple[int] |
Member
There was a problem hiding this comment.
This makes the test not very useful (without manual review) since type checkers will now pass no matter what type they infer for val. One solution could be to use two types and asserts that exactly one of them errors, by using an error group and asserting for both the narrowed and non-narrowed type. However, we don't currently check in that case that there is exactly one error. I can send a PR doing that.
Contributor
Author
There was a problem hiding this comment.
Sounds reasonable to me.
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.
This pull request marks optional type narrowing in tuples_type_compat.py with
E?to signify its optionality. Otherwise, it is treated as obligatory.See the comment
NOTE: This type narrowing functionality is optional, not mandated.at the beginning of each function.