pythonwhat icon indicating copy to clipboard operation
pythonwhat copied to clipboard

check_assignment function

Open machow opened this issue 8 years ago • 4 comments

From slack discussion w/ @yrdatacamp

e.g.

Solution

x = sum(range(10))

x = ['a', 'b', 'c']

SCT

Ex().check_assignment('x', 0).has_equal_ast()

checks the first assignment, and looks for the AST of sum(range(10)). The code for getting assignments is already in the parsers, so should be quick

Design decision needed: using the existing parser, check_assignment would find "All assignments at top-level, as well as in if, while, for and with statements". This is different from, say, looking for a list comprehension, which does not look inside for loops, etc.. Should check_assignment follow the same rules as a list comprehension? (cc @filipsch)

machow avatar Mar 17 '17 14:03 machow

@machow yep, agree, it should follow the same rules as a list comprehension; it's time to clean these 'rules' up a bit.

filipsch avatar Mar 20 '17 07:03 filipsch

it's time to clean these 'rules' up a bit.

that's fair! I've looked back through the parsing code a few times over the last couple weeks, and there's a lot of consistency. A big improvement understanding it would be changing the way parsing is specified so things like this...

class SomeParser(Parser):
    def visit_BinOp(self, node):
        self.visit(node.left)
        self.visit(node.right)

    def visit_Assign(self, node):
        self.visit(node.value)

    def visit_AugAssign(self, node):
        self.visit(node.value)

    def visit_Compare(self, node):
        self.visit_each(node.comparators)

    def visit_UnaryOp(self, node):
        self.visit(node.operand)

looks like this (or something)...

class ParseSomething(Parser):
    _visit = ['BinOp', 'Assign', 'AugAssign', 'Compare', 'UnaryOp']

machow avatar Mar 20 '17 13:03 machow

@machow +1!

filipsch avatar Mar 20 '17 18:03 filipsch

Was originally hoping it would be a quick fix--but I think it will need to be done after cleaning up the parsing rules.

machow avatar May 22 '17 14:05 machow