From cdd0e40255d39c22f9c6a93ca3429667ca3d00a2 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Mon, 9 Dec 2024 15:32:00 +0100 Subject: [PATCH] Add regression test for isinstance narrowing --- test-data/unit/check-isinstance.test | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test-data/unit/check-isinstance.test b/test-data/unit/check-isinstance.test index 99bd62765b11..4ad128914c4e 100644 --- a/test-data/unit/check-isinstance.test +++ b/test-data/unit/check-isinstance.test @@ -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]