Skip to content

Commit 1dff589

Browse files
Use TypeIs for various stdlib functions (#11823)
1 parent 48a68f5 commit 1dff589

File tree

2 files changed

+23
-20
lines changed

2 files changed

+23
-20
lines changed

stdlib/builtins.pyi

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ from typing_extensions import ( # noqa: Y023
6666
Self,
6767
TypeAlias,
6868
TypeGuard,
69+
TypeIs,
6970
TypeVarTuple,
7071
deprecated,
7172
)
@@ -1241,7 +1242,7 @@ def any(iterable: Iterable[object], /) -> bool: ...
12411242
def ascii(obj: object, /) -> str: ...
12421243
def bin(number: int | SupportsIndex, /) -> str: ...
12431244
def breakpoint(*args: Any, **kws: Any) -> None: ...
1244-
def callable(obj: object, /) -> TypeGuard[Callable[..., object]]: ...
1245+
def callable(obj: object, /) -> TypeIs[Callable[..., object]]: ...
12451246
def chr(i: int, /) -> str: ...
12461247

12471248
# We define this here instead of using os.PathLike to avoid import cycle issues.
@@ -1351,6 +1352,8 @@ class filter(Iterator[_T]):
13511352
@overload
13521353
def __new__(cls, function: Callable[[_S], TypeGuard[_T]], iterable: Iterable[_S], /) -> Self: ...
13531354
@overload
1355+
def __new__(cls, function: Callable[[_S], TypeIs[_T]], iterable: Iterable[_S], /) -> Self: ...
1356+
@overload
13541357
def __new__(cls, function: Callable[[_T], Any], iterable: Iterable[_T], /) -> Self: ...
13551358
def __iter__(self) -> Self: ...
13561359
def __next__(self) -> _T: ...

stdlib/inspect.pyi

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ from types import (
2626
WrapperDescriptorType,
2727
)
2828
from typing import Any, ClassVar, Literal, NamedTuple, Protocol, TypeVar, overload
29-
from typing_extensions import ParamSpec, Self, TypeAlias, TypeGuard
29+
from typing_extensions import ParamSpec, Self, TypeAlias, TypeGuard, TypeIs
3030

3131
if sys.version_info >= (3, 11):
3232
__all__ = [
@@ -192,10 +192,10 @@ if sys.version_info >= (3, 11):
192192
def getmembers_static(object: object, predicate: _GetMembersPredicate | None = None) -> _GetMembersReturn: ...
193193

194194
def getmodulename(path: StrPath) -> str | None: ...
195-
def ismodule(object: object) -> TypeGuard[ModuleType]: ...
196-
def isclass(object: object) -> TypeGuard[type[Any]]: ...
197-
def ismethod(object: object) -> TypeGuard[MethodType]: ...
198-
def isfunction(object: object) -> TypeGuard[FunctionType]: ...
195+
def ismodule(object: object) -> TypeIs[ModuleType]: ...
196+
def isclass(object: object) -> TypeIs[type[Any]]: ...
197+
def ismethod(object: object) -> TypeIs[MethodType]: ...
198+
def isfunction(object: object) -> TypeIs[FunctionType]: ...
199199

200200
if sys.version_info >= (3, 12):
201201
def markcoroutinefunction(func: _F) -> _F: ...
@@ -214,9 +214,9 @@ def iscoroutinefunction(obj: Callable[_P, Awaitable[_T]]) -> TypeGuard[Callable[
214214
def iscoroutinefunction(obj: Callable[_P, object]) -> TypeGuard[Callable[_P, CoroutineType[Any, Any, Any]]]: ...
215215
@overload
216216
def iscoroutinefunction(obj: object) -> TypeGuard[Callable[..., CoroutineType[Any, Any, Any]]]: ...
217-
def isgenerator(object: object) -> TypeGuard[GeneratorType[Any, Any, Any]]: ...
218-
def iscoroutine(object: object) -> TypeGuard[CoroutineType[Any, Any, Any]]: ...
219-
def isawaitable(object: object) -> TypeGuard[Awaitable[Any]]: ...
217+
def isgenerator(object: object) -> TypeIs[GeneratorType[Any, Any, Any]]: ...
218+
def iscoroutine(object: object) -> TypeIs[CoroutineType[Any, Any, Any]]: ...
219+
def isawaitable(object: object) -> TypeIs[Awaitable[Any]]: ...
220220
@overload
221221
def isasyncgenfunction(obj: Callable[..., AsyncGenerator[Any, Any]]) -> bool: ...
222222
@overload
@@ -230,18 +230,18 @@ class _SupportsSet(Protocol[_T_cont, _V_cont]):
230230
class _SupportsDelete(Protocol[_T_cont]):
231231
def __delete__(self, instance: _T_cont, /) -> None: ...
232232

233-
def isasyncgen(object: object) -> TypeGuard[AsyncGeneratorType[Any, Any]]: ...
234-
def istraceback(object: object) -> TypeGuard[TracebackType]: ...
235-
def isframe(object: object) -> TypeGuard[FrameType]: ...
236-
def iscode(object: object) -> TypeGuard[CodeType]: ...
237-
def isbuiltin(object: object) -> TypeGuard[BuiltinFunctionType]: ...
233+
def isasyncgen(object: object) -> TypeIs[AsyncGeneratorType[Any, Any]]: ...
234+
def istraceback(object: object) -> TypeIs[TracebackType]: ...
235+
def isframe(object: object) -> TypeIs[FrameType]: ...
236+
def iscode(object: object) -> TypeIs[CodeType]: ...
237+
def isbuiltin(object: object) -> TypeIs[BuiltinFunctionType]: ...
238238

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

242242
def isroutine(
243243
object: object,
244-
) -> TypeGuard[
244+
) -> TypeIs[
245245
FunctionType
246246
| LambdaType
247247
| MethodType
@@ -251,11 +251,11 @@ def isroutine(
251251
| MethodDescriptorType
252252
| ClassMethodDescriptorType
253253
]: ...
254-
def ismethoddescriptor(object: object) -> TypeGuard[MethodDescriptorType]: ...
255-
def ismemberdescriptor(object: object) -> TypeGuard[MemberDescriptorType]: ...
254+
def ismethoddescriptor(object: object) -> TypeIs[MethodDescriptorType]: ...
255+
def ismemberdescriptor(object: object) -> TypeIs[MemberDescriptorType]: ...
256256
def isabstract(object: object) -> bool: ...
257-
def isgetsetdescriptor(object: object) -> TypeGuard[GetSetDescriptorType]: ...
258-
def isdatadescriptor(object: object) -> TypeGuard[_SupportsSet[Any, Any] | _SupportsDelete[Any]]: ...
257+
def isgetsetdescriptor(object: object) -> TypeIs[GetSetDescriptorType]: ...
258+
def isdatadescriptor(object: object) -> TypeIs[_SupportsSet[Any, Any] | _SupportsDelete[Any]]: ...
259259

260260
#
261261
# Retrieving source code

0 commit comments

Comments
 (0)