Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def Visit_power(self, node): # pylint: disable=invalid-name,missing-docstring
# power ::= atom trailer* ['**' factor]
self.DefaultNodeVisit(node)
# When atom is followed by a trailer, we can not break between them.
# E.g. arr[idx] - no break allowed between 'arr' and '['.
if (len(node.children) > 1 and
pytree_utils.NodeName(node.children[1]) == 'trailer'):
# children[1] itself is a whole trailer: we don't want to
# mark all of it as unbreakable, only its first token: (, [ or .
first = pytree_utils.FirstLeafNode(node.children[1])
if first.value != '.':
_SetUnbreakable(node.children[1].children[0])
# A special case when there are more trailers in the sequence. Given:
# atom tr1 tr2
# The last token of tr1 and the first token of tr2 comprise an unbreakable
# region. For example: foo.bar.baz(1)
# We can't put breaks between either of the '.', '(', or '[' and the names
# *preceding* them.
prev_trailer_idx = 1
while prev_trailer_idx < len(node.children) - 1:
cur_trailer_idx = prev_trailer_idx + 1
cur_trailer = node.children[cur_trailer_idx]
if pytree_utils.NodeName(cur_trailer) == 'trailer':
# Now we know we have two trailers one after the other
prev_trailer = node.children[prev_trailer_idx]
if first == last and first.type == token.COMMENT:
# A comment was inserted before the value, which is a pytree.Leaf.
# Encompass the dictionary's value into an ATOM node.
last = first.next_sibling
last_clone = last.clone()
new_node = pytree.Node(syms.atom, [first.clone(), last_clone])
for orig_leaf, clone_leaf in zip(last.leaves(), last_clone.leaves()):
pytree_utils.CopyYapfAnnotations(orig_leaf, clone_leaf)
if hasattr(orig_leaf, 'is_pseudo'):
clone_leaf.is_pseudo = orig_leaf.is_pseudo
node.replace(new_node)
node = new_node
last.remove()
first = pytree_utils.FirstLeafNode(node)
last = pytree_utils.LastLeafNode(node)
lparen = pytree.Leaf(
token.LPAR, u'(', context=('', (first.get_lineno(), first.column - 1)))
last_lineno = last.get_lineno()
if last.type == token.STRING and '\n' in last.value:
last_lineno += last.value.count('\n')
if last.type == token.STRING and '\n' in last.value:
last_column = len(last.value.split('\n')[-1]) + 1
else:
last_column = last.column + len(last.value) + 1
rparen = pytree.Leaf(
token.RPAR, u')', context=('', (last_lineno, last_column)))
lparen.is_pseudo = True
def Visit_subscriptlist(self, node): # pylint: disable=invalid-name
# subscriptlist ::= subscript (',' subscript)* [',']
self.DefaultNodeVisit(node)
_SetSplitPenalty(pytree_utils.FirstLeafNode(node), 0)
prev_child = None
for child in node.children:
if prev_child and pytree_utils.NodeName(prev_child) == 'COMMA':
_SetSplitPenalty(pytree_utils.FirstLeafNode(child), 0)
prev_child = child
def Visit_or_test(self, node): # pylint: disable=invalid-name
# or_test ::= and_test ('or' and_test)*
self.DefaultNodeVisit(node)
_IncreasePenalty(node, OR_TEST)
index = 1
while index + 1 < len(node.children):
if style.Get('SPLIT_BEFORE_LOGICAL_OPERATOR'):
_DecrementSplitPenalty(
pytree_utils.FirstLeafNode(node.children[index]), OR_TEST)
else:
_DecrementSplitPenalty(
pytree_utils.FirstLeafNode(node.children[index + 1]), OR_TEST)
index += 2
def Visit_subscriptlist(self, node): # pylint: disable=invalid-name
# subscriptlist ::= subscript (',' subscript)* [',']
self.DefaultNodeVisit(node)
_SetSplitPenalty(pytree_utils.FirstLeafNode(node), 0)
prev_child = None
for child in node.children:
if prev_child and pytree_utils.NodeName(prev_child) == 'COMMA':
_SetSplitPenalty(pytree_utils.FirstLeafNode(child), 0)
prev_child = child
def Visit_atom(self, node): # pylint: disable=invalid-name
# atom ::= ('(' [yield_expr|testlist_gexp] ')'
# '[' [listmaker] ']' |
# '{' [dictsetmaker] '}')
self.DefaultNodeVisit(node)
if (node.children[0].value == '(' and
not hasattr(node.children[0], 'is_pseudo')):
if node.children[-1].value == ')':
if pytree_utils.NodeName(node.parent) == 'if_stmt':
_SetSplitPenalty(node.children[-1], STRONGLY_CONNECTED)
else:
if len(node.children) > 2:
_SetSplitPenalty(pytree_utils.FirstLeafNode(node.children[1]), EXPR)
_SetSplitPenalty(node.children[-1], ATOM)
elif node.children[0].value in '[{' and len(node.children) == 2:
# Keep empty containers together if we can.
_SetUnbreakable(node.children[-1])
def RecExpression(node, first_child_leaf):
if node is first_child_leaf:
return
if isinstance(node, pytree.Leaf):
if node.value in {'(', 'for'}:
return
penalty = pytree_utils.GetNodeAnnotation(
node, pytree_utils.Annotation.SPLIT_PENALTY, default=0)
_SetSplitPenalty(node, penalty + amt)
else:
for child in node.children:
RecExpression(child, first_child_leaf)
RecExpression(node, pytree_utils.FirstLeafNode(node))
def _InsertPseudoParentheses(node):
"""Insert pseudo parentheses so that dicts can be formatted correctly."""
comment_node = None
if isinstance(node, pytree.Node):
if node.children[-1].type == token.COMMENT:
comment_node = node.children[-1].clone()
node.children[-1].remove()
first = pytree_utils.FirstLeafNode(node)
last = pytree_utils.LastLeafNode(node)
if first == last and first.type == token.COMMENT:
# A comment was inserted before the value, which is a pytree.Leaf.
# Encompass the dictionary's value into an ATOM node.
last = first.next_sibling
last_clone = last.clone()
new_node = pytree.Node(syms.atom, [first.clone(), last_clone])
for orig_leaf, clone_leaf in zip(last.leaves(), last_clone.leaves()):
pytree_utils.CopyYapfAnnotations(orig_leaf, clone_leaf)
if hasattr(orig_leaf, 'is_pseudo'):
clone_leaf.is_pseudo = orig_leaf.is_pseudo
node.replace(new_node)
node = new_node
last.remove()
def RecExpression(node, first_child_leaf):
if node is first_child_leaf:
return
if isinstance(node, pytree.Leaf):
if node.value in {'(', 'for', 'if'}:
return
penalty_annotation = pytree_utils.GetNodeAnnotation(
node, pytree_utils.Annotation.SPLIT_PENALTY, default=0)
if penalty_annotation < penalty:
_SetSplitPenalty(node, penalty)
else:
for child in node.children:
RecExpression(child, first_child_leaf)
RecExpression(node, pytree_utils.FirstLeafNode(node))