From 9bda65b9d894d64cb4a306776ea6cf8cd2d86acf Mon Sep 17 00:00:00 2001 From: nightcityblade Date: Fri, 21 Aug 2026 23:30:42 +0800 Subject: [PATCH] Fix inspect function predicate return types --- stdlib/@tests/test_cases/check_inspect.py | 23 ++++++++++++++++++----- stdlib/inspect.pyi | 14 +++++++------- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/stdlib/@tests/test_cases/check_inspect.py b/stdlib/@tests/test_cases/check_inspect.py index e7ef3d25cf6e..3723739ac537 100644 --- a/stdlib/@tests/test_cases/check_inspect.py +++ b/stdlib/@tests/test_cases/check_inspect.py @@ -1,8 +1,7 @@ from __future__ import annotations import inspect -from collections.abc import Awaitable, Callable, Coroutine -from types import CoroutineType +from collections.abc import AsyncGenerator, Awaitable, Callable, Coroutine, Generator from typing import Any from typing_extensions import assert_type @@ -17,10 +16,24 @@ def test_iscoroutinefunction_inspect( assert_type(x, Callable[[str, int], Coroutine[str, int, bytes]]) if inspect.iscoroutinefunction(y): - assert_type(y, Callable[[str, int], CoroutineType[Any, Any, bytes]]) + assert_type(y, Callable[[str, int], Coroutine[Any, Any, bytes]]) if inspect.iscoroutinefunction(z): - assert_type(z, Callable[[str, int], CoroutineType[Any, Any, Any]]) + assert_type(z, Callable[[str, int], Coroutine[Any, Any, Any]]) if inspect.iscoroutinefunction(xx): - assert_type(xx, Callable[..., CoroutineType[Any, Any, Any]]) + assert_type(xx, Callable[..., Coroutine[Any, Any, Any]]) + + +def test_isgeneratorfunction_inspect(x: Callable[[str], object], y: object) -> None: + if inspect.isgeneratorfunction(x): + assert_type(x, Callable[[str], Generator[Any, Any, Any]]) + if inspect.isgeneratorfunction(y): + assert_type(y, Callable[..., Generator[Any, Any, Any]]) + + +def test_isasyncgenfunction_inspect(x: Callable[[str], object], y: object) -> None: + if inspect.isasyncgenfunction(x): + assert_type(x, Callable[[str], AsyncGenerator[Any, Any]]) + if inspect.isasyncgenfunction(y): + assert_type(y, Callable[..., AsyncGenerator[Any, Any]]) diff --git a/stdlib/inspect.pyi b/stdlib/inspect.pyi index c3110ce28d8e..9bca21ab2dfd 100644 --- a/stdlib/inspect.pyi +++ b/stdlib/inspect.pyi @@ -232,18 +232,18 @@ if sys.version_info >= (3, 12): @overload def isgeneratorfunction(obj: Callable[..., Generator[Any, Any, Any]]) -> bool: ... @overload -def isgeneratorfunction(obj: Callable[_P, Any]) -> TypeGuard[Callable[_P, GeneratorType[Any, Any, Any]]]: ... +def isgeneratorfunction(obj: Callable[_P, Any]) -> TypeGuard[Callable[_P, Generator[Any, Any, Any]]]: ... @overload -def isgeneratorfunction(obj: object) -> TypeGuard[Callable[..., GeneratorType[Any, Any, Any]]]: ... +def isgeneratorfunction(obj: object) -> TypeGuard[Callable[..., Generator[Any, Any, Any]]]: ... @overload def iscoroutinefunction(obj: Callable[..., Coroutine[Any, Any, Any]]) -> bool: ... @overload -def iscoroutinefunction(obj: Callable[_P, Awaitable[_T]]) -> TypeGuard[Callable[_P, CoroutineType[Any, Any, _T]]]: ... +def iscoroutinefunction(obj: Callable[_P, Awaitable[_T]]) -> TypeGuard[Callable[_P, Coroutine[Any, Any, _T]]]: ... @overload -def iscoroutinefunction(obj: Callable[_P, object]) -> TypeGuard[Callable[_P, CoroutineType[Any, Any, Any]]]: ... +def iscoroutinefunction(obj: Callable[_P, object]) -> TypeGuard[Callable[_P, Coroutine[Any, Any, Any]]]: ... @overload -def iscoroutinefunction(obj: object) -> TypeGuard[Callable[..., CoroutineType[Any, Any, Any]]]: ... +def iscoroutinefunction(obj: object) -> TypeGuard[Callable[..., Coroutine[Any, Any, Any]]]: ... def isgenerator(object: object) -> TypeIs[GeneratorType[object, Never, object]]: ... def iscoroutine(object: object) -> TypeIs[CoroutineType[Any, Any, Any]]: ... @@ -252,9 +252,9 @@ def isawaitable(object: object) -> TypeIs[Awaitable[Any]]: ... @overload def isasyncgenfunction(obj: Callable[..., AsyncGenerator[Any, Any]]) -> bool: ... @overload -def isasyncgenfunction(obj: Callable[_P, Any]) -> TypeGuard[Callable[_P, AsyncGeneratorType[Any, Any]]]: ... +def isasyncgenfunction(obj: Callable[_P, Any]) -> TypeGuard[Callable[_P, AsyncGenerator[Any, Any]]]: ... @overload -def isasyncgenfunction(obj: object) -> TypeGuard[Callable[..., AsyncGeneratorType[Any, Any]]]: ... +def isasyncgenfunction(obj: object) -> TypeGuard[Callable[..., AsyncGenerator[Any, Any]]]: ... @type_check_only class _SupportsSet(Protocol[_T_contra, _V_contra]):