I don't find this example particularly compelling. `TypeGaurd` is not an instantiable type. A function that returns `TypeGaurd[T]` in actuality returns `bool`:
Return statements within a type guard function should return bool values, and type checkers should verify that all return paths return a bool.
So this issue of overloading seems pretty irrelevant.
I found this confusing too, until I looked further into the overload rules and overlapping with classes. Consider this reduced example:
from typing import Union, overload
class bool: ...
class TypeGuard(bool): ... # Illegal, but pretend it is okay.
def func(x: TypeGuard) -> int: ...
def func(x: bool) -> str: ...
def func(x: Union[TypeGuard, bool]) -> Union[int, str]:
if isinstance(x, TypeGuard):
If I then write:
def do_something() -> bool: return TypeGuard()
b: bool = do_something()
x = func(b)
If we say "it's a `bool`" and attempt to pick the second overload, it will be wrong, because at runtime it's `TypeGuard`, and `func` returns an `int`, not the expected `str`.
See also: https://github.com/python/typing/issues/253#issuecomment-389262904
Typing-sig mailing list -- firstname.lastname@example.org
To unsubscribe send an email to email@example.com
Member address: firstname.lastname@example.org