From 693e1d8d74ba331c2bd36ff4774c9a4eefe03ab6 Mon Sep 17 00:00:00 2001 From: Ivan Levkivskyi Date: Sat, 15 Jun 2024 15:28:25 +0100 Subject: [PATCH] Add a test case for no overload overlap with self-types (#17388) Fixes https://github.com/python/mypy/issues/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. --- test-data/unit/check-overloading.test | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/test-data/unit/check-overloading.test b/test-data/unit/check-overloading.test index 7bca5cc7b508..bcb775ba5dac 100644 --- a/test-data/unit/check-overloading.test +++ b/test-data/unit/check-overloading.test @@ -4542,6 +4542,23 @@ reveal_type(Child().foo("...")) # N: Revealed type is "builtins.st reveal_type(Child().foo(x)) # N: Revealed type is "Union[__main__.Child, builtins.str]" reveal_type(Child().foo(3).child_only()) # N: Revealed type is "builtins.int" +[case testOverloadAndSelfTypesGenericNoOverlap] +from typing import Generic, TypeVar, Any, overload, Self, Union + +T = TypeVar("T", bound=Any) +class C(Generic[T]): + @overload + def get(self, obj: None) -> Self: ... + @overload + def get(self, obj: Any) -> T: ... + def get(self, obj: Union[Any, None]) -> Union[T, Self]: + return self + +class D(C[int]): ... +d: D +reveal_type(d.get(None)) # N: Revealed type is "__main__.D" +reveal_type(d.get("whatever")) # N: Revealed type is "builtins.int" + [case testOverloadAndClassTypes] from typing import overload, Union, TypeVar, Type