-
-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathutil.py
More file actions
42 lines (31 loc) · 1.16 KB
/
Copy pathutil.py
File metadata and controls
42 lines (31 loc) · 1.16 KB
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
import python_minifier.ast_compat as ast
def is_constant_node(node, types):
"""
Is a node one of the specified node types
A node type may be an actual ast class or a tuple of many.
If types includes a specific Constant type (Str, Bytes, Num etc),
returns true for Constant nodes of the correct type.
:type node: ast.AST
:param types:
:rtype: bool
"""
if not isinstance(types, tuple):
types = (types,)
for node_type in types:
assert not isinstance(node_type, str)
if isinstance(node, types):
return True
if isinstance(node, ast.Constant):
if type(node.value) in [type(None), type(True), type(False)]:
return ast.NameConstant in types
elif isinstance(node.value, (int, float, complex)):
return ast.Num in types
elif isinstance(node.value, str):
return ast.Str in types
elif isinstance(node.value, bytes):
return ast.Bytes in types
elif node.value == Ellipsis:
return ast.Ellipsis in types
else:
raise RuntimeError('Unknown Constant value %r' % type(node.value))
return False