Skip to content

Commit 72e879e

Browse files
committed
better generic handling
1 parent e4d68da commit 72e879e

4 files changed

Lines changed: 7 additions & 33 deletions

File tree

mypy/checker.py

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
from mypy.expandtype import expand_type
4646
from mypy.literals import Key, extract_var_from_literal_hash, literal, literal_hash
4747
from mypy.maptype import map_instance_to_supertype
48-
from mypy.meet import is_overlapping_erased_types, is_overlapping_types, meet_types
48+
from mypy.meet import is_overlapping_types, meet_types
4949
from mypy.message_registry import ErrorMessage
5050
from mypy.messages import (
5151
SUGGESTED_TEST_FIXTURES,
@@ -6553,19 +6553,6 @@ def comparison_type_narrowing_helper(self, node: ComparisonExpr) -> tuple[TypeMa
65536553
narrowable_indices={0},
65546554
)
65556555

6556-
# We only try and narrow away 'None' for now
6557-
if (
6558-
if_map is not None
6559-
and is_overlapping_none(item_type)
6560-
and not is_overlapping_none(collection_item_type)
6561-
and not (
6562-
isinstance(collection_item_type, Instance)
6563-
and collection_item_type.type.fullname == "builtins.object"
6564-
)
6565-
and is_overlapping_erased_types(item_type, collection_item_type)
6566-
):
6567-
if_map[operands[left_index]] = remove_optional(item_type)
6568-
65696556
if right_index in narrowable_operand_index_to_hash:
65706557
if_type, else_type = self.conditional_types_for_iterable(
65716558
item_type, iterable_type
@@ -6690,6 +6677,8 @@ def narrow_type_by_identity_equality(
66906677
# `x` to `Literal[Foo.A]` iff `Foo` has exactly one member.
66916678
# See testMatchEnumSingleChoice
66926679
target_type = coerce_to_literal(target_type)
6680+
if isinstance(target_type, Instance):
6681+
target_type = erase_type(target_type)
66936682

66946683
if (
66956684
# See comments in ambiguous_enum_equality_keys
@@ -8577,9 +8566,6 @@ def reduce_and_conditional_type_maps(ms: list[TypeMap], *, use_meet: bool) -> Ty
85778566
"builtins.bytes",
85788567
"builtins.bytearray",
85798568
"builtins.memoryview",
8580-
"builtins.list",
8581-
"builtins.dict",
8582-
"builtins.set",
85838569
}
85848570

85858571

mypy/meet.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -657,18 +657,6 @@ def _type_object_overlap(left: Type, right: Type) -> bool:
657657
return False
658658

659659

660-
def is_overlapping_erased_types(
661-
left: Type, right: Type, *, ignore_promotions: bool = False
662-
) -> bool:
663-
"""The same as 'is_overlapping_erased_types', except the types are erased first."""
664-
return is_overlapping_types(
665-
erase_type(left),
666-
erase_type(right),
667-
ignore_promotions=ignore_promotions,
668-
prohibit_none_typevar_overlap=True,
669-
)
670-
671-
672660
def are_typed_dicts_overlapping(
673661
left: TypedDictType, right: TypedDictType, is_overlapping: Callable[[Type, Type], bool]
674662
) -> bool:

test-data/unit/check-isinstance.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2018,7 +2018,7 @@ nested_any: List[List[Any]]
20182018
if lst in nested_any:
20192019
reveal_type(lst) # N: Revealed type is "builtins.list[builtins.int]"
20202020
if x in nested_any:
2021-
reveal_type(x) # N: Revealed type is "builtins.int | None"
2021+
reveal_type(x) # E: Statement is unreachable
20222022
[builtins fixtures/list.pyi]
20232023

20242024
[case testNarrowTypeAfterInTuple]

test-data/unit/check-narrowing.test

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2876,7 +2876,7 @@ def main(key: str):
28762876
[builtins fixtures/tuple.pyi]
28772877

28782878
[case testNarrowingCollections]
2879-
# flags: --warn-unreachable
2879+
# flags: --warn-unreachable --strict-equality
28802880
from typing import cast
28812881

28822882
class X:
@@ -2893,7 +2893,7 @@ class X:
28932893
reveal_type(self.x) # N: Revealed type is "builtins.dict[builtins.str, builtins.str]"
28942894
self.x["asdf"]
28952895

2896-
if self.x == cast(dict[int, int], {}):
2896+
if self.x == cast(dict[int, int], {}): # E: Non-overlapping equality check (left operand type: "dict[str, str]", right operand type: "dict[int, int]")
28972897
reveal_type(self.x) # N: Revealed type is "builtins.dict[builtins.str, builtins.str]"
28982898
self.x["asdf"]
28992899

@@ -2906,7 +2906,7 @@ class X:
29062906
reveal_type(self.y) # N: Revealed type is "builtins.list[builtins.str]"
29072907
self.y[0].does_not_exist # E: "str" has no attribute "does_not_exist"
29082908

2909-
if self.y == cast(list[int], []):
2909+
if self.y == cast(list[int], []): # E: Non-overlapping equality check (left operand type: "list[str]", right operand type: "list[int]")
29102910
reveal_type(self.y) # N: Revealed type is "builtins.list[builtins.str]"
29112911
self.y[0].does_not_exist # E: "str" has no attribute "does_not_exist"
29122912
[builtins fixtures/dict.pyi]

0 commit comments

Comments
 (0)