-
-
Notifications
You must be signed in to change notification settings - Fork 170
/
_ast_helpers.py
59 lines (46 loc) · 1.42 KB
/
_ast_helpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import ast
import warnings
from typing import Container
from typing import Dict
from typing import Set
from typing import Union
from tokenize_rt import Offset
def ast_parse(contents_text: str) -> ast.Module:
# intentionally ignore warnings, we might be fixing warning-ridden syntax
with warnings.catch_warnings():
warnings.simplefilter('ignore')
return ast.parse(contents_text.encode())
def ast_to_offset(node: Union[ast.expr, ast.stmt]) -> Offset:
return Offset(node.lineno, node.col_offset)
def is_name_attr(
node: ast.AST,
imports: Dict[str, Set[str]],
mod: str,
names: Container[str],
) -> bool:
return (
isinstance(node, ast.Name) and
node.id in names and
node.id in imports[mod]
) or (
isinstance(node, ast.Attribute) and
isinstance(node.value, ast.Name) and
node.value.id == mod and
node.attr in names
)
def has_starargs(call: ast.Call) -> bool:
return (
any(k.arg is None for k in call.keywords) or
any(isinstance(a, ast.Starred) for a in call.args)
)
def contains_await(node: ast.AST) -> bool:
for node_ in ast.walk(node):
if isinstance(node_, ast.Await):
return True
else:
return False
def is_async_listcomp(node: ast.ListComp) -> bool:
return (
any(gen.is_async for gen in node.generators) or
contains_await(node)
)