On Sun, Jan 2, 2022 at 8:32 PM <jbrockmendel@gmail.com> wrote:
> Also, the only realistic use case that I have ever heard people ask for is "NOT str", as in "any sequence except str".

Found this thread while trying to address some of the "type: ignore" comments in pandas.  A couple of examples where this feature would be useful:

A bunch of functions are just optimized isinstance checks where we'd like to overload:

```
@overload
def is_float(x: float | np.floating) -> True: ...

@overload
def is_float(x: AnythingBut[float | np.floating]) -> False: ...
```

Maybe this could use TypeGuard:

def is_float(x: object) -> TypeGuard[float|np.floating]:
    ...

(Though perhaps the new, *precise* typeguard that's being discussed in another thread on this list would be better.)
 
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
 
--
--Guido van Rossum (python.org/~guido)