pythonwhat icon indicating copy to clipboard operation
pythonwhat copied to clipboard

Fix handling of missing orelse block

Open machow opened this issue 8 years ago • 2 comments

in most AST nodes, when a missing attribute,

  1. usually only corresponds to a single node, it is None
  2. 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.

machow avatar Dec 22 '16 17:12 machow

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.

machow avatar Jan 02 '17 19:01 machow

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).

machow avatar Mar 02 '17 20:03 machow