def visit(self, node: Node): if node.get_lineno() > self.lineno: if node.parent is None: node.remove() return for i, x in enumerate(node.parent.children): if x == node: for y in node.parent.children[i + 1 :]: if node != y: y.parent = None node.parent.children = node.parent.children[: i + 1] node.remove() return self.seen.add(node.get_lineno()) super().visit(node)
def wrap_parens(arg_node: PyNode, checker_fn: callable) -> PyNode or PyLeaf: """ If a node that represents an argument to assert_ function should be grouped, return a new node that adds parentheses around arg_node. Otherwise, return arg_node. :param arg_node: the arg_node to parenthesize :return: the arg_node for the parenthesized expression, or the arg_node itself """ if isinstance(arg_node, PyNode) and checker_fn(arg_node): # log.info('adding parens: "{}" ({}), "{}" ({})'.format(first_child, first_child.type, sibling, sibling.type)) # sometimes arg_node has parent, need to remove it before giving to parenthesize() then re-insert: parent = arg_node.parent if parent is not None: pos_parent = arg_node.remove() new_node = parenthesize(arg_node) parent.insert_child(pos_parent, new_node) else: new_node = parenthesize(arg_node) new_node.prefix = arg_node.prefix arg_node.prefix = '' return new_node return arg_node