Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add test case `testNarrowingIsInstanceNoIntersectionWithFinalTypeAndN…
…oneType` and modify (fix?) the second algorithm tried in `conditional_types_with_intersection`.
  • Loading branch information
tyralla committed Dec 5, 2023
commit fe12f7d1a62a66c45ba23c45965e4064117a2fff
7 changes: 4 additions & 3 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -7120,9 +7120,10 @@ def conditional_types_with_intersection(
possible_target_types = []
for tr in type_ranges:
item = get_proper_type(tr.item)
if not isinstance(item, Instance) or tr.is_upper_bound:
return yes_type, no_type
possible_target_types.append(item)
if isinstance(item, Instance):
possible_target_types.append(item)
if not possible_target_types:
return yes_type, no_type

out = []
errors: list[tuple[str, str]] = []
Expand Down
22 changes: 22 additions & 0 deletions test-data/unit/check-narrowing.test
Original file line number Diff line number Diff line change
Expand Up @@ -2067,3 +2067,25 @@ def f(cls: Type[Union[None, int]]) -> None:
else:
reveal_type(cls) # N: Revealed type is "Type[builtins.int]"
[builtins fixtures/isinstance.pyi]

[case testNarrowingIsInstanceNoIntersectionWithFinalTypeAndNoneType]
# flags: --warn-unreachable --python-version 3.10

from types import NoneType
from typing import final

class X: ...
class Y: ...
@final
class Z: ...

x: X

if isinstance(x, (Y, Z)):
reveal_type(x) # N: Revealed type is "__main__.<subclass of "X" and "Y">"
if isinstance(x, (Y, NoneType)):
reveal_type(x) # N: Revealed type is "__main__.<subclass of "X" and "Y">1"
if isinstance(x, (Y, Z, NoneType)):
reveal_type(x) # N: Revealed type is "__main__.<subclass of "X" and "Y">2"

[builtins fixtures/isinstance.pyi]