Skip to content

Commit 240fd75

Browse files
[inspect] Fix inspect function predicate return types (#16282)
1 parent 87e90bf commit 240fd75

2 files changed

Lines changed: 25 additions & 12 deletions

File tree

stdlib/@tests/test_cases/check_inspect.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
from __future__ import annotations
22

33
import inspect
4-
from collections.abc import Awaitable, Callable, Coroutine
5-
from types import CoroutineType
4+
from collections.abc import AsyncGenerator, Awaitable, Callable, Coroutine, Generator
65
from typing import Any
76
from typing_extensions import assert_type
87

@@ -17,10 +16,24 @@ def test_iscoroutinefunction_inspect(
1716
assert_type(x, Callable[[str, int], Coroutine[str, int, bytes]])
1817

1918
if inspect.iscoroutinefunction(y):
20-
assert_type(y, Callable[[str, int], CoroutineType[Any, Any, bytes]])
19+
assert_type(y, Callable[[str, int], Coroutine[Any, Any, bytes]])
2120

2221
if inspect.iscoroutinefunction(z):
23-
assert_type(z, Callable[[str, int], CoroutineType[Any, Any, Any]])
22+
assert_type(z, Callable[[str, int], Coroutine[Any, Any, Any]])
2423

2524
if inspect.iscoroutinefunction(xx):
26-
assert_type(xx, Callable[..., CoroutineType[Any, Any, Any]])
25+
assert_type(xx, Callable[..., Coroutine[Any, Any, Any]])
26+
27+
28+
def test_isgeneratorfunction_inspect(x: Callable[[str], object], y: object) -> None:
29+
if inspect.isgeneratorfunction(x):
30+
assert_type(x, Callable[[str], Generator[Any, Any, Any]])
31+
if inspect.isgeneratorfunction(y):
32+
assert_type(y, Callable[..., Generator[Any, Any, Any]])
33+
34+
35+
def test_isasyncgenfunction_inspect(x: Callable[[str], object], y: object) -> None:
36+
if inspect.isasyncgenfunction(x):
37+
assert_type(x, Callable[[str], AsyncGenerator[Any, Any]])
38+
if inspect.isasyncgenfunction(y):
39+
assert_type(y, Callable[..., AsyncGenerator[Any, Any]])

stdlib/inspect.pyi

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -232,18 +232,18 @@ if sys.version_info >= (3, 12):
232232
@overload
233233
def isgeneratorfunction(obj: Callable[..., Generator[Any, Any, Any]]) -> bool: ...
234234
@overload
235-
def isgeneratorfunction(obj: Callable[_P, Any]) -> TypeGuard[Callable[_P, GeneratorType[Any, Any, Any]]]: ...
235+
def isgeneratorfunction(obj: Callable[_P, Any]) -> TypeGuard[Callable[_P, Generator[Any, Any, Any]]]: ...
236236
@overload
237-
def isgeneratorfunction(obj: object) -> TypeGuard[Callable[..., GeneratorType[Any, Any, Any]]]: ...
237+
def isgeneratorfunction(obj: object) -> TypeGuard[Callable[..., Generator[Any, Any, Any]]]: ...
238238

239239
@overload
240240
def iscoroutinefunction(obj: Callable[..., Coroutine[Any, Any, Any]]) -> bool: ...
241241
@overload
242-
def iscoroutinefunction(obj: Callable[_P, Awaitable[_T]]) -> TypeGuard[Callable[_P, CoroutineType[Any, Any, _T]]]: ...
242+
def iscoroutinefunction(obj: Callable[_P, Awaitable[_T]]) -> TypeGuard[Callable[_P, Coroutine[Any, Any, _T]]]: ...
243243
@overload
244-
def iscoroutinefunction(obj: Callable[_P, object]) -> TypeGuard[Callable[_P, CoroutineType[Any, Any, Any]]]: ...
244+
def iscoroutinefunction(obj: Callable[_P, object]) -> TypeGuard[Callable[_P, Coroutine[Any, Any, Any]]]: ...
245245
@overload
246-
def iscoroutinefunction(obj: object) -> TypeGuard[Callable[..., CoroutineType[Any, Any, Any]]]: ...
246+
def iscoroutinefunction(obj: object) -> TypeGuard[Callable[..., Coroutine[Any, Any, Any]]]: ...
247247

248248
def isgenerator(object: object) -> TypeIs[GeneratorType[object, Never, object]]: ...
249249
def iscoroutine(object: object) -> TypeIs[CoroutineType[Any, Any, Any]]: ...
@@ -252,9 +252,9 @@ def isawaitable(object: object) -> TypeIs[Awaitable[Any]]: ...
252252
@overload
253253
def isasyncgenfunction(obj: Callable[..., AsyncGenerator[Any, Any]]) -> bool: ...
254254
@overload
255-
def isasyncgenfunction(obj: Callable[_P, Any]) -> TypeGuard[Callable[_P, AsyncGeneratorType[Any, Any]]]: ...
255+
def isasyncgenfunction(obj: Callable[_P, Any]) -> TypeGuard[Callable[_P, AsyncGenerator[Any, Any]]]: ...
256256
@overload
257-
def isasyncgenfunction(obj: object) -> TypeGuard[Callable[..., AsyncGeneratorType[Any, Any]]]: ...
257+
def isasyncgenfunction(obj: object) -> TypeGuard[Callable[..., AsyncGenerator[Any, Any]]]: ...
258258

259259
@type_check_only
260260
class _SupportsSet(Protocol[_T_contra, _V_contra]):

0 commit comments

Comments
 (0)