Skip to content

Commit

Permalink
Add regression test for isinstance narrowing (#18272)
Browse files Browse the repository at this point in the history
Regression test for #11839 resolved
with #18138.
  • Loading branch information
cdce8p authored Dec 9, 2024
1 parent ec4ccb0 commit ac89577
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions test-data/unit/check-isinstance.test
Original file line number Diff line number Diff line change
Expand Up @@ -2917,3 +2917,18 @@ if hasattr(mod, "y"):
[file mod.py]
def __getattr__(attr: str) -> str: ...
[builtins fixtures/module.pyi]

[case testTypeIsntLostAfterNarrowing]
from typing import Any

var: Any
reveal_type(var) # N: Revealed type is "Any"
assert isinstance(var, (bool, str))
reveal_type(var) # N: Revealed type is "Union[builtins.bool, builtins.str]"

if isinstance(var, bool):
reveal_type(var) # N: Revealed type is "builtins.bool"

# Type of var shouldn't fall back to Any
reveal_type(var) # N: Revealed type is "Union[builtins.bool, builtins.str]"
[builtins fixtures/isinstance.pyi]

0 comments on commit ac89577

Please sign in to comment.