Skip to content

Narrow Union[str, Literal[False]] via is #8810

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
sbdchd opened this issue May 13, 2020 · 0 comments · Fixed by #8821
Closed

Narrow Union[str, Literal[False]] via is #8810

sbdchd opened this issue May 13, 2020 · 0 comments · Fixed by #8821

Comments

@sbdchd
Copy link
Contributor

sbdchd commented May 13, 2020

With the Union[str, Literal[False]] type below, mypy narrows the type correctly via isinstance, but is not False and is False do not work as expected.

from typing import Union, Literal

x: Union[str, Literal[False]] = False

if x is not False:
    reveal_type(x) # note: Revealed type is 'Union[builtins.str, Literal[False]]'
    
assert x is not False
reveal_type(x) # note: Revealed type is 'Union[builtins.str, Literal[False]]'

assert x is False
reveal_type(x) # note: Revealed type is 'Union[builtins.str, Literal[False]]'

assert not isinstance(x, bool)
reveal_type(x) # note: Revealed type is 'builtins.str'

Expected:

is not False and is False to narrow Union[str, Literal[False]]

Actual:

type isn't narrowed

mypy version: 0.770

test case

diff --git a/test-data/unit/check-narrowing.test b/test-data/unit/check-narrowing.test
index 45d4a625..4b693165 100644
--- a/test-data/unit/check-narrowing.test
+++ b/test-data/unit/check-narrowing.test
@@ -984,3 +984,20 @@ if true_or_false:
 else:
     reveal_type(true_or_false)  # N: Revealed type is 'Literal[False]'
 [builtins fixtures/primitives.pyi]
+
+[case testNarrowingLiteralIdentityCheck]
+from typing import Union
+from typing_extensions import Literal
+
+str_or_false: Union[Literal[False], str]
+
+if str_or_false is not False:
+    reveal_type(str_or_false)   # N: Revealed type is 'builtins.str'
+else:
+    reveal_type(str_or_false)   # N: Revealed type is 'Literal[False]'
+
+if str_or_false is False:
+    reveal_type(str_or_false)  # N: Revealed type is 'Literal[False]'
+else:
+    reveal_type(str_or_false)  # N: Revealed type is 'builtins.str'
+[builtins fixtures/primitives.pyi]

semi related:

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant