Skip to content

Commit

Permalink
Make sure to treat async funcdefs the same way as normal funcdefs
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhalter committed Jul 8, 2018
1 parent 52e3db4 commit 3f7aad8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
44 changes: 27 additions & 17 deletions parso/python/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ def _flows_finished(pgen_grammar, stack):
return True


def _func_or_class_has_suite(node):
if node.type == 'decorated':
node = node.children[-1]
if node.type in ('async_funcdef', 'async_stmt'):
node = node.children[-1]
return node.type in ('classdef', 'funcdef') and node.children[-1].type == 'suite'


def suite_or_file_input_is_valid(pgen_grammar, stack):
if not _flows_finished(pgen_grammar, stack):
return False
Expand Down Expand Up @@ -511,7 +519,7 @@ def _copy_nodes(self, tos, nodes, until_line, line_offset):
# binary search.
if _get_last_line(node) > until_line:
# We can split up functions and classes later.
if node.type in ('classdef', 'funcdef') and node.children[-1].type == 'suite':
if _func_or_class_has_suite(node):
new_nodes.append(node)
break

Expand All @@ -522,23 +530,25 @@ def _copy_nodes(self, tos, nodes, until_line, line_offset):

last_node = new_nodes[-1]
line_offset_index = -1
if last_node.type in ('classdef', 'funcdef'):
suite = last_node.children[-1]
if suite.type == 'suite':
suite_tos = _NodesStackNode(suite)
# Don't need to pass line_offset here, it's already done by the
# parent.
suite_nodes, recursive_tos = self._copy_nodes(
suite_tos, suite.children, until_line, line_offset)
if len(suite_nodes) < 2:
# A suite only with newline is not valid.
new_nodes.pop()
else:
suite_tos.parent = tos
new_tos = recursive_tos
line_offset_index = -2
if _func_or_class_has_suite(last_node):
suite = last_node
while suite.type != 'suite':
suite = suite.children[-1]

suite_tos = _NodesStackNode(suite)
# Don't need to pass line_offset here, it's already done by the
# parent.
suite_nodes, recursive_tos = self._copy_nodes(
suite_tos, suite.children, until_line, line_offset)
if len(suite_nodes) < 2:
# A suite only with newline is not valid.
new_nodes.pop()
else:
suite_tos.parent = tos
new_tos = recursive_tos
line_offset_index = -2

elif (new_nodes[-1].type in ('error_leaf', 'error_node') or
elif (last_node.type in ('error_leaf', 'error_node') or
_is_flow_node(new_nodes[-1])):
# Error leafs/nodes don't have a defined start/end. Error
# nodes might not end with a newline (e.g. if there's an
Expand Down
2 changes: 1 addition & 1 deletion test/test_diff_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ def spam():
json.l''')

differ.initialize(code)
module = differ.parse(code + '\n', copies=0)
module = differ.parse(code + '\n', copies=1)
decorated, endmarker = module.children
assert decorated.type == 'decorated'
decorator, func = decorated.children
Expand Down

0 comments on commit 3f7aad8

Please sign in to comment.