@@ -1444,52 +1444,52 @@ class Child4(Parent):
1444
1444
[case testOverloadWithIncompatibleMethodOverrideAndImplementation]
1445
1445
from typing import overload, Union, Any
1446
1446
1447
- class StrSub: pass
1447
+ class Sub: pass
1448
+ class A: pass
1449
+ class B: pass
1448
1450
1449
1451
class ParentWithTypedImpl:
1450
1452
@overload
1451
- def f(self, arg: int ) -> int : ...
1453
+ def f(self, arg: A ) -> A : ...
1452
1454
@overload
1453
- def f(self, arg: str ) -> str : ...
1454
- def f(self, arg: Union[int, str ]) -> Union[int, str ]: ...
1455
+ def f(self, arg: B ) -> B : ...
1456
+ def f(self, arg: Union[A, B ]) -> Union[A, B ]: ...
1455
1457
1456
1458
class Child1(ParentWithTypedImpl):
1457
1459
@overload # E: Signature of "f" incompatible with supertype "ParentWithTypedImpl"
1458
- def f(self, arg: int ) -> int : ...
1460
+ def f(self, arg: A ) -> A : ...
1459
1461
@overload
1460
- def f(self, arg: StrSub ) -> str : ...
1461
- def f(self, arg: Union[int, StrSub ]) -> Union[int, str ]: ...
1462
+ def f(self, arg: Sub ) -> B : ...
1463
+ def f(self, arg: Union[A, Sub ]) -> Union[A, B ]: ...
1462
1464
1463
1465
class Child2(ParentWithTypedImpl):
1464
1466
@overload # E: Signature of "f" incompatible with supertype "ParentWithTypedImpl"
1465
- def f(self, arg: int ) -> int : ...
1467
+ def f(self, arg: A ) -> A : ...
1466
1468
@overload
1467
- def f(self, arg: StrSub ) -> str : ...
1469
+ def f(self, arg: Sub ) -> B : ...
1468
1470
def f(self, arg: Any) -> Any: ...
1469
1471
1470
1472
class ParentWithDynamicImpl:
1471
1473
@overload
1472
- def f(self, arg: int ) -> int : ...
1474
+ def f(self, arg: A ) -> A : ...
1473
1475
@overload
1474
- def f(self, arg: str ) -> str : ...
1476
+ def f(self, arg: B ) -> B : ...
1475
1477
def f(self, arg: Any) -> Any: ...
1476
1478
1477
1479
class Child3(ParentWithDynamicImpl):
1478
1480
@overload # E: Signature of "f" incompatible with supertype "ParentWithDynamicImpl"
1479
- def f(self, arg: int ) -> int : ...
1481
+ def f(self, arg: A ) -> A : ...
1480
1482
@overload
1481
- def f(self, arg: StrSub ) -> str : ...
1482
- def f(self, arg: Union[int, StrSub ]) -> Union[int, str ]: ...
1483
+ def f(self, arg: Sub ) -> B : ...
1484
+ def f(self, arg: Union[A, Sub ]) -> Union[A, B ]: ...
1483
1485
1484
1486
class Child4(ParentWithDynamicImpl):
1485
1487
@overload # E: Signature of "f" incompatible with supertype "ParentWithDynamicImpl"
1486
- def f(self, arg: int ) -> int : ...
1488
+ def f(self, arg: A ) -> A : ...
1487
1489
@overload
1488
- def f(self, arg: StrSub ) -> str : ...
1490
+ def f(self, arg: Sub ) -> B : ...
1489
1491
def f(self, arg: Any) -> Any: ...
1490
1492
1491
- [builtins fixtures/tuple.pyi]
1492
-
1493
1493
[case testOverloadInferUnionReturnBasic]
1494
1494
from typing import overload, Union
1495
1495
@@ -1515,8 +1515,6 @@ def f2(x): ...
1515
1515
1516
1516
reveal_type(f2(arg1)) # E: Revealed type is '__main__.B'
1517
1517
1518
- [builtins fixtures/tuple.pyi]
1519
-
1520
1518
[case testOverloadInferUnionReturnMultipleArguments]
1521
1519
from typing import overload, Union
1522
1520
@@ -1544,7 +1542,6 @@ def f2(x, y): ...
1544
1542
reveal_type(f2(arg1, arg1))
1545
1543
reveal_type(f2(arg1, C()))
1546
1544
1547
- [builtins fixtures/tuple.pyi]
1548
1545
[out]
1549
1546
main:16: error: Revealed type is '__main__.B'
1550
1547
main:16: error: Argument 1 to "f1" has incompatible type "Union[A, C]"; expected "A"
@@ -1553,6 +1550,24 @@ main:24: error: Revealed type is 'Union[__main__.B, __main__.D]'
1553
1550
main:24: error: Argument 2 to "f2" has incompatible type "Union[A, C]"; expected "C"
1554
1551
main:25: error: Revealed type is 'Union[__main__.B, __main__.D]'
1555
1552
1553
+ [case testOverloadInferUnionSkipIfParameterNamesAreDifferent]
1554
+ from typing import overload, Union
1555
+
1556
+ class A: ...
1557
+ class B: ...
1558
+ class C: ...
1559
+
1560
+ @overload
1561
+ def f(x: A) -> B: ...
1562
+ @overload
1563
+ def f(y: B) -> C: ...
1564
+ def f(x): ...
1565
+
1566
+ x: Union[A, B]
1567
+ reveal_type(f(A())) # E: Revealed type is '__main__.B'
1568
+ reveal_type(f(B())) # E: Revealed type is '__main__.C'
1569
+ f(x) # E: Argument 1 to "f" has incompatible type "Union[A, B]"; expected "A"
1570
+
1556
1571
[case testOverloadInferUnionReturnFunctionsWithKwargs]
1557
1572
from typing import overload, Union, Optional
1558
1573
@@ -1584,3 +1599,50 @@ main:19: error: Revealed type is '__main__.C'
1584
1599
main:19: error: Argument 2 to "f" has incompatible type "Union[B, C]"; expected "Optional[B]"
1585
1600
main:21: error: Revealed type is '__main__.A'
1586
1601
1602
+ [case testOverloadingInferUnionReturnWithTypevarWithValueRestriction]
1603
+ from typing import overload, Union, TypeVar, Generic
1604
+
1605
+ class A: pass
1606
+ class B: pass
1607
+ class C: pass
1608
+
1609
+ T = TypeVar('T', B, C)
1610
+
1611
+ class Wrapper(Generic[T]):
1612
+ @overload
1613
+ def f(self, x: T) -> B: ...
1614
+
1615
+ @overload
1616
+ def f(self, x: A) -> C: ...
1617
+
1618
+ def f(self, x): ...
1619
+
1620
+ obj: Wrapper[B] = Wrapper()
1621
+ x: Union[A, B]
1622
+
1623
+ reveal_type(obj.f(A())) # E: Revealed type is '__main__.C'
1624
+ reveal_type(obj.f(B())) # E: Revealed type is '__main__.B'
1625
+ reveal_type(obj.f(x)) # E: Revealed type is 'Union[__main__.B, __main__.C]'
1626
+
1627
+ [case testOverloadingInferUnionReturnWithTypevarReturn]
1628
+ from typing import overload, Union, TypeVar, Generic
1629
+
1630
+ T = TypeVar('T')
1631
+
1632
+ class Wrapper1(Generic[T]): pass
1633
+ class Wrapper2(Generic[T]): pass
1634
+ class A: pass
1635
+ class B: pass
1636
+
1637
+ @overload
1638
+ def f(x: Wrapper1[T]) -> T: ...
1639
+ @overload
1640
+ def f(x: Wrapper2[T]) -> T: ...
1641
+ def f(x): ...
1642
+
1643
+ obj1: Union[Wrapper1[A], Wrapper2[A]]
1644
+ reveal_type(f(obj1)) # E: Revealed type is '__main__.A'
1645
+
1646
+ obj2: Union[Wrapper1[A], Wrapper2[B]]
1647
+ reveal_type(f(obj2)) # E: Revealed type is 'Union[__main__.A, __main__.B]'
1648
+
0 commit comments