Skip to content

Fix issue #20820#20824

Open
lshariprasad wants to merge 2 commits intopython:masterfrom
lshariprasad:fix-20820
Open

Fix issue #20820#20824
lshariprasad wants to merge 2 commits intopython:masterfrom
lshariprasad:fix-20820

Conversation

@lshariprasad
Copy link

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 B

Changes
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect narrowing between two different unions

1 participant