Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def DefaultNodeVisit(self, node):
"""Override the default visitor for Node.
This will set the blank lines required if the last entity was a class or
function.
Arguments:
node: (pytree.Node) The node to visit.
"""
if self.last_was_class_or_function:
if pytree_utils.NodeName(node) in _PYTHON_STATEMENTS:
leaf = _GetFirstChildLeaf(node)
self._SetNumNewlines(leaf, self._GetNumNewlines(leaf))
self.last_was_class_or_function = False
super(_BlankLineCalculator, self).DefaultNodeVisit(node)
def Visit_async_funcdef(self, node): # pylint: disable=invalid-name
self._StartNewLine()
index = 0
for child in node.children:
index += 1
self.Visit(child)
if pytree_utils.NodeName(child) == 'ASYNC':
break
for child in node.children[index].children:
self.Visit(child)
def Visit_atom(self, node): # pylint: disable=invalid-name
for child in node.children:
self.Visit(child)
if len(node.children) != 3:
return
if pytree_utils.NodeName(node.children[0]) != 'LPAR':
return
for child in node.children[1].children:
pytree_utils.SetOpeningBracket(
pytree_utils.FirstLeafNode(child), node.children[0])
def Visit(self, node):
"""Visit a node."""
method = 'Visit_{0}'.format(pytree_utils.NodeName(node))
if hasattr(self, method):
# Found a specific visitor for this node
getattr(self, method)(node)
else:
if isinstance(node, pytree.Leaf):
self.DefaultLeafVisit(node)
else:
self.DefaultNodeVisit(node)
def _AsyncFunction(node):
return (py3compat.PY3 and node.prev_sibling and
pytree_utils.NodeName(node.prev_sibling) == 'ASYNC')
def _HasPrecedence(tok):
"""Whether a binary operation has precedence within its context."""
node = tok.node
# We let ancestor be the statement surrounding the operation that tok is the
# operator in.
ancestor = node.parent.parent
while ancestor is not None:
# Search through the ancestor nodes in the parse tree for operators with
# lower precedence.
predecessor_type = pytree_utils.NodeName(ancestor)
if predecessor_type in ['arith_expr', 'term']:
# An ancestor "arith_expr" or "term" means we have found an operator
# with lower precedence than our tok.
return True
if predecessor_type != 'atom':
# We understand the context to look for precedence within as an
# arbitrary nesting of "arith_expr", "term", and "atom" nodes. If we
# leave this context we have not found a lower precedence operator.
return False
# Under normal usage we expect a complete parse tree to be available and
# we will return before we get an AttributeError from the root.
ancestor = ancestor.parent
def _FindNodeWithStandaloneLineParent(node):
"""Find a node whose parent is a 'standalone line' node.
See the comment above _STANDALONE_LINE_NODES for more details.
Arguments:
node: node to start from
Returns:
Suitable node that's either the node itself or one of its ancestors.
"""
if pytree_utils.NodeName(node.parent) in _STANDALONE_LINE_NODES:
return node
else:
# This is guaranteed to terminate because 'file_input' is the root node of
# any pytree.
return _FindNodeWithStandaloneLineParent(node.parent)
def _DetermineMustSplitAnnotation(node):
"""Enforce a split in the list if the list ends with a comma."""
if style.Get('DISABLE_ENDING_COMMA_HEURISTIC'):
return
if not _ContainsComments(node):
token = next(node.parent.leaves())
if token.value == '(':
if sum(1 for ch in node.children
if pytree_utils.NodeName(ch) == 'COMMA') < 2:
return
if (not isinstance(node.children[-1], pytree.Leaf) or
node.children[-1].value != ','):
return
num_children = len(node.children)
index = 0
_SetMustSplitOnFirstLeaf(node.children[0])
while index < num_children - 1:
child = node.children[index]
if isinstance(child, pytree.Leaf) and child.value == ',':
next_child = node.children[index + 1]
if next_child.type == grammar_token.COMMENT:
index += 1
if index >= num_children - 1:
break
_SetMustSplitOnFirstLeaf(node.children[index + 1])
return node_subtype in pytree_utils.GetNodeAnnotation(
node, pytree_utils.Annotation.SUBTYPE, set())
for child in node.children:
node_name = pytree_utils.NodeName(child)
if node_name not in {'atom', 'arglist', 'power'}:
if HasSubtype(child):
return True
return False
if not HasSubtype(node):
return
for child in node.children:
node_name = pytree_utils.NodeName(child)
if node_name not in {'atom', 'COMMA'}:
_AppendFirstLeafTokenSubtype(child, list_subtype)