From 6f9ff36af05142ec48ec263d3ad828a874652803 Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Fri, 31 Mar 2017 13:03:51 +0100 Subject: [PATCH] Make is_subtype and is_proper_subtype do promotion the same way Fixes #1850. --- mypy/subtypes.py | 9 +++++---- test-data/unit/check-unions.test | 8 ++++++++ test-data/unit/fixtures/list.pyi | 3 ++- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/mypy/subtypes.py b/mypy/subtypes.py index b96618da28eb..3169864fe85c 100644 --- a/mypy/subtypes.py +++ b/mypy/subtypes.py @@ -128,10 +128,11 @@ def visit_instance(self, left: Instance) -> bool: if isinstance(right, TupleType) and right.fallback.type.is_enum: return is_subtype(left, right.fallback) if isinstance(right, Instance): - if left.type._promote and is_subtype( - left.type._promote, self.right, self.check_type_parameter, - ignore_pos_arg_names=self.ignore_pos_arg_names): - return True + for base in left.type.mro: + if base._promote and is_subtype( + base._promote, self.right, self.check_type_parameter, + ignore_pos_arg_names=self.ignore_pos_arg_names): + return True rname = right.type.fullname() if not left.type.has_base(rname) and rname != 'builtins.object': return False diff --git a/test-data/unit/check-unions.test b/test-data/unit/check-unions.test index 8c597bef9ac4..5b1363876932 100644 --- a/test-data/unit/check-unions.test +++ b/test-data/unit/check-unions.test @@ -436,3 +436,11 @@ class D: def __le__(self, other) -> int: ... class E: def __ge__(self, other: Union[C, D]) -> int: ... + +[case testUnionSimplificationWithBoolIntAndFloat] +from typing import List, Union +l = reveal_type([]) # type: List[Union[bool, int, float]] \ + # E: Revealed type is 'builtins.list[builtins.float]' +reveal_type(l) \ + # E: Revealed type is 'builtins.list[Union[builtins.bool, builtins.int, builtins.float]]' +[builtins fixtures/list.pyi] diff --git a/test-data/unit/fixtures/list.pyi b/test-data/unit/fixtures/list.pyi index ce9978a38c4c..4d811813776d 100644 --- a/test-data/unit/fixtures/list.pyi +++ b/test-data/unit/fixtures/list.pyi @@ -26,7 +26,8 @@ class list(Iterable[T], Generic[T]): class tuple: pass class function: pass class int: pass +class float: pass class str: pass -class bool: pass +class bool(int): pass property = object() # Dummy definition.