forked from pixee/codemodder-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
56 lines (46 loc) · 1.52 KB
/
utils.py
File metadata and controls
56 lines (46 loc) · 1.52 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import libcst as cst
from functools import cache
@cache
def list_subclasses(base_kls) -> set[str]:
all_subclasses = set()
stack = [base_kls]
while stack:
kls = stack.pop()
all_subclasses.add(kls)
stack.extend(kls.__subclasses__())
return all_subclasses
def full_qualified_name_from_class(cls) -> str:
if cls.__module__ == "builtins":
return cls.__qualname__
return f"{cls.__module__}.{cls.__qualname__}"
def clean_simplestring(node: cst.SimpleString | str) -> str:
if isinstance(node, str):
return node.strip('"')
return node.raw_value
def true_value(node: cst.Name | cst.SimpleString) -> str | int | bool:
match node:
case cst.SimpleString():
return clean_simplestring(node)
case cst.Name():
val = node.value
if val.lower() == "true":
return True
elif val.lower() == "false":
return False
return val
return ""
def extract_targets_of_assignment(
assignment: cst.AnnAssign | cst.Assign | cst.WithItem | cst.NamedExpr,
) -> list[cst.BaseExpression]:
match assignment:
case cst.AnnAssign():
if assignment.target:
return [assignment.target]
case cst.Assign():
return [t.target for t in assignment.targets]
case cst.NamedExpr():
return [assignment.target]
case cst.WithItem():
if assignment.asname:
return [assignment.asname.name]
return []