forked from datacamp/pythonwhat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils_ast.py
More file actions
35 lines (31 loc) · 1.27 KB
/
utils_ast.py
File metadata and controls
35 lines (31 loc) · 1.27 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
import ast
from protowhat.Feedback import InstructorError
def wrap_in_module(node):
new_node = ast.Module(node)
if isinstance(node, list):
if len(node) > 0:
new_node.first_token = node[0].first_token
new_node.last_token = node[-1].last_token
else:
pass # do nothing
else:
new_node.first_token = node.first_token
new_node.last_token = node.first_token
return new_node
def assert_ast(state, element, fmt_kwargs):
patt = (
"You are zooming in on the {{part}}, but it is not an AST, so it can't be re-run."
+ " If this error occurred because of ``check_args()``,"
+ "you may have to refer to your argument differently, e.g. `['args', 0]` or `['kwargs', 'a']`. "
+ "Read https://pythonwhat.readthedocs.io/en/latest/articles/checking_function_calls.html#signatures for more info."
)
_err_msg = "SCT fails on solution: "
_err_msg += state.build_message(patt, fmt_kwargs)
# element can also be { 'node': AST }
if isinstance(element, dict):
element = element["node"]
if isinstance(element, ast.AST):
return
if isinstance(element, list) and all([isinstance(el, ast.AST) for el in element]):
return
raise InstructorError(_err_msg)