10
10
UninhabitedType , TypeType , TypeOfAny , Overloaded , FunctionLike , LiteralType ,
11
11
ProperType , get_proper_type , get_proper_types
12
12
)
13
- from mypy .subtypes import (
14
- is_equivalent , is_subtype , is_protocol_implementation , is_callable_compatible ,
15
- is_proper_subtype ,
16
- )
13
+ from mypy .subtypes import is_equivalent , is_subtype , is_callable_compatible , is_proper_subtype
17
14
from mypy .erasetype import erase_type
18
15
from mypy .maptype import map_instance_to_supertype
19
16
from mypy .typeops import tuple_fallback
@@ -276,7 +273,13 @@ def _type_object_overlap(left: ProperType, right: ProperType) -> bool:
276
273
right = right .fallback
277
274
278
275
if isinstance (left , LiteralType ) and isinstance (right , LiteralType ):
279
- return left == right
276
+ if left .value == right .value :
277
+ # If values are the same, we still need to check if fallbacks are overlapping,
278
+ # this is done below.
279
+ left = left .fallback
280
+ right = right .fallback
281
+ else :
282
+ return False
280
283
elif isinstance (left , LiteralType ):
281
284
left = left .fallback
282
285
elif isinstance (right , LiteralType ):
@@ -285,15 +288,13 @@ def _type_object_overlap(left: ProperType, right: ProperType) -> bool:
285
288
# Finally, we handle the case where left and right are instances.
286
289
287
290
if isinstance (left , Instance ) and isinstance (right , Instance ):
288
- if left .type .is_protocol and is_protocol_implementation (right , left ):
289
- return True
290
- if right .type .is_protocol and is_protocol_implementation (left , right ):
291
+ # First we need to handle promotions and structural compatibility for instances
292
+ # that came as fallbacks, so simply call is_subtype() to avoid code duplication.
293
+ if (is_subtype (left , right , ignore_promotions = ignore_promotions )
294
+ or is_subtype (right , left , ignore_promotions = ignore_promotions )):
291
295
return True
292
296
293
297
# Two unrelated types cannot be partially overlapping: they're disjoint.
294
- # We don't need to handle promotions because they've already been handled
295
- # by the calls to `is_subtype(...)` up above (and promotable types never
296
- # have any generic arguments we need to recurse on).
297
298
if left .type .has_base (right .type .fullname ()):
298
299
left = map_instance_to_supertype (left , right .type )
299
300
elif right .type .has_base (left .type .fullname ()):
@@ -302,9 +303,6 @@ def _type_object_overlap(left: ProperType, right: ProperType) -> bool:
302
303
return False
303
304
304
305
if len (left .args ) == len (right .args ):
305
- if not left .args :
306
- # We can get here if the instance is in fact a fallback from another type.
307
- return True
308
306
# Note: we don't really care about variance here, since the overlapping check
309
307
# is symmetric and since we want to return 'True' even for partial overlaps.
310
308
#
0 commit comments