Inline functions used only once.

Also move class methods out if they don't require to be in the class.
This commit is contained in:
Bill Wendling 2020-02-05 19:37:48 -08:00
parent 963639422a
commit 15ae9f63b1
3 changed files with 20 additions and 32 deletions

View File

@ -70,9 +70,9 @@ class _BlankLineCalculator(pytree_visitor.PyTreeVisitor):
def Visit_decorator(self, node): # pylint: disable=invalid-name
if (self.last_comment_lineno and
self.last_comment_lineno == node.children[0].lineno - 1):
self._SetNumNewlines(node.children[0], _NO_BLANK_LINES)
_SetNumNewlines(node.children[0], _NO_BLANK_LINES)
else:
self._SetNumNewlines(node.children[0], self._GetNumNewlines(node))
_SetNumNewlines(node.children[0], self._GetNumNewlines(node))
for child in node.children:
self.Visit(child)
self.last_was_decorator = True
@ -93,7 +93,7 @@ class _BlankLineCalculator(pytree_visitor.PyTreeVisitor):
if _AsyncFunction(node):
index = self._SetBlankLinesBetweenCommentAndClassFunc(
node.prev_sibling.parent)
self._SetNumNewlines(node.children[0], None)
_SetNumNewlines(node.children[0], None)
else:
index = self._SetBlankLinesBetweenCommentAndClassFunc(node)
self.last_was_decorator = False
@ -115,7 +115,7 @@ class _BlankLineCalculator(pytree_visitor.PyTreeVisitor):
if self.last_was_class_or_function:
if pytree_utils.NodeName(node) in _PYTHON_STATEMENTS:
leaf = pytree_utils.FirstLeafNode(node)
self._SetNumNewlines(leaf, self._GetNumNewlines(leaf))
_SetNumNewlines(leaf, self._GetNumNewlines(leaf))
self.last_was_class_or_function = False
super(_BlankLineCalculator, self).DefaultNodeVisit(node)
@ -137,17 +137,17 @@ class _BlankLineCalculator(pytree_visitor.PyTreeVisitor):
# node as its only child.
self.Visit(node.children[index].children[0])
if not self.last_was_decorator:
self._SetNumNewlines(node.children[index].children[0], _ONE_BLANK_LINE)
_SetNumNewlines(node.children[index].children[0], _ONE_BLANK_LINE)
index += 1
if (index and node.children[index].lineno - 1
== node.children[index - 1].children[0].lineno):
self._SetNumNewlines(node.children[index], _NO_BLANK_LINES)
_SetNumNewlines(node.children[index], _NO_BLANK_LINES)
else:
if self.last_comment_lineno + 1 == node.children[index].lineno:
num_newlines = _NO_BLANK_LINES
else:
num_newlines = self._GetNumNewlines(node)
self._SetNumNewlines(node.children[index], num_newlines)
_SetNumNewlines(node.children[index], num_newlines)
return index
def _GetNumNewlines(self, node):
@ -157,15 +157,16 @@ class _BlankLineCalculator(pytree_visitor.PyTreeVisitor):
return 1 + style.Get('BLANK_LINES_AROUND_TOP_LEVEL_DEFINITION')
return _ONE_BLANK_LINE
def _SetNumNewlines(self, node, num_newlines):
pytree_utils.SetNodeAnnotation(node, pytree_utils.Annotation.NEWLINES,
num_newlines)
def _IsTopLevel(self, node):
return (not (self.class_level or self.function_level) and
_StartsInZerothColumn(node))
def _SetNumNewlines(node, num_newlines):
pytree_utils.SetNodeAnnotation(node, pytree_utils.Annotation.NEWLINES,
num_newlines)
def _StartsInZerothColumn(node):
return (pytree_utils.FirstLeafNode(node).column == 0 or
(_AsyncFunction(node) and node.prev_sibling.column == 0))

View File

@ -188,7 +188,11 @@ class _SplitPenaltyAssigner(pytree_visitor.PyTreeVisitor):
def Visit_dotted_name(self, node): # pylint: disable=invalid-name
# dotted_name ::= NAME ('.' NAME)*
self._SetUnbreakableOnChildren(node)
for child in node.children:
self.Visit(child)
start = 2 if hasattr(node.children[0], 'is_pseudo') else 1
for i in py3compat.range(start, len(node.children)):
_SetUnbreakable(node.children[i])
def Visit_dictsetmaker(self, node): # pylint: disable=invalid-name
# dictsetmaker ::= ( (test ':' test
@ -244,7 +248,8 @@ class _SplitPenaltyAssigner(pytree_visitor.PyTreeVisitor):
pytree_utils.FirstLeafNode(node.children[1].children[2]), 0)
# Don't split the ending bracket of a subscript list.
_SetVeryStronglyConnected(node.children[-1])
_RecAnnotate(node.children[-1], pytree_utils.Annotation.SPLIT_PENALTY,
VERY_STRONGLY_CONNECTED)
elif name not in {
'arglist', 'argument', 'term', 'or_test', 'and_test', 'comparison',
'atom', 'power'
@ -499,17 +504,6 @@ class _SplitPenaltyAssigner(pytree_visitor.PyTreeVisitor):
_SetSplitPenalty(pytree_utils.FirstLeafNode(child), TOGETHER)
prev_was_comma = False
############################################################################
# Helper methods that set the annotations.
def _SetUnbreakableOnChildren(self, node):
"""Set an UNBREAKABLE penalty annotation on children of node."""
for child in node.children:
self.Visit(child)
start = 2 if hasattr(node.children[0], 'is_pseudo') else 1
for i in py3compat.range(start, len(node.children)):
_SetUnbreakable(node.children[i])
def _SetUnbreakable(node):
"""Set an UNBREAKABLE penalty annotation for the given node."""
@ -523,13 +517,6 @@ def _SetStronglyConnected(*nodes):
STRONGLY_CONNECTED)
def _SetVeryStronglyConnected(*nodes):
"""Set a VERY_STRONGLY_CONNECTED penalty annotation for the given nodes."""
for node in nodes:
_RecAnnotate(node, pytree_utils.Annotation.SPLIT_PENALTY,
VERY_STRONGLY_CONNECTED)
def _SetExpressionPenalty(node, penalty):
"""Set a penalty annotation on children nodes."""

View File

@ -256,7 +256,7 @@ _STYLE_HELP = dict(
Use spaces around the subscript / slice operator. For example:
my_list[1 : 10 : 2]
""")
"""),
SPACES_AROUND_TUPLE_DELIMITERS=textwrap.dedent("""\
Adds a space after the opening '(' and before the ending ')' tuple delimiters.