Skip to content

Commit 9cf9114

Browse files
committed
Proper subtype checks use type promotions
1 parent fc57a76 commit 9cf9114

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

mypy/subtypes.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,10 @@ def visit_deleted_type(self, left: DeletedType) -> bool:
563563

564564
def visit_instance(self, left: Instance) -> bool:
565565
if isinstance(self.right, Instance):
566+
for base in left.type.mro:
567+
if base._promote and is_proper_subtype(base._promote, self.right):
568+
return True
569+
566570
if not left.type.has_base(self.right.type.fullname()):
567571
return False
568572

@@ -579,6 +583,7 @@ def check_argument(leftarg: Type, rightarg: Type, variance: int) -> bool:
579583

580584
return all(check_argument(ta, ra, tvar.variance) for ta, ra, tvar in
581585
zip(left.args, self.right.args, self.right.type.defn.type_vars))
586+
# TODO: TypeType
582587
return False
583588

584589
def visit_type_var(self, left: TypeVarType) -> bool:

test-data/unit/check-unions.test

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,3 +295,15 @@ def foo(a: Union[A, B, C]):
295295
a.y # E: Some element of union has no attribute "y"
296296
b = a # type: Union[B, C]
297297
[builtins fixtures/isinstance.pyi]
298+
299+
[case testSimplifyingUnionAndTypePromotions]
300+
from typing import TypeVar, Union
301+
T = TypeVar('T')
302+
S = TypeVar('S')
303+
def u(x: T, y: S) -> Union[S, T]: pass
304+
305+
reveal_type(u(1, 2.3)) # E: Revealed type is 'builtins.float*'
306+
reveal_type(u(2.3, 1)) # E: Revealed type is 'builtins.float*'
307+
reveal_type(u(False, 2.2)) # E: Revealed type is 'builtins.float*'
308+
reveal_type(u(2.2, False)) # E: Revealed type is 'builtins.float*'
309+
[builtins fixtures/primitives.pyi]

test-data/unit/fixtures/primitives.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class int:
1111
def __add__(self, i: int) -> int: pass
1212
class float: pass
1313
class complex: pass
14-
class bool: pass
14+
class bool(int): pass
1515
class str:
1616
def __add__(self, s: str) -> str: pass
1717
def format(self, *args) -> str: pass

0 commit comments

Comments
 (0)