Skip to content

Commit

Permalink
Use TypeIs for various stdlib functions (#11823)
Browse files Browse the repository at this point in the history
  • Loading branch information
JelleZijlstra authored Apr 26, 2024
1 parent 48a68f5 commit 1dff589
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
5 changes: 4 additions & 1 deletion stdlib/builtins.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ from typing_extensions import ( # noqa: Y023
Self,
TypeAlias,
TypeGuard,
TypeIs,
TypeVarTuple,
deprecated,
)
Expand Down Expand Up @@ -1241,7 +1242,7 @@ def any(iterable: Iterable[object], /) -> bool: ...
def ascii(obj: object, /) -> str: ...
def bin(number: int | SupportsIndex, /) -> str: ...
def breakpoint(*args: Any, **kws: Any) -> None: ...
def callable(obj: object, /) -> TypeGuard[Callable[..., object]]: ...
def callable(obj: object, /) -> TypeIs[Callable[..., object]]: ...
def chr(i: int, /) -> str: ...

# We define this here instead of using os.PathLike to avoid import cycle issues.
Expand Down Expand Up @@ -1351,6 +1352,8 @@ class filter(Iterator[_T]):
@overload
def __new__(cls, function: Callable[[_S], TypeGuard[_T]], iterable: Iterable[_S], /) -> Self: ...
@overload
def __new__(cls, function: Callable[[_S], TypeIs[_T]], iterable: Iterable[_S], /) -> Self: ...
@overload
def __new__(cls, function: Callable[[_T], Any], iterable: Iterable[_T], /) -> Self: ...
def __iter__(self) -> Self: ...
def __next__(self) -> _T: ...
Expand Down
38 changes: 19 additions & 19 deletions stdlib/inspect.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ from types import (
WrapperDescriptorType,
)
from typing import Any, ClassVar, Literal, NamedTuple, Protocol, TypeVar, overload
from typing_extensions import ParamSpec, Self, TypeAlias, TypeGuard
from typing_extensions import ParamSpec, Self, TypeAlias, TypeGuard, TypeIs

if sys.version_info >= (3, 11):
__all__ = [
Expand Down Expand Up @@ -192,10 +192,10 @@ if sys.version_info >= (3, 11):
def getmembers_static(object: object, predicate: _GetMembersPredicate | None = None) -> _GetMembersReturn: ...

def getmodulename(path: StrPath) -> str | None: ...
def ismodule(object: object) -> TypeGuard[ModuleType]: ...
def isclass(object: object) -> TypeGuard[type[Any]]: ...
def ismethod(object: object) -> TypeGuard[MethodType]: ...
def isfunction(object: object) -> TypeGuard[FunctionType]: ...
def ismodule(object: object) -> TypeIs[ModuleType]: ...
def isclass(object: object) -> TypeIs[type[Any]]: ...
def ismethod(object: object) -> TypeIs[MethodType]: ...
def isfunction(object: object) -> TypeIs[FunctionType]: ...

if sys.version_info >= (3, 12):
def markcoroutinefunction(func: _F) -> _F: ...
Expand All @@ -214,9 +214,9 @@ def iscoroutinefunction(obj: Callable[_P, Awaitable[_T]]) -> TypeGuard[Callable[
def iscoroutinefunction(obj: Callable[_P, object]) -> TypeGuard[Callable[_P, CoroutineType[Any, Any, Any]]]: ...
@overload
def iscoroutinefunction(obj: object) -> TypeGuard[Callable[..., CoroutineType[Any, Any, Any]]]: ...
def isgenerator(object: object) -> TypeGuard[GeneratorType[Any, Any, Any]]: ...
def iscoroutine(object: object) -> TypeGuard[CoroutineType[Any, Any, Any]]: ...
def isawaitable(object: object) -> TypeGuard[Awaitable[Any]]: ...
def isgenerator(object: object) -> TypeIs[GeneratorType[Any, Any, Any]]: ...
def iscoroutine(object: object) -> TypeIs[CoroutineType[Any, Any, Any]]: ...
def isawaitable(object: object) -> TypeIs[Awaitable[Any]]: ...
@overload
def isasyncgenfunction(obj: Callable[..., AsyncGenerator[Any, Any]]) -> bool: ...
@overload
Expand All @@ -230,18 +230,18 @@ class _SupportsSet(Protocol[_T_cont, _V_cont]):
class _SupportsDelete(Protocol[_T_cont]):
def __delete__(self, instance: _T_cont, /) -> None: ...

def isasyncgen(object: object) -> TypeGuard[AsyncGeneratorType[Any, Any]]: ...
def istraceback(object: object) -> TypeGuard[TracebackType]: ...
def isframe(object: object) -> TypeGuard[FrameType]: ...
def iscode(object: object) -> TypeGuard[CodeType]: ...
def isbuiltin(object: object) -> TypeGuard[BuiltinFunctionType]: ...
def isasyncgen(object: object) -> TypeIs[AsyncGeneratorType[Any, Any]]: ...
def istraceback(object: object) -> TypeIs[TracebackType]: ...
def isframe(object: object) -> TypeIs[FrameType]: ...
def iscode(object: object) -> TypeIs[CodeType]: ...
def isbuiltin(object: object) -> TypeIs[BuiltinFunctionType]: ...

if sys.version_info >= (3, 11):
def ismethodwrapper(object: object) -> TypeGuard[MethodWrapperType]: ...
def ismethodwrapper(object: object) -> TypeIs[MethodWrapperType]: ...

def isroutine(
object: object,
) -> TypeGuard[
) -> TypeIs[
FunctionType
| LambdaType
| MethodType
Expand All @@ -251,11 +251,11 @@ def isroutine(
| MethodDescriptorType
| ClassMethodDescriptorType
]: ...
def ismethoddescriptor(object: object) -> TypeGuard[MethodDescriptorType]: ...
def ismemberdescriptor(object: object) -> TypeGuard[MemberDescriptorType]: ...
def ismethoddescriptor(object: object) -> TypeIs[MethodDescriptorType]: ...
def ismemberdescriptor(object: object) -> TypeIs[MemberDescriptorType]: ...
def isabstract(object: object) -> bool: ...
def isgetsetdescriptor(object: object) -> TypeGuard[GetSetDescriptorType]: ...
def isdatadescriptor(object: object) -> TypeGuard[_SupportsSet[Any, Any] | _SupportsDelete[Any]]: ...
def isgetsetdescriptor(object: object) -> TypeIs[GetSetDescriptorType]: ...
def isdatadescriptor(object: object) -> TypeIs[_SupportsSet[Any, Any] | _SupportsDelete[Any]]: ...

#
# Retrieving source code
Expand Down

0 comments on commit 1dff589

Please sign in to comment.