Skip to content

Commit

Permalink
Fixes type narrowing for overlaping runtime types (#11273)
Browse files Browse the repository at this point in the history
Closes #11272
  • Loading branch information
sobolevn authored Oct 31, 2021
1 parent 4ddc8fb commit f4a21a4
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
8 changes: 5 additions & 3 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -5395,9 +5395,11 @@ def conditional_type_map(expr: Expression,
return None, {}
else:
# we can only restrict when the type is precise, not bounded
proposed_precise_type = UnionType([type_range.item
for type_range in proposed_type_ranges
if not type_range.is_upper_bound])
proposed_precise_type = UnionType.make_union([
type_range.item
for type_range in proposed_type_ranges
if not type_range.is_upper_bound
])
remaining_type = restrict_subtype_away(current_type, proposed_precise_type)
return {expr: proposed_type}, {expr: remaining_type}
else:
Expand Down
2 changes: 2 additions & 0 deletions mypy/subtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1138,6 +1138,8 @@ def restrict_subtype_away(t: Type, s: Type, *, ignore_promotions: bool = False)
if (isinstance(get_proper_type(item), AnyType) or
not covers_at_runtime(item, s, ignore_promotions))]
return UnionType.make_union(new_items)
elif covers_at_runtime(t, s, ignore_promotions):
return UninhabitedType()
else:
return t

Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-inference.test
Original file line number Diff line number Diff line change
Expand Up @@ -1975,7 +1975,7 @@ T = TypeVar('T')

class A:
def f(self) -> None:
self.g() # E: Too few arguments for "g" of "A"
self.g() # E: Too few arguments for "g" of "A"
self.g(1)
@dec
def g(self, x: str) -> None: pass
Expand Down
29 changes: 29 additions & 0 deletions test-data/unit/check-narrowing.test
Original file line number Diff line number Diff line change
Expand Up @@ -1143,3 +1143,32 @@ else:
reveal_type(abc) # N: Revealed type is "TypedDict('__main__.B', {'tag': Literal['B'], 'b': builtins.int})"

[builtins fixtures/primitives.pyi]


[case testNarrowingRuntimeCover]
from typing import Dict, List, Union

def unreachable(x: Union[str, List[str]]) -> None:
if isinstance(x, str):
reveal_type(x) # N: Revealed type is "builtins.str"
elif isinstance(x, list):
reveal_type(x) # N: Revealed type is "builtins.list[builtins.str]"
else:
reveal_type(x) # N: Revealed type is "<nothing>"

def all_parts_covered(x: Union[str, List[str], List[int], int]) -> None:
if isinstance(x, str):
reveal_type(x) # N: Revealed type is "builtins.str"
elif isinstance(x, list):
reveal_type(x) # N: Revealed type is "Union[builtins.list[builtins.str], builtins.list[builtins.int]]"
else:
reveal_type(x) # N: Revealed type is "builtins.int"

def two_type_vars(x: Union[str, Dict[str, int], Dict[bool, object], int]) -> None:
if isinstance(x, str):
reveal_type(x) # N: Revealed type is "builtins.str"
elif isinstance(x, dict):
reveal_type(x) # N: Revealed type is "Union[builtins.dict[builtins.str, builtins.int], builtins.dict[builtins.bool, builtins.object]]"
else:
reveal_type(x) # N: Revealed type is "builtins.int"
[builtins fixtures/dict.pyi]

0 comments on commit f4a21a4

Please sign in to comment.