Kaydet (Commit) a957d588 authored tarafından Patrick Maupin's avatar Patrick Maupin Kaydeden (comit) GitHub

Generate PEP8-compatible blank lines in output (#73)

üst aebce9b0
...@@ -310,13 +310,15 @@ class SourceGenerator(ExplicitNodeVisitor): ...@@ -310,13 +310,15 @@ class SourceGenerator(ExplicitNodeVisitor):
def visit_FunctionDef(self, node, async=False): def visit_FunctionDef(self, node, async=False):
prefix = 'async ' if async else '' prefix = 'async ' if async else ''
self.decorators(node, 1) self.decorators(node, 1 if self.indentation else 2)
self.statement(node, '%sdef %s' % (prefix, node.name), '(') self.statement(node, '%sdef %s' % (prefix, node.name), '(')
self.visit_arguments(node.args) self.visit_arguments(node.args)
self.write(')') self.write(')')
self.conditional_write(' ->', self.get_returns(node)) self.conditional_write(' ->', self.get_returns(node))
self.write(':') self.write(':')
self.body(node.body) self.body(node.body)
if not self.indentation:
self.newline(extra=2)
# introduced in Python 3.5 # introduced in Python 3.5
def visit_AsyncFunctionDef(self, node): def visit_AsyncFunctionDef(self, node):
...@@ -344,6 +346,8 @@ class SourceGenerator(ExplicitNodeVisitor): ...@@ -344,6 +346,8 @@ class SourceGenerator(ExplicitNodeVisitor):
self.conditional_write(paren_or_comma, '**', self.get_kwargs(node)) self.conditional_write(paren_or_comma, '**', self.get_kwargs(node))
self.write(have_args and '):' or ':') self.write(have_args and '):' or ':')
self.body(node.body) self.body(node.body)
if not self.indentation:
self.newline(extra=2)
def visit_If(self, node): def visit_If(self, node):
set_precedence(node, node.test) set_precedence(node, node.test)
......
...@@ -129,6 +129,7 @@ class CodegenTestCase(unittest.TestCase): ...@@ -129,6 +129,7 @@ class CodegenTestCase(unittest.TestCase):
source = """ source = """
j = [1, 2, 3] j = [1, 2, 3]
def test(a1, a2, b1=j, b2='123', b3={}, b4=[]): def test(a1, a2, b1=j, b2='123', b3={}, b4=[]):
pass""" pass"""
self.assertAstSourceEqual(source) self.assertAstSourceEqual(source)
...@@ -137,6 +138,7 @@ class CodegenTestCase(unittest.TestCase): ...@@ -137,6 +138,7 @@ class CodegenTestCase(unittest.TestCase):
source = canonical(""" source = canonical("""
j = [1, 2, 3] j = [1, 2, 3]
def test(a1, a2, b1=j, b2='123', b3={}, b4=[]): def test(a1, a2, b1=j, b2='123', b3={}, b4=[]):
pass""") pass""")
root_node = ast.parse(source) root_node = ast.parse(source)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment