|
6 | 6 | order to get a single Astroid representation
|
7 | 7 | """
|
8 | 8 |
|
| 9 | +import ast |
9 | 10 | import sys
|
10 | 11 | import token
|
11 | 12 | import tokenize
|
12 | 13 | from io import StringIO
|
13 | 14 | from tokenize import TokenInfo, generate_tokens
|
14 | 15 | from typing import (
|
15 |
| - TYPE_CHECKING, |
16 | 16 | Callable,
|
17 | 17 | Dict,
|
18 | 18 | Generator,
|
|
39 | 39 | else:
|
40 | 40 | from typing_extensions import Final
|
41 | 41 |
|
42 |
| -if TYPE_CHECKING: |
43 |
| - import ast |
44 |
| - |
45 | 42 |
|
46 | 43 | REDIRECT: Final[Dict[str, str]] = {
|
47 | 44 | "arguments": "Arguments",
|
@@ -1185,9 +1182,14 @@ def _visit_for(
|
1185 | 1182 | self, cls: Type[T_For], node: Union["ast.For", "ast.AsyncFor"], parent: NodeNG
|
1186 | 1183 | ) -> T_For:
|
1187 | 1184 | """visit a For node by returning a fresh instance of it"""
|
| 1185 | + col_offset = node.col_offset |
| 1186 | + if IS_PYPY and not PY39_PLUS and isinstance(node, ast.AsyncFor) and self._data: |
| 1187 | + # pylint: disable-next=unsubscriptable-object |
| 1188 | + col_offset = self._data[node.lineno - 1].index("async") |
| 1189 | + |
1188 | 1190 | newnode = cls(
|
1189 | 1191 | lineno=node.lineno,
|
1190 |
| - col_offset=node.col_offset, |
| 1192 | + col_offset=col_offset, |
1191 | 1193 | # end_lineno and end_col_offset added in 3.8
|
1192 | 1194 | end_lineno=getattr(node, "end_lineno", None),
|
1193 | 1195 | end_col_offset=getattr(node, "end_col_offset", None),
|
@@ -1292,6 +1294,10 @@ def _visit_functiondef(
|
1292 | 1294 | position=self._get_position_info(node, newnode),
|
1293 | 1295 | doc_node=self.visit(doc_ast_node, newnode),
|
1294 | 1296 | )
|
| 1297 | + if IS_PYPY and PY36 and newnode.position: |
| 1298 | + # PyPy: col_offset in Python 3.6 doesn't include 'async', |
| 1299 | + # use position.col_offset instead. |
| 1300 | + newnode.col_offset = newnode.position.col_offset |
1295 | 1301 | self._fix_doc_node_position(newnode)
|
1296 | 1302 | self._global_names.pop()
|
1297 | 1303 | return newnode
|
@@ -1903,9 +1909,14 @@ def _visit_with(
|
1903 | 1909 | node: Union["ast.With", "ast.AsyncWith"],
|
1904 | 1910 | parent: NodeNG,
|
1905 | 1911 | ) -> T_With:
|
| 1912 | + col_offset = node.col_offset |
| 1913 | + if IS_PYPY and not PY39_PLUS and isinstance(node, ast.AsyncWith) and self._data: |
| 1914 | + # pylint: disable-next=unsubscriptable-object |
| 1915 | + col_offset = self._data[node.lineno - 1].index("async") |
| 1916 | + |
1906 | 1917 | newnode = cls(
|
1907 | 1918 | lineno=node.lineno,
|
1908 |
| - col_offset=node.col_offset, |
| 1919 | + col_offset=col_offset, |
1909 | 1920 | # end_lineno and end_col_offset added in 3.8
|
1910 | 1921 | end_lineno=getattr(node, "end_lineno", None),
|
1911 | 1922 | end_col_offset=getattr(node, "end_col_offset", None),
|
|
0 commit comments