While working on python/typeshed#6140 I've noticed that there's a problem with how TypeGuard and bool types match in overload method. Here's a simplified example:
fromtypingimportCallable, TypeVar, Generic, TypeGuard, Any, List, Optional, overload_T=TypeVar('_T')
classfilter(Generic[_T]):
@overloaddef__init__(self, __function: Callable[[object], TypeGuard[_T]]) ->None: ...
@overloaddef__init__(self, __function: Callable[[_T], Any]) ->None: ...
# Demo:a: List[Optional[int]]
defis_int_typeguard(a: object) ->TypeGuard[int]: passdefreturns_bool(a: object) ->bool: pass# Ok:reveal_type(filter(is_int_typeguard)) # N: Revealed type is "ex.filter[builtins.int*]"# Not ok:reveal_type(filter(returns_bool)) # N: Revealed type is "ex.filter[builtins.bool*]"# Expected: Revealed type is "ex.filter[builtins.object*]"For some reason bool matches TypeGuard[_T] and infers _T to be bool. I think that TypeGuard here is more specific type than just bool and this match should not happen.
While working on python/typeshed#6140 I've noticed that there's a problem with how
TypeGuardandbooltypes match inoverloadmethod. Here's a simplified example:For some reason
boolmatchesTypeGuard[_T]and infers_Tto bebool. I think thatTypeGuardhere is more specific type than justbooland this match should not happen.