Skip to content

Commit b315209

Browse files
committed
Fix mypy primer bugs
1 parent 888788f commit b315209

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

mypy/checker.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4702,14 +4702,20 @@ def conditional_len_map(self,
47024702
possible_types = union_items(current_type)
47034703
len_of_types = [len_of_type(typ) for typ in possible_types]
47044704

4705-
proposed_type = UnionType([
4705+
proposed_type = make_simplified_union([
47064706
self.narrow_type_by_length(typ, length)
47074707
for typ, l in zip(possible_types, len_of_types)
47084708
if l is None or l == length])
4709-
remaining_type = UnionType([
4709+
remaining_type = make_simplified_union([
47104710
typ for typ, l in zip(possible_types, len_of_types)
47114711
if l is None or l != length])
4712-
return {expr: proposed_type}, {expr: remaining_type}
4712+
if_map = (
4713+
{} if is_same_type(proposed_type, current_type)
4714+
else {expr: proposed_type})
4715+
else_map = (
4716+
{} if is_same_type(remaining_type, current_type)
4717+
else {expr: remaining_type})
4718+
return if_map, else_map
47134719
else:
47144720
return {}, {}
47154721

test-data/unit/check-narrowing.test

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,8 +1078,8 @@ else:
10781078
reveal_type(x) # N: Revealed type is "Tuple[builtins.int, builtins.int, builtins.int]"
10791079
[builtins fixtures/len.pyi]
10801080

1081-
[case testNarrowingLenListAndStrUnaffected]
1082-
from typing import Tuple, Union, List
1081+
[case testNarrowingLenTypeUnaffected]
1082+
from typing import Tuple, Union, List, Any
10831083

10841084
def make() -> Union[str, List[int]]:
10851085
return ""
@@ -1090,6 +1090,13 @@ if len(x) == 3:
10901090
reveal_type(x) # N: Revealed type is "Union[builtins.str, builtins.list[builtins.int]]"
10911091
else:
10921092
reveal_type(x) # N: Revealed type is "Union[builtins.str, builtins.list[builtins.int]]"
1093+
1094+
def f(self, value: Any) -> Any:
1095+
if isinstance(value, list) and len(value) == 0:
1096+
reveal_type(value) # N: Revealed type is "builtins.list[Any]"
1097+
return value
1098+
reveal_type(value) # N: Revealed type is "Any"
1099+
return None
10931100
[builtins fixtures/len.pyi]
10941101

10951102
[case testNarrowingLenLiteral]

0 commit comments

Comments
 (0)