1
1
import enum
2
2
import sys
3
3
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 ,
20
16
)
21
- from typing_extensions import Literal
17
+ from typing import Any , ClassVar , NamedTuple , Optional , Tuple , Type , Union
18
+ from typing_extensions import Literal , TypeGuard
22
19
23
20
#
24
21
# Types and members
@@ -47,12 +44,14 @@ CO_ITERABLE_COROUTINE: int
47
44
CO_ASYNC_GENERATOR : int
48
45
TPFLAGS_IS_ABSTRACT : int
49
46
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 ]]: ...
51
48
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
53
52
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 ] : ...
56
55
57
56
if sys .version_info >= (3 , 8 ):
58
57
def isgeneratorfunction (obj : object ) -> bool : ...
@@ -62,21 +61,21 @@ else:
62
61
def isgeneratorfunction (object : object ) -> bool : ...
63
62
def iscoroutinefunction (object : object ) -> bool : ...
64
63
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 ]] : ...
68
67
69
68
if sys .version_info >= (3 , 8 ):
70
69
def isasyncgenfunction (obj : object ) -> bool : ...
71
70
72
71
else :
73
72
def isasyncgenfunction (object : object ) -> bool : ...
74
73
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 ] : ...
80
79
def isroutine (object : object ) -> bool : ...
81
80
def isabstract (object : object ) -> bool : ...
82
81
def ismethoddescriptor (object : object ) -> bool : ...
@@ -89,15 +88,15 @@ def ismemberdescriptor(object: object) -> bool: ...
89
88
#
90
89
_SourceObjectType = Union [ModuleType , Type [Any ], MethodType , FunctionType , TracebackType , FrameType , CodeType , Callable [..., Any ]]
91
90
92
- def findsource (object : _SourceObjectType ) -> Tuple [List [str ], int ]: ...
91
+ def findsource (object : _SourceObjectType ) -> Tuple [list [str ], int ]: ...
93
92
def getabsfile (object : _SourceObjectType , _filename : Optional [str ] = ...) -> str : ...
94
93
def getblock (lines : Sequence [str ]) -> Sequence [str ]: ...
95
94
def getdoc (object : object ) -> Optional [str ]: ...
96
95
def getcomments (object : object ) -> Optional [str ]: ...
97
96
def getfile (object : _SourceObjectType ) -> str : ...
98
97
def getmodule (object : object , _filename : Optional [str ] = ...) -> Optional [ModuleType ]: ...
99
98
def getsourcefile (object : _SourceObjectType ) -> Optional [str ]: ...
100
- def getsourcelines (object : _SourceObjectType ) -> Tuple [List [str ], int ]: ...
99
+ def getsourcelines (object : _SourceObjectType ) -> Tuple [list [str ], int ]: ...
101
100
def getsource (object : _SourceObjectType ) -> str : ...
102
101
def cleandoc (doc : str ) -> str : ...
103
102
def indentsize (line : str ) -> int : ...
@@ -152,7 +151,7 @@ if sys.version_info >= (3, 10):
152
151
globals : Optional [Mapping [str , Any ]] = ...,
153
152
locals : Optional [Mapping [str , Any ]] = ...,
154
153
eval_str : bool = ...,
155
- ) -> Dict [str , Any ]: ...
154
+ ) -> dict [str , Any ]: ...
156
155
157
156
# The name is the same as the enum's name in CPython
158
157
class _ParameterKind (enum .IntEnum ):
@@ -185,7 +184,7 @@ class Parameter:
185
184
class BoundArguments :
186
185
arguments : OrderedDict [str , Any ]
187
186
args : Tuple [Any , ...]
188
- kwargs : Dict [str , Any ]
187
+ kwargs : dict [str , Any ]
189
188
signature : Signature
190
189
def __init__ (self , signature : Signature , arguments : OrderedDict [str , Any ]) -> None : ...
191
190
def apply_defaults (self ) -> None : ...
@@ -194,54 +193,54 @@ class BoundArguments:
194
193
# Classes and functions
195
194
#
196
195
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
198
197
# 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 ]: ...
202
201
203
202
class ArgSpec (NamedTuple ):
204
- args : List [str ]
203
+ args : list [str ]
205
204
varargs : Optional [str ]
206
205
keywords : Optional [str ]
207
206
defaults : Tuple [Any , ...]
208
207
209
208
class Arguments (NamedTuple ):
210
- args : List [str ]
209
+ args : list [str ]
211
210
varargs : Optional [str ]
212
211
varkw : Optional [str ]
213
212
214
213
def getargs (co : CodeType ) -> Arguments : ...
215
214
def getargspec (func : object ) -> ArgSpec : ...
216
215
217
216
class FullArgSpec (NamedTuple ):
218
- args : List [str ]
217
+ args : list [str ]
219
218
varargs : Optional [str ]
220
219
varkw : Optional [str ]
221
220
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 ]
225
224
226
225
def getfullargspec (func : object ) -> FullArgSpec : ...
227
226
228
227
class ArgInfo (NamedTuple ):
229
- args : List [str ]
228
+ args : list [str ]
230
229
varargs : Optional [str ]
231
230
keywords : Optional [str ]
232
- locals : Dict [str , Any ]
231
+ locals : dict [str , Any ]
233
232
234
233
def getargvalues (frame : FrameType ) -> ArgInfo : ...
235
234
def formatannotation (annotation : object , base_module : Optional [str ] = ...) -> str : ...
236
235
def formatannotationrelativeto (object : object ) -> Callable [[object ], str ]: ...
237
236
def formatargspec (
238
- args : List [str ],
237
+ args : list [str ],
239
238
varargs : Optional [str ] = ...,
240
239
varkw : Optional [str ] = ...,
241
240
defaults : Optional [Tuple [Any , ...]] = ...,
242
241
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 ] = ...,
245
244
formatarg : Callable [[str ], str ] = ...,
246
245
formatvarargs : Callable [[str ], str ] = ...,
247
246
formatvarkw : Callable [[str ], str ] = ...,
@@ -250,23 +249,23 @@ def formatargspec(
250
249
formatannotation : Callable [[Any ], str ] = ...,
251
250
) -> str : ...
252
251
def formatargvalues (
253
- args : List [str ],
252
+ args : list [str ],
254
253
varargs : Optional [str ],
255
254
varkw : Optional [str ],
256
- locals : Optional [Dict [str , Any ]],
255
+ locals : Optional [dict [str , Any ]],
257
256
formatarg : Optional [Callable [[str ], str ]] = ...,
258
257
formatvarargs : Optional [Callable [[str ], str ]] = ...,
259
258
formatvarkw : Optional [Callable [[str ], str ]] = ...,
260
259
formatvalue : Optional [Callable [[Any ], str ]] = ...,
261
260
) -> str : ...
262
261
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 ]: ...
264
263
265
264
class ClosureVars (NamedTuple ):
266
265
nonlocals : Mapping [str , Any ]
267
266
globals : Mapping [str , Any ]
268
267
builtins : Mapping [str , Any ]
269
- unbound : AbstractSet [str ]
268
+ unbound : Set [str ]
270
269
271
270
def getclosurevars (func : Callable [..., Any ]) -> ClosureVars : ...
272
271
def unwrap (func : Callable [..., Any ], * , stop : Optional [Callable [[Any ], Any ]] = ...) -> Any : ...
@@ -279,24 +278,24 @@ class Traceback(NamedTuple):
279
278
filename : str
280
279
lineno : int
281
280
function : str
282
- code_context : Optional [List [str ]]
281
+ code_context : Optional [list [str ]]
283
282
index : Optional [int ] # type: ignore
284
283
285
284
class FrameInfo (NamedTuple ):
286
285
frame : FrameType
287
286
filename : str
288
287
lineno : int
289
288
function : str
290
- code_context : Optional [List [str ]]
289
+ code_context : Optional [list [str ]]
291
290
index : Optional [int ] # type: ignore
292
291
293
292
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 ]: ...
296
295
def getlineno (frame : FrameType ) -> int : ...
297
296
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 ]: ...
300
299
301
300
#
302
301
# Fetching attributes statically
@@ -324,10 +323,10 @@ CORO_SUSPENDED: str
324
323
CORO_CLOSED : str
325
324
# TODO can we be more specific than "object"?
326
325
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 ]: ...
328
327
329
328
# TODO can we be more specific than "object"?
330
- def getcoroutinelocals (coroutine : object ) -> Dict [str , Any ]: ...
329
+ def getcoroutinelocals (coroutine : object ) -> dict [str , Any ]: ...
331
330
332
331
# Create private type alias to avoid conflict with symbol of same
333
332
# name created in Attribute class.
@@ -339,7 +338,7 @@ class Attribute(NamedTuple):
339
338
defining_class : type
340
339
object : _Object
341
340
342
- def classify_class_attrs (cls : type ) -> List [Attribute ]: ...
341
+ def classify_class_attrs (cls : type ) -> list [Attribute ]: ...
343
342
344
343
if sys .version_info >= (3 , 9 ):
345
344
class ClassFoundException (Exception ): ...
0 commit comments