check_assignment function
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 yep, agree, it should follow the same rules as a list comprehension; it's time to clean these 'rules' up a bit.
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 +1!
Was originally hoping it would be a quick fix--but I think it will need to be done after cleaning up the parsing rules.