diff --git a/pythonwhat/check_funcs.py b/pythonwhat/check_funcs.py index 709607f7..b3d0cda2 100644 --- a/pythonwhat/check_funcs.py +++ b/pythonwhat/check_funcs.py @@ -422,8 +422,13 @@ def has_equal_ast(incorrect_msg="FMT: Your code does not seem to match the solut """ rep = Reporter.active_reporter - parse_tree = lambda n: ast.dump(n.body[0] if isinstance(n, ast.Module) and len(n.body) == 1 else n) + def parse_tree(n): + # get contents of module.body if only 1 element + crnt = n.body[0] if isinstance(n, ast.Module) and len(n.body) == 1 else n + # remove Expr if it exists + return ast.dump(crnt.value if isinstance(crnt, ast.Expr) else crnt) + stu_rep = parse_tree(state.student_tree) sol_rep = parse_tree(state.solution_tree if not code else ast.parse(code)) diff --git a/tests/test_spec.py b/tests/test_spec.py index cb3aa5f8..4938e9dc 100644 --- a/tests/test_spec.py +++ b/tests/test_spec.py @@ -220,6 +220,15 @@ def test_exact_false_fail(self): self.data["DC_SCT"] = "Ex().has_equal_ast(exact=False)" self.failing_submission() + def test_part_of_method_pass(self): + self.data["DC_SCT"] = """Ex().has_equal_ast(code = 'dict(a = "a")', exact=False)""" + sct_payload = helper.run(self.data) + self.assertTrue(sct_payload['correct']) + + def test_part_of_method_fail(self): + self.data["DC_SCT"] = """Ex().has_equal_ast(code = 'dict(a = "a")', exact=False)""" + self.failing_submission() + class TestOverride(unittest.TestCase): """ This class is used to test overriding w/ correct and incorrect code. Tests are