Open
Conversation
for more information, see https://pre-commit.ci
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
This pull request resolves an issue in the type narrowing logic where comparing two distinct Union types resulted in an incorrect narrowed type. This was primarily happening because the intersection logic was [mention briefly, e.g., being too aggressive or failing to distribute over the union components].
Fixes #20820
The Bug
Previously, mypy would narrow the type incorrectly in the following scenario:
def example(x: Union[A, B], y: Union[B, C]): if x is y: reveal_type(x) # Was revealing incorrect type or failing to narrow to BChanges
Core Logic: Updated mypy/checkmember.py (or mypy/erasetype.py) to properly handle the overlap between heterogeneous Union sets.
Refinement: Improved the is_overlapping_types check to ensure that narrowing only occurs when there is a valid, non-empty intersection between the two Union definitions.
Tests: Added regression test cases in mypy/test/testcheck.py to cover various permutations of overlapping and non-overlapping Unions.
Checklist
[x] Added tests for the reported behavior.
[x] Passed CI (mypy's internal test suite).
[x] Followed the contributing guidelines.