Fix narrowing with chained comparison#21150
Open
hauntsaninja wants to merge 1 commit intopython:masterfrom
Open
Fix narrowing with chained comparison#21150hauntsaninja wants to merge 1 commit intopython:masterfrom
hauntsaninja wants to merge 1 commit intopython:masterfrom
Conversation
Contributor
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
hauntsaninja
commented
Apr 3, 2026
| def f1(a: str | None, b: str | None) -> None: | ||
| if None is not a == b: | ||
| reveal_type(a) # N: Revealed type is "builtins.str" | ||
| reveal_type(b) # N: Revealed type is "builtins.str | None" |
Collaborator
Author
There was a problem hiding this comment.
This should also be str. I will fix this in a subsequent PR (this one is not a regression, it will need net new code)
ilevkivskyi
reviewed
Apr 3, 2026
| # TODO: support regular and len() narrowing in the same chain. | ||
| if any(m != ({}, {}) for m in partial_type_maps): | ||
| return reduce_conditional_maps(partial_type_maps) | ||
| if any(len(m[0]) or len(m[1]) for m in partial_type_maps): |
Member
There was a problem hiding this comment.
TBH I don't understand how is this different from what it was before. Is this just some drive-by performance micro-optimization?
ilevkivskyi
reviewed
Apr 3, 2026
| if any(m != ({}, {}) for m in partial_type_maps): | ||
| return reduce_conditional_maps(partial_type_maps) | ||
| if any(len(m[0]) or len(m[1]) for m in partial_type_maps): | ||
| return reduce_conditional_maps(partial_type_maps, use_meet=True) |
Member
There was a problem hiding this comment.
I am in favour of this, but just as a warning, this may have some subtle side-effect w.r.t. Any, so you may want to add some tests involving Any as well.
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 #21149 (the regression part of it, still working on the bad behaviour present in previous mypy versions)