MCPcopy Create free account
hub / github.com/scottrogowski/code2flow / get_call_from_func_element

Function get_call_from_func_element

code2flow/python.py:9–38  ·  view source on GitHub ↗

Given a python ast that represents a function call, clear and create our generic Call object. Some calls have no chance at resolution (e.g. array[2](param)) so we return nothing instead. :param func ast: :rtype: Call|None

(func)

Source from the content-addressed store, hash-verified

7
8
9def get_call_from_func_element(func):
10 """
11 Given a python ast that represents a function call, clear and create our
12 generic Call object. Some calls have no chance at resolution (e.g. array[2](param))
13 so we return nothing instead.
14
15 :param func ast:
16 :rtype: Call|None
17 """
18 assert type(func) in (ast.Attribute, ast.Name, ast.Subscript, ast.Call)
19 if type(func) == ast.Attribute:
20 owner_token = []
21 val = func.value
22 while True:
23 try:
24 owner_token.append(getattr(val, 'attr', val.id))
25 except AttributeError:
26 pass
27 val = getattr(val, 'value', None)
28 if not val:
29 break
30 if owner_token:
31 owner_token = djoin(*reversed(owner_token))
32 else:
33 owner_token = OWNER_CONST.UNKNOWN_VAR
34 return Call(token=func.attr, line_number=func.lineno, owner_token=owner_token)
35 if type(func) == ast.Name:
36 return Call(token=func.id, line_number=func.lineno)
37 if type(func) in (ast.Subscript, ast.Call):
38 return None
39
40
41def make_calls(lines):

Callers 2

make_callsFunction · 0.70
process_assignFunction · 0.70

Calls 2

djoinFunction · 0.85
CallClass · 0.85

Tested by

no test coverage detected