
Guido van Rossum wrote:
Similar but a bit more complicated, we have a `def find_common_type(types: list[np.dtype | ExtensionDtype]) -> np.dtype | ExtensionDtype:` where we'd like an overload to declare `def find_common_type(types: list[np.dtype]) -> np.dtype`. AFAICT we can't do this without an AnythingBut[X] annotation. For this, I think overload actually works just fine. E.g. from typing import * @overload def foo(a: list[int]) -> int: ... @overload def foo(a: list[str]) -> str: ... @overload def foo(a: list[int|str]) -> int|str: ... def foo(a): ... reveal_type(foo([1,2,3])) # int reveal_type(foo(['a', 'b'])) # str x: list[int|str] = [1, 2, 'a', 'b'] reveal_type(foo(x)) # int|str
Another solution that might work would be to use a type variable. ``` from typing import TypeVar T = TypeVar("T") def foo(a: list[T]) -> T: ... ```