Skip to content
Next Next commit
1) Added a new function in fixer_util.py, BlankLineOrPass. 2) Modifie…
…d future and itertools_imports fixers to use BlankLineOrPass instead of BlankLine.
  • Loading branch information
Samuel Tatasurya Samuel Tatasurya
Samuel Tatasurya authored and Samuel Tatasurya committed Nov 6, 2019
commit 8e48b5decd81ebeb85703de61d392af48899d896
33 changes: 33 additions & 0 deletions Lib/lib2to3/fixer_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,39 @@ def ImportAndCall(node, results, names):
new.prefix = node.prefix
return new

def BlankLineOrPass(node):
"""Returns either a blank line or a pass statement depending on
the node's parent's siblings to maintain syntactic correctness
within a suite after conversion"""
skip = {token.NEWLINE, token.INDENT, token.DEDENT}
def has_significant_sibling(node, is_forward):
sibling = None
Comment thread
samtatasurya marked this conversation as resolved.
Outdated
if is_forward:
sibling = node.next_sibling
else:
sibling = node.prev_sibling
while sibling:
if isinstance(sibling, Node):
if sibling.type != syms.simple_stmt:
return True
for child in sibling.children:
if child.type not in skip:
return True
elif isinstance(sibling, Leaf) and sibling.type not in skip:
return True
if is_forward:
sibling = sibling.next_sibling
else:
sibling = sibling.prev_sibling
return False

parent = node.parent
if parent and parent.parent and parent.parent.type == syms.suite:
if (not has_significant_sibling(parent, False)
and not has_significant_sibling(parent, True)):
return Name("pass")
return BlankLine()


###########################################################
### Determine whether a node represents a given literal
Expand Down
4 changes: 2 additions & 2 deletions Lib/lib2to3/fixes/fix_future.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

# Local imports
from .. import fixer_base
from ..fixer_util import BlankLine
from ..fixer_util import BlankLineOrPass

class FixFuture(fixer_base.BaseFix):
BM_compatible = True
Expand All @@ -17,6 +17,6 @@ class FixFuture(fixer_base.BaseFix):
run_order = 10

def transform(self, node, results):
new = BlankLine()
new = BlankLineOrPass(node)
new.prefix = node.prefix
return new
4 changes: 2 additions & 2 deletions Lib/lib2to3/fixes/fix_itertools_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Local imports
from lib2to3 import fixer_base
from lib2to3.fixer_util import BlankLine, syms, token
from lib2to3.fixer_util import BlankLineOrPass, syms, token


class FixItertoolsImports(fixer_base.BaseFix):
Expand Down Expand Up @@ -52,6 +52,6 @@ def transform(self, node, results):
if (not (imports.children or getattr(imports, 'value', None)) or
imports.parent is None):
p = node.prefix
node = BlankLine()
node = BlankLineOrPass(node)
node.prefix = p
return node