iter_return_stmts should also return return statements without a value.

This commit is contained in:
Dave Halter
2017-09-02 12:32:32 +02:00
parent 9ab5937a3c
commit e79c0755eb
3 changed files with 25 additions and 3 deletions

View File

@@ -32,6 +32,10 @@ from parso.tree import Node, BaseNode, Leaf, ErrorNode, ErrorLeaf, \
search_ancestor search_ancestor
from parso.python.prefix import split_prefix from parso.python.prefix import split_prefix
_FLOW_CONTAINERS = set(['if_stmt', 'while_stmt', 'for_stmt', 'try_stmt',
'with_stmt', 'async_stmt', 'suite'])
_RETURN_STMT_CONTAINERS = set(['suite', 'simple_stmt']) | _FLOW_CONTAINERS
class DocstringMixin(object): class DocstringMixin(object):
__slots__ = () __slots__ = ()
@@ -42,7 +46,7 @@ class DocstringMixin(object):
""" """
if self.type == 'file_input': if self.type == 'file_input':
node = self.children[0] node = self.children[0]
elif isinstance(self, ClassOrFunc): elif self.type in ('funcdef', 'classdef'):
node = self.children[self.children.index(':') + 1] node = self.children[self.children.index(':') + 1]
if node.type == 'suite': # Normally a suite if node.type == 'suite': # Normally a suite
node = node.children[1] # -> NEWLINE stmt node = node.children[1] # -> NEWLINE stmt
@@ -542,7 +546,16 @@ class Function(ClassOrFunc):
""" """
Returns a generator of `return_stmt`. Returns a generator of `return_stmt`.
""" """
return self._search_in_scope('return_stmt') def scan(children):
for element in children:
if element.type == 'return_stmt' \
or element.type == 'keyword' and element.value == 'return':
yield element
if element.type in _RETURN_STMT_CONTAINERS:
for e in scan(element.children):
yield e
return scan(self.children)
def is_generator(self): def is_generator(self):
""" """

View File

@@ -2,7 +2,7 @@
addopts = --doctest-modules addopts = --doctest-modules
# Ignore broken files inblackbox test directories # Ignore broken files inblackbox test directories
norecursedirs = .* docs scripts normalizer_issue_files norecursedirs = .* docs scripts normalizer_issue_files build
# Activate `clean_jedi_cache` fixture for all tests. This should be # Activate `clean_jedi_cache` fixture for all tests. This should be
# fine as long as we are using `clean_jedi_cache` as a session scoped # fine as long as we are using `clean_jedi_cache` as a session scoped

View File

@@ -140,3 +140,12 @@ def test_yields(each_version):
def test_yield_from(): def test_yield_from():
y, = get_yield_exprs('def x(): (yield from 1)', '3.3') y, = get_yield_exprs('def x(): (yield from 1)', '3.3')
assert y.type == 'yield_expr' assert y.type == 'yield_expr'
def test_returns():
r, = get_return_stmts('def x(): return')
assert r.value == 'return'
assert r.type == 'keyword'
r, = get_return_stmts('def x(): return 1')
assert r.type == 'return_stmt'