You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[match-case] Fix narrowing of class pattern with union-argument. (#19517)
Fixes#19468
Based on earlier PR #19473
- refactored the `conditional_types` function.
- return `UninhabitedType(), default` when no ranges are given. This
corresponds to `isinstance(x, ())`, with an empty tuple, which always
returns `False` at runtime.
- modified #19473 to change the proposed type, rather than directly
returning. This is essential to maintain the behavior of the unit test
`testIsinstanceWithOverlappingPromotionTypes`
- Added special casing in `restrict_subtype_away`: if the second
argument is a `TypeVar`, replace it with its upper bound (crucial to get
correct result in `testNarrowSelfType`)
- Allow `TypeChecker.get_isinstance_type` to return empty list (fixes
`isinstance(x, ())` behavior).
## Modified tests
- `testIsInstanceWithEmtpy2ndArg` now correctly infers unreachable for
`isinstance(x, ())`.
- `testNarrowingUnionMixins` now predicts the same results as pyright
playground
## New Tests
- `testMatchNarrowDownUnionUsingClassPattern`
(https://mypy-play.net/?mypy=1.17.0&python=3.12&gist=e9ec514f49903022bd32a82ae1774abd)
reveal_type(item) # N: Revealed type is "Union[__main__.<subclass of "__main__.Base" and "__main__.FooMixin">, __main__.<subclass of "__main__.Base" and "__main__.BarMixin">]"
2641
2641
if isinstance(item, FooMixin):
2642
-
reveal_type(item) # N: Revealed type is "__main__.FooMixin"
2642
+
reveal_type(item) # N: Revealed type is "__main__.<subclass of "__main__.Base" and "__main__.FooMixin">"
2643
2643
item.foo()
2644
2644
else:
2645
2645
reveal_type(item) # N: Revealed type is "__main__.<subclass of "__main__.Base" and "__main__.BarMixin">"
0 commit comments