|
39 | 39 | from mypy import types
|
40 | 40 | from mypy.sametypes import is_same_type
|
41 | 41 | from mypy.erasetype import replace_meta_vars, erase_type
|
| 42 | +from mypy.maptype import map_instance_to_supertype |
42 | 43 | from mypy.messages import MessageBuilder
|
43 | 44 | from mypy import message_registry
|
44 | 45 | from mypy.infer import infer_type_arguments, infer_function_type_arguments
|
|
71 | 72 | MAX_UNIONS = 5 # type: Final
|
72 | 73 |
|
73 | 74 |
|
| 75 | +# Types considered safe for comparisons with --strict-equality due to known behaviour of __eq__. |
| 76 | +# NOTE: All these types are subtypes of AbstractSet. |
| 77 | +OVERLAPPING_TYPES_WHITELIST = ['builtins.set', 'builtins.frozenset', |
| 78 | + 'typing.KeysView', 'typing.ItemsView'] # type: Final |
| 79 | + |
| 80 | + |
74 | 81 | class TooManyUnions(Exception):
|
75 | 82 | """Indicates that we need to stop splitting unions in an attempt
|
76 | 83 | to match an overload in order to save performance.
|
@@ -2051,6 +2058,14 @@ def dangerous_comparison(self, left: Type, right: Type,
|
2051 | 2058 | # We need to special case bytes, because both 97 in b'abc' and b'a' in b'abc'
|
2052 | 2059 | # return True (and we want to show the error only if the check can _never_ be True).
|
2053 | 2060 | return False
|
| 2061 | + if isinstance(left, Instance) and isinstance(right, Instance): |
| 2062 | + # Special case some builtin implementations of AbstractSet. |
| 2063 | + if (left.type.fullname() in OVERLAPPING_TYPES_WHITELIST and |
| 2064 | + right.type.fullname() in OVERLAPPING_TYPES_WHITELIST): |
| 2065 | + abstract_set = self.chk.lookup_typeinfo('typing.AbstractSet') |
| 2066 | + left = map_instance_to_supertype(left, abstract_set) |
| 2067 | + right = map_instance_to_supertype(right, abstract_set) |
| 2068 | + return not is_overlapping_types(left.args[0], right.args[0]) |
2054 | 2069 | return not is_overlapping_types(left, right, ignore_promotions=False)
|
2055 | 2070 |
|
2056 | 2071 | def get_operator_method(self, op: str) -> str:
|
|
0 commit comments