Skip to content

Commit 693e1d8

Browse files
authored
Add a test case for no overload overlap with self-types (python#17388)
Fixes python#14641 I don't think we have a test case for this, and although it is a bit in a grey area (because of `x: Any = None`), I think we should allow this.
1 parent 740d39e commit 693e1d8

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

test-data/unit/check-overloading.test

+17
Original file line numberDiff line numberDiff line change
@@ -4542,6 +4542,23 @@ reveal_type(Child().foo("...")) # N: Revealed type is "builtins.st
45424542
reveal_type(Child().foo(x)) # N: Revealed type is "Union[__main__.Child, builtins.str]"
45434543
reveal_type(Child().foo(3).child_only()) # N: Revealed type is "builtins.int"
45444544

4545+
[case testOverloadAndSelfTypesGenericNoOverlap]
4546+
from typing import Generic, TypeVar, Any, overload, Self, Union
4547+
4548+
T = TypeVar("T", bound=Any)
4549+
class C(Generic[T]):
4550+
@overload
4551+
def get(self, obj: None) -> Self: ...
4552+
@overload
4553+
def get(self, obj: Any) -> T: ...
4554+
def get(self, obj: Union[Any, None]) -> Union[T, Self]:
4555+
return self
4556+
4557+
class D(C[int]): ...
4558+
d: D
4559+
reveal_type(d.get(None)) # N: Revealed type is "__main__.D"
4560+
reveal_type(d.get("whatever")) # N: Revealed type is "builtins.int"
4561+
45454562
[case testOverloadAndClassTypes]
45464563
from typing import overload, Union, TypeVar, Type
45474564

0 commit comments

Comments
 (0)