6
6
import tokenize
7
7
import warnings
8
8
from bisect import bisect_right
9
+ from types import CodeType
9
10
from types import FrameType
10
11
from typing import Iterator
11
12
from typing import List
17
18
import py
18
19
19
20
from _pytest .compat import overload
21
+ from _pytest .compat import TYPE_CHECKING
22
+
23
+ if TYPE_CHECKING :
24
+ from typing_extensions import Literal
20
25
21
26
22
27
class Source :
@@ -120,7 +125,7 @@ def getstatement(self, lineno: int) -> "Source":
120
125
start , end = self .getstatementrange (lineno )
121
126
return self [start :end ]
122
127
123
- def getstatementrange (self , lineno : int ):
128
+ def getstatementrange (self , lineno : int ) -> Tuple [ int , int ] :
124
129
""" return (start, end) tuple which spans the minimal
125
130
statement region which containing the given lineno.
126
131
"""
@@ -158,14 +163,36 @@ def isparseable(self, deindent: bool = True) -> bool:
158
163
def __str__ (self ) -> str :
159
164
return "\n " .join (self .lines )
160
165
166
+ @overload
161
167
def compile (
162
168
self ,
163
- filename = None ,
164
- mode = "exec" ,
169
+ filename : Optional [str ] = ...,
170
+ mode : str = ...,
171
+ flag : "Literal[0]" = ...,
172
+ dont_inherit : int = ...,
173
+ _genframe : Optional [FrameType ] = ...,
174
+ ) -> CodeType :
175
+ raise NotImplementedError ()
176
+
177
+ @overload # noqa: F811
178
+ def compile ( # noqa: F811
179
+ self ,
180
+ filename : Optional [str ] = ...,
181
+ mode : str = ...,
182
+ flag : int = ...,
183
+ dont_inherit : int = ...,
184
+ _genframe : Optional [FrameType ] = ...,
185
+ ) -> Union [CodeType , ast .AST ]:
186
+ raise NotImplementedError ()
187
+
188
+ def compile ( # noqa: F811
189
+ self ,
190
+ filename : Optional [str ] = None ,
191
+ mode : str = "exec" ,
165
192
flag : int = 0 ,
166
193
dont_inherit : int = 0 ,
167
194
_genframe : Optional [FrameType ] = None ,
168
- ):
195
+ ) -> Union [ CodeType , ast . AST ] :
169
196
""" return compiled code object. if filename is None
170
197
invent an artificial filename which displays
171
198
the source/line position of the caller frame.
@@ -196,7 +223,9 @@ def compile(
196
223
raise newex
197
224
else :
198
225
if flag & ast .PyCF_ONLY_AST :
226
+ assert isinstance (co , ast .AST )
199
227
return co
228
+ assert isinstance (co , CodeType )
200
229
lines = [(x + "\n " ) for x in self .lines ]
201
230
# Type ignored because linecache.cache is private.
202
231
linecache .cache [filename ] = (1 , None , lines , filename ) # type: ignore
@@ -208,22 +237,52 @@ def compile(
208
237
#
209
238
210
239
211
- def compile_ (source , filename = None , mode = "exec" , flags : int = 0 , dont_inherit : int = 0 ):
240
+ @overload
241
+ def compile_ (
242
+ source : Union [str , bytes , ast .mod , ast .AST ],
243
+ filename : Optional [str ] = ...,
244
+ mode : str = ...,
245
+ flags : "Literal[0]" = ...,
246
+ dont_inherit : int = ...,
247
+ ) -> CodeType :
248
+ raise NotImplementedError ()
249
+
250
+
251
+ @overload # noqa: F811
252
+ def compile_ ( # noqa: F811
253
+ source : Union [str , bytes , ast .mod , ast .AST ],
254
+ filename : Optional [str ] = ...,
255
+ mode : str = ...,
256
+ flags : int = ...,
257
+ dont_inherit : int = ...,
258
+ ) -> Union [CodeType , ast .AST ]:
259
+ raise NotImplementedError ()
260
+
261
+
262
+ def compile_ ( # noqa: F811
263
+ source : Union [str , bytes , ast .mod , ast .AST ],
264
+ filename : Optional [str ] = None ,
265
+ mode : str = "exec" ,
266
+ flags : int = 0 ,
267
+ dont_inherit : int = 0 ,
268
+ ) -> Union [CodeType , ast .AST ]:
212
269
""" compile the given source to a raw code object,
213
270
and maintain an internal cache which allows later
214
271
retrieval of the source code for the code object
215
272
and any recursively created code objects.
216
273
"""
217
274
if isinstance (source , ast .AST ):
218
275
# XXX should Source support having AST?
219
- return compile (source , filename , mode , flags , dont_inherit )
276
+ assert filename is not None
277
+ co = compile (source , filename , mode , flags , dont_inherit )
278
+ assert isinstance (co , (CodeType , ast .AST ))
279
+ return co
220
280
_genframe = sys ._getframe (1 ) # the caller
221
281
s = Source (source )
222
- co = s .compile (filename , mode , flags , _genframe = _genframe )
223
- return co
282
+ return s .compile (filename , mode , flags , _genframe = _genframe )
224
283
225
284
226
- def getfslineno (obj ):
285
+ def getfslineno (obj ) -> Tuple [ Union [ str , py . path . local ], int ] :
227
286
""" Return source location (path, lineno) for the given object.
228
287
If the source cannot be determined return ("", -1).
229
288
0 commit comments