Skip to content

Commit b2e3c4f

Browse files
improve inspect.pyi (#5473)
- Use TypeGuard for various is* functions (refer to #5406) - Use collections.abc and builtin containers
1 parent e47eff3 commit b2e3c4f

File tree

1 file changed

+60
-61
lines changed

1 file changed

+60
-61
lines changed

stdlib/inspect.pyi

Lines changed: 60 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
11
import enum
22
import sys
33
from collections import OrderedDict
4-
from types import CodeType, FrameType, FunctionType, MethodType, ModuleType, TracebackType
5-
from typing import (
6-
AbstractSet,
7-
Any,
8-
Callable,
9-
ClassVar,
10-
Dict,
11-
Generator,
12-
List,
13-
Mapping,
14-
NamedTuple,
15-
Optional,
16-
Sequence,
17-
Tuple,
18-
Type,
19-
Union,
4+
from collections.abc import Awaitable, Callable, Generator, Mapping, Sequence, Set
5+
from types import (
6+
AsyncGeneratorType,
7+
BuiltinFunctionType,
8+
CodeType,
9+
CoroutineType,
10+
FrameType,
11+
FunctionType,
12+
GeneratorType,
13+
MethodType,
14+
ModuleType,
15+
TracebackType,
2016
)
21-
from typing_extensions import Literal
17+
from typing import Any, ClassVar, NamedTuple, Optional, Tuple, Type, Union
18+
from typing_extensions import Literal, TypeGuard
2219

2320
#
2421
# Types and members
@@ -47,12 +44,14 @@ CO_ITERABLE_COROUTINE: int
4744
CO_ASYNC_GENERATOR: int
4845
TPFLAGS_IS_ABSTRACT: int
4946

50-
def getmembers(object: object, predicate: Optional[Callable[[Any], bool]] = ...) -> List[Tuple[str, Any]]: ...
47+
def getmembers(object: object, predicate: Optional[Callable[[Any], bool]] = ...) -> list[Tuple[str, Any]]: ...
5148
def getmodulename(path: str) -> Optional[str]: ...
52-
def ismodule(object: object) -> bool: ...
49+
def ismodule(object: object) -> TypeGuard[ModuleType]: ...
50+
51+
# TODO: use TypeGuard[Type[Any]] after python/mypy#10486 is resolved
5352
def isclass(object: object) -> bool: ...
54-
def ismethod(object: object) -> bool: ...
55-
def isfunction(object: object) -> bool: ...
53+
def ismethod(object: object) -> TypeGuard[MethodType]: ...
54+
def isfunction(object: object) -> TypeGuard[FunctionType]: ...
5655

5756
if sys.version_info >= (3, 8):
5857
def isgeneratorfunction(obj: object) -> bool: ...
@@ -62,21 +61,21 @@ else:
6261
def isgeneratorfunction(object: object) -> bool: ...
6362
def iscoroutinefunction(object: object) -> bool: ...
6463

65-
def isgenerator(object: object) -> bool: ...
66-
def iscoroutine(object: object) -> bool: ...
67-
def isawaitable(object: object) -> bool: ...
64+
def isgenerator(object: object) -> TypeGuard[GeneratorType[Any, Any, Any]]: ...
65+
def iscoroutine(object: object) -> TypeGuard[CoroutineType]: ...
66+
def isawaitable(object: object) -> TypeGuard[Awaitable[Any]]: ...
6867

6968
if sys.version_info >= (3, 8):
7069
def isasyncgenfunction(obj: object) -> bool: ...
7170

7271
else:
7372
def isasyncgenfunction(object: object) -> bool: ...
7473

75-
def isasyncgen(object: object) -> bool: ...
76-
def istraceback(object: object) -> bool: ...
77-
def isframe(object: object) -> bool: ...
78-
def iscode(object: object) -> bool: ...
79-
def isbuiltin(object: object) -> bool: ...
74+
def isasyncgen(object: object) -> TypeGuard[AsyncGeneratorType[Any, Any]]: ...
75+
def istraceback(object: object) -> TypeGuard[TracebackType]: ...
76+
def isframe(object: object) -> TypeGuard[FrameType]: ...
77+
def iscode(object: object) -> TypeGuard[CodeType]: ...
78+
def isbuiltin(object: object) -> TypeGuard[BuiltinFunctionType]: ...
8079
def isroutine(object: object) -> bool: ...
8180
def isabstract(object: object) -> bool: ...
8281
def ismethoddescriptor(object: object) -> bool: ...
@@ -89,15 +88,15 @@ def ismemberdescriptor(object: object) -> bool: ...
8988
#
9089
_SourceObjectType = Union[ModuleType, Type[Any], MethodType, FunctionType, TracebackType, FrameType, CodeType, Callable[..., Any]]
9190

92-
def findsource(object: _SourceObjectType) -> Tuple[List[str], int]: ...
91+
def findsource(object: _SourceObjectType) -> Tuple[list[str], int]: ...
9392
def getabsfile(object: _SourceObjectType, _filename: Optional[str] = ...) -> str: ...
9493
def getblock(lines: Sequence[str]) -> Sequence[str]: ...
9594
def getdoc(object: object) -> Optional[str]: ...
9695
def getcomments(object: object) -> Optional[str]: ...
9796
def getfile(object: _SourceObjectType) -> str: ...
9897
def getmodule(object: object, _filename: Optional[str] = ...) -> Optional[ModuleType]: ...
9998
def getsourcefile(object: _SourceObjectType) -> Optional[str]: ...
100-
def getsourcelines(object: _SourceObjectType) -> Tuple[List[str], int]: ...
99+
def getsourcelines(object: _SourceObjectType) -> Tuple[list[str], int]: ...
101100
def getsource(object: _SourceObjectType) -> str: ...
102101
def cleandoc(doc: str) -> str: ...
103102
def indentsize(line: str) -> int: ...
@@ -152,7 +151,7 @@ if sys.version_info >= (3, 10):
152151
globals: Optional[Mapping[str, Any]] = ...,
153152
locals: Optional[Mapping[str, Any]] = ...,
154153
eval_str: bool = ...,
155-
) -> Dict[str, Any]: ...
154+
) -> dict[str, Any]: ...
156155

157156
# The name is the same as the enum's name in CPython
158157
class _ParameterKind(enum.IntEnum):
@@ -185,7 +184,7 @@ class Parameter:
185184
class BoundArguments:
186185
arguments: OrderedDict[str, Any]
187186
args: Tuple[Any, ...]
188-
kwargs: Dict[str, Any]
187+
kwargs: dict[str, Any]
189188
signature: Signature
190189
def __init__(self, signature: Signature, arguments: OrderedDict[str, Any]) -> None: ...
191190
def apply_defaults(self) -> None: ...
@@ -194,54 +193,54 @@ class BoundArguments:
194193
# Classes and functions
195194
#
196195

197-
# TODO: The actual return type should be List[_ClassTreeItem] but mypy doesn't
196+
# TODO: The actual return type should be list[_ClassTreeItem] but mypy doesn't
198197
# seem to be supporting this at the moment:
199-
# _ClassTreeItem = Union[List[_ClassTreeItem], Tuple[type, Tuple[type, ...]]]
200-
def getclasstree(classes: List[type], unique: bool = ...) -> List[Any]: ...
201-
def walktree(classes: List[type], children: Dict[Type[Any], List[type]], parent: Optional[Type[Any]]) -> List[Any]: ...
198+
# _ClassTreeItem = Union[list[_ClassTreeItem], Tuple[type, Tuple[type, ...]]]
199+
def getclasstree(classes: list[type], unique: bool = ...) -> list[Any]: ...
200+
def walktree(classes: list[type], children: dict[Type[Any], list[type]], parent: Optional[Type[Any]]) -> list[Any]: ...
202201

203202
class ArgSpec(NamedTuple):
204-
args: List[str]
203+
args: list[str]
205204
varargs: Optional[str]
206205
keywords: Optional[str]
207206
defaults: Tuple[Any, ...]
208207

209208
class Arguments(NamedTuple):
210-
args: List[str]
209+
args: list[str]
211210
varargs: Optional[str]
212211
varkw: Optional[str]
213212

214213
def getargs(co: CodeType) -> Arguments: ...
215214
def getargspec(func: object) -> ArgSpec: ...
216215

217216
class FullArgSpec(NamedTuple):
218-
args: List[str]
217+
args: list[str]
219218
varargs: Optional[str]
220219
varkw: Optional[str]
221220
defaults: Optional[Tuple[Any, ...]]
222-
kwonlyargs: List[str]
223-
kwonlydefaults: Optional[Dict[str, Any]]
224-
annotations: Dict[str, Any]
221+
kwonlyargs: list[str]
222+
kwonlydefaults: Optional[dict[str, Any]]
223+
annotations: dict[str, Any]
225224

226225
def getfullargspec(func: object) -> FullArgSpec: ...
227226

228227
class ArgInfo(NamedTuple):
229-
args: List[str]
228+
args: list[str]
230229
varargs: Optional[str]
231230
keywords: Optional[str]
232-
locals: Dict[str, Any]
231+
locals: dict[str, Any]
233232

234233
def getargvalues(frame: FrameType) -> ArgInfo: ...
235234
def formatannotation(annotation: object, base_module: Optional[str] = ...) -> str: ...
236235
def formatannotationrelativeto(object: object) -> Callable[[object], str]: ...
237236
def formatargspec(
238-
args: List[str],
237+
args: list[str],
239238
varargs: Optional[str] = ...,
240239
varkw: Optional[str] = ...,
241240
defaults: Optional[Tuple[Any, ...]] = ...,
242241
kwonlyargs: Optional[Sequence[str]] = ...,
243-
kwonlydefaults: Optional[Dict[str, Any]] = ...,
244-
annotations: Dict[str, Any] = ...,
242+
kwonlydefaults: Optional[dict[str, Any]] = ...,
243+
annotations: dict[str, Any] = ...,
245244
formatarg: Callable[[str], str] = ...,
246245
formatvarargs: Callable[[str], str] = ...,
247246
formatvarkw: Callable[[str], str] = ...,
@@ -250,23 +249,23 @@ def formatargspec(
250249
formatannotation: Callable[[Any], str] = ...,
251250
) -> str: ...
252251
def formatargvalues(
253-
args: List[str],
252+
args: list[str],
254253
varargs: Optional[str],
255254
varkw: Optional[str],
256-
locals: Optional[Dict[str, Any]],
255+
locals: Optional[dict[str, Any]],
257256
formatarg: Optional[Callable[[str], str]] = ...,
258257
formatvarargs: Optional[Callable[[str], str]] = ...,
259258
formatvarkw: Optional[Callable[[str], str]] = ...,
260259
formatvalue: Optional[Callable[[Any], str]] = ...,
261260
) -> str: ...
262261
def getmro(cls: type) -> Tuple[type, ...]: ...
263-
def getcallargs(__func: Callable[..., Any], *args: Any, **kwds: Any) -> Dict[str, Any]: ...
262+
def getcallargs(__func: Callable[..., Any], *args: Any, **kwds: Any) -> dict[str, Any]: ...
264263

265264
class ClosureVars(NamedTuple):
266265
nonlocals: Mapping[str, Any]
267266
globals: Mapping[str, Any]
268267
builtins: Mapping[str, Any]
269-
unbound: AbstractSet[str]
268+
unbound: Set[str]
270269

271270
def getclosurevars(func: Callable[..., Any]) -> ClosureVars: ...
272271
def unwrap(func: Callable[..., Any], *, stop: Optional[Callable[[Any], Any]] = ...) -> Any: ...
@@ -279,24 +278,24 @@ class Traceback(NamedTuple):
279278
filename: str
280279
lineno: int
281280
function: str
282-
code_context: Optional[List[str]]
281+
code_context: Optional[list[str]]
283282
index: Optional[int] # type: ignore
284283

285284
class FrameInfo(NamedTuple):
286285
frame: FrameType
287286
filename: str
288287
lineno: int
289288
function: str
290-
code_context: Optional[List[str]]
289+
code_context: Optional[list[str]]
291290
index: Optional[int] # type: ignore
292291

293292
def getframeinfo(frame: Union[FrameType, TracebackType], context: int = ...) -> Traceback: ...
294-
def getouterframes(frame: Any, context: int = ...) -> List[FrameInfo]: ...
295-
def getinnerframes(tb: TracebackType, context: int = ...) -> List[FrameInfo]: ...
293+
def getouterframes(frame: Any, context: int = ...) -> list[FrameInfo]: ...
294+
def getinnerframes(tb: TracebackType, context: int = ...) -> list[FrameInfo]: ...
296295
def getlineno(frame: FrameType) -> int: ...
297296
def currentframe() -> Optional[FrameType]: ...
298-
def stack(context: int = ...) -> List[FrameInfo]: ...
299-
def trace(context: int = ...) -> List[FrameInfo]: ...
297+
def stack(context: int = ...) -> list[FrameInfo]: ...
298+
def trace(context: int = ...) -> list[FrameInfo]: ...
300299

301300
#
302301
# Fetching attributes statically
@@ -324,10 +323,10 @@ CORO_SUSPENDED: str
324323
CORO_CLOSED: str
325324
# TODO can we be more specific than "object"?
326325
def getcoroutinestate(coroutine: object) -> str: ...
327-
def getgeneratorlocals(generator: Generator[Any, Any, Any]) -> Dict[str, Any]: ...
326+
def getgeneratorlocals(generator: Generator[Any, Any, Any]) -> dict[str, Any]: ...
328327

329328
# TODO can we be more specific than "object"?
330-
def getcoroutinelocals(coroutine: object) -> Dict[str, Any]: ...
329+
def getcoroutinelocals(coroutine: object) -> dict[str, Any]: ...
331330

332331
# Create private type alias to avoid conflict with symbol of same
333332
# name created in Attribute class.
@@ -339,7 +338,7 @@ class Attribute(NamedTuple):
339338
defining_class: type
340339
object: _Object
341340

342-
def classify_class_attrs(cls: type) -> List[Attribute]: ...
341+
def classify_class_attrs(cls: type) -> list[Attribute]: ...
343342

344343
if sys.version_info >= (3, 9):
345344
class ClassFoundException(Exception): ...

0 commit comments

Comments
 (0)