Skip to content

Add narrowing Union w/ bool Literal via identity check #8821

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 3 commits into from
May 19, 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: 4 additions & 2 deletions mypy/typeops.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,10 +604,12 @@ def is_singleton_type(typ: Type) -> bool:
constructing two distinct instances of 100001.
"""
typ = get_proper_type(typ)
# TODO: Also make this return True if the type is a bool LiteralType.
# TODO:
# Also make this return True if the type corresponds to ... (ellipsis) or NotImplemented?
return (
isinstance(typ, NoneType) or (isinstance(typ, LiteralType) and typ.is_enum_literal())
isinstance(typ, NoneType)
or (isinstance(typ, LiteralType)
and (typ.is_enum_literal() or isinstance(typ.value, bool)))
or (isinstance(typ, Instance) and typ.type.is_enum and len(get_enum_values(typ)) == 1)
)

Expand Down
42 changes: 42 additions & 0 deletions test-data/unit/check-narrowing.test
Original file line number Diff line number Diff line change
Expand Up @@ -984,3 +984,45 @@ 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'

str_or_true: Union[Literal[True], str]

if str_or_true is True:
reveal_type(str_or_true) # N: Revealed type is 'Literal[True]'
else:
reveal_type(str_or_true) # N: Revealed type is 'builtins.str'

if str_or_true is not True:
reveal_type(str_or_true) # N: Revealed type is 'builtins.str'
else:
reveal_type(str_or_true) # N: Revealed type is 'Literal[True]'

str_or_bool_literal: Union[Literal[False], Literal[True], str]

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

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

[builtins fixtures/primitives.pyi]