pythonwhat
pythonwhat copied to clipboard
Fix handling of missing orelse block
in most AST nodes, when a missing attribute,
- usually only corresponds to a single node, it is None
- usually corresponds to one or more nodes, it is an empty list
However, the orelse node lives a double life. Sometimes it is a list of expressions, as in...
if True: pass
else:
"a"
"b"
Sometimes it is a list containing only another If node, as in...
if True: 1
elif False: 2
else: 3
Because of this, it's currently impossible to test if someone omitted the else block in the statement above. check_part
looks for cases where it is None, but will receive an empty list. Should be a quick fix.
I tested out a fix here: https://github.com/datacamp/pythonwhat/tree/fix-check-orelse
The only problem is that internally, functions like test_while_loop use check_orelse
, since they may also have else blocks. Since the else block is missing in most examples, test_while_loop will inappropriately fail.
Should be able to fix by modifying has_part
to fail only when no orelse part in both student and submission.
Not that the patch in the fix-check-orelse branch fails for functions like test_for_loop
, since they allow for pieces to be empty. (e.g. get the ifs
in a list comp, and don't fail if there aren't any).