Skip to content

Support determining whether a literal is truthy #8368

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 3 additions & 3 deletions mypy/test/testtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -736,10 +736,10 @@ def test_literal_type(self) -> None:
self.assert_join(UnionType([lit1, lit2]), lit2, UnionType([lit1, lit2]))
self.assert_join(UnionType([lit1, lit2]), a, a)
self.assert_join(UnionType([lit1, lit3]), a, UnionType([a, lit3]))
self.assert_join(UnionType([d, lit3]), lit3, UnionType([d, lit3]))
self.assert_join(UnionType([d, lit3]), lit3, d)
self.assert_join(UnionType([d, lit3]), d, UnionType([d, lit3]))
self.assert_join(UnionType([a, lit1]), lit1, UnionType([a, lit1]))
self.assert_join(UnionType([a, lit1]), lit2, UnionType([a, lit1]))
self.assert_join(UnionType([a, lit1]), lit1, a)
self.assert_join(UnionType([a, lit1]), lit2, a)
self.assert_join(UnionType([lit1, lit2]),
UnionType([lit1, lit2]),
UnionType([lit1, lit2]))
Expand Down
8 changes: 7 additions & 1 deletion mypy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -1591,10 +1591,16 @@ class LiteralType(ProperType):

def __init__(self, value: LiteralValue, fallback: Instance,
line: int = -1, column: int = -1) -> None:
super().__init__(line, column)
self.value = value
super().__init__(line, column)
self.fallback = fallback

def can_be_false_default(self) -> bool:
return not self.value

def can_be_true_default(self) -> bool:
return bool(self.value)

def accept(self, visitor: 'TypeVisitor[T]') -> T:
return visitor.visit_literal_type(self)

Expand Down
19 changes: 19 additions & 0 deletions test-data/unit/check-narrowing.test
Original file line number Diff line number Diff line change
Expand Up @@ -965,3 +965,22 @@ else:
reveal_type(a) # E: Statement is unreachable
reveal_type(b)
[builtins fixtures/primitives.pyi]

[case testNarrowingLiteralTruthiness]
from typing import Union
from typing_extensions import Literal

str_or_false: Union[Literal[False], str]

if str_or_false:
reveal_type(str_or_false) # N: Revealed type is 'builtins.str'
else:
reveal_type(str_or_false) # N: Revealed type is 'Union[Literal[False], builtins.str]'

true_or_false: Literal[True, False]

if true_or_false:
reveal_type(true_or_false) # N: Revealed type is 'Literal[True]'
else:
reveal_type(true_or_false) # N: Revealed type is 'Literal[False]'
[builtins fixtures/primitives.pyi]