55import re
66import warnings
77from pathlib import Path
8- from textwrap import dedent
8+ from textwrap import dedent , indent
99from typing import Generator , List , Optional , Tuple , Type
1010
1111from .ansi import isatty , sformat
@@ -222,7 +222,7 @@ def _parse_code(self, call_frame, func_regex, filename) -> Tuple[Optional[ast.AS
222222 func_ast = None
223223 tail_index = call_frame .index
224224 try :
225- func_ast = ast . parse (code , filename = filename ). body [ 0 ]. value
225+ func_ast = self . _wrap_parse (code , filename )
226226 except SyntaxError as e1 :
227227 # if the trailing bracket(s) of the function is/are on a new line eg.
228228 # debug(
@@ -233,7 +233,7 @@ def _parse_code(self, call_frame, func_regex, filename) -> Tuple[Optional[ast.AS
233233 extra_lines = call_frame .code_context [tail_index + 1 :tail_index + extra ]
234234 code = dedent ('' .join (call_lines + extra_lines ))
235235 try :
236- func_ast = ast . parse (code ). body [ 0 ]. value
236+ func_ast = self . _wrap_parse (code , filename )
237237 except SyntaxError :
238238 pass
239239 else :
@@ -253,17 +253,25 @@ def _parse_code(self, call_frame, func_regex, filename) -> Tuple[Optional[ast.AS
253253 code_lines [- 1 ] = code_lines [- 1 ][:- 1 ]
254254 return func_ast , code_lines , lineno
255255
256- @classmethod
257- def _get_offsets (cls , func_ast ):
256+ @staticmethod
257+ def _wrap_parse (code , filename ):
258+ """
259+ async wrapper is required to avoid await calls raising a SyntaxError
260+ """
261+ code = 'async def wrapper():\n ' + indent (code , ' ' )
262+ return ast .parse (code , filename = filename ).body [0 ].body [0 ].value
263+
264+ @staticmethod
265+ def _get_offsets (func_ast ):
258266 for arg in func_ast .args :
259- start_line , start_col = arg .lineno - 1 , arg .col_offset
267+ start_line , start_col = arg .lineno - 2 , arg .col_offset - 1
260268
261269 # horrible hack for http://bugs.python.org/issue31241
262270 if isinstance (arg , (ast .ListComp , ast .GeneratorExp )):
263271 start_col -= 1
264272 yield start_line , start_col
265273 for kw in func_ast .keywords :
266- yield kw .value .lineno - 1 , kw .value .col_offset - len (kw .arg ) - 1
274+ yield kw .value .lineno - 2 , kw .value .col_offset - len (kw .arg ) - 2
267275
268276 def _warn (self , msg , category : Type [Warning ]= RuntimeWarning ):
269277 if self ._show_warnings :
0 commit comments