10000 Allow booleans to be narrowed to literal types by ethan-leba · Pull Request #10389 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Allow booleans to be narrowed to literal types #10389

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 8 commits into from
Nov 7, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Rename expansion-related locals/functions
No longer only works on enums
  • Loading branch information
ethan-leba authored and Ethan Leba committed Aug 6, 2021
commit 9e7a695b308cb658d0c0cc0f09ab1f9f2bc62a43
10 changes: 5 additions & 5 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
map_type_from_supertype, bind_self, erase_to_bound, make_simplified_union,
erase_def_to_union_or_bound, erase_to_union_or_bound, coerce_to_literal,
try_getting_str_literals_from_type, try_getting_int_literals_from_type,
tuple_fallback, is_singleton_type, try_expanding_enum_to_union,
tuple_fallback, is_singleton_type, try_expanding_sum_type_to_union,
true_only, false_only, function_type, get_type_vars, custom_special_method,
is_literal_type_like,
)
Expand Down Expand Up @@ -4612,11 +4612,11 @@ def refine_identity_comparison_expression(self,
if singleton_index == -1:
singleton_index = possible_target_indices[-1]

enum_name = None
sum_type_name = None
target = get_proper_type(target)
if (isinstance(target, LiteralType) and
(target.is_enum_literal() or isinstance(target.value, bool))):
enum_name = target.fallback.type.fullname
sum_type_name = target.fallback.type.fullname

target_type = [TypeRange(target, is_upper_bound=False)]

Expand All @@ -4637,8 +4637,8 @@ def refine_identity_comparison_expression(self,
expr = operands[i]
expr_type = coerce_to_literal(operand_types[i])

if enum_name is not None:
expr_type = try_expanding_enum_to_union(expr_type, enum_name)
if sum_type_name is not None:
expr_type = try_expanding_sum_type_to_union(expr_type, sum_type_name)

# We intentionally use 'conditional_type_map' directly here instead of
# 'self.conditional_type_map_with_intersection': we only compute ad-hoc
Expand Down
4 changes: 2 additions & 2 deletions mypy/typeops.py
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ def is_singleton_type(typ: Type) -> bool:
)


def try_expanding_enum_to_union(typ: Type, target_fullname: str) -> ProperType:
def try_expanding_sum_type_to_union(typ: Type, target_fullname: str) -> ProperType:
"""Attempts to recursively expand any enum Instances with the given target_fullname
into a Union of all of its component LiteralTypes.

Expand All @@ -721,7 +721,7 @@ class Status(Enum):
typ = get_proper_type(typ)

if isinstance(typ, UnionType):
items = [try_expanding_enum_to_union(item, target_fullname) for item in typ.items]
items = [try_expanding_sum_type_to_union(item, target_fullname) for item in typ.items]
return make_simplified_union(items, contract_literals=False)
elif isinstance(typ, Instance) and typ.type.fullname == target_fullname:
if typ.type.is_enum:
Expand Down
0