Skip to content

Commit 6f9ff36

Browse files
committed
Make is_subtype and is_proper_subtype do promotion the same way
Fixes #1850.
1 parent 29a74f3 commit 6f9ff36

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

mypy/subtypes.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,11 @@ def visit_instance(self, left: Instance) -> bool:
128128
if isinstance(right, TupleType) and right.fallback.type.is_enum:
129129
return is_subtype(left, right.fallback)
130130
if isinstance(right, Instance):
131-
if left.type._promote and is_subtype(
132-
left.type._promote, self.right, self.check_type_parameter,
133-
ignore_pos_arg_names=self.ignore_pos_arg_names):
134-
return True
131+
for base in left.type.mro:
132+
if base._promote and is_subtype(
133+
base._promote, self.right, self.check_type_parameter,
134+
ignore_pos_arg_names=self.ignore_pos_arg_names):
135+
return True
135136
rname = right.type.fullname()
136137
if not left.type.has_base(rname) and rname != 'builtins.object':
137138
return False

test-data/unit/check-unions.test

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,3 +436,11 @@ class D:
436436
def __le__(self, other) -> int: ...
437437
class E:
438438
def __ge__(self, other: Union[C, D]) -> int: ...
439+
440+
[case testUnionSimplificationWithBoolIntAndFloat]
441+
from typing import List, Union
442+
l = reveal_type([]) # type: List[Union[bool, int, float]] \
443+
# E: Revealed type is 'builtins.list[builtins.float]'
444+
reveal_type(l) \
445+
# E: Revealed type is 'builtins.list[Union[builtins.bool, builtins.int, builtins.float]]'
446+
[builtins fixtures/list.pyi]

test-data/unit/fixtures/list.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ class list(Iterable[T], Generic[T]):
2626
class tuple: pass
2727
class function: pass
2828
class int: pass
29+
class float: pass
2930
class str: pass
30-
class bool: pass
31+
class bool(int): pass
3132

3233
property = object() # Dummy definition.

0 commit comments

Comments
 (0)