Skip to content

Commit

Permalink
Fix return value checking condition if nested function exists (#13).
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-ritchie committed Jul 16, 2019
1 parent cf63c07 commit 18c648b
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 0 deletions.
80 changes: 80 additions & 0 deletions numdoclint/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -1169,6 +1169,13 @@ def return_val_exists_in_func(module_str, func_name):
func_str=func_str, module_str=module_str, func_name=func_name)
if func_str == '':
return False
func_indent_num = get_func_indent_num(
py_module_str=module_str,
func_name=func_name)
func_str = _remove_nested_func_str(
func_str=func_str, func_indent_num=func_indent_num)
if func_str == '':
return False
line_splitted_list = func_str.split('\n')
for line_str in line_splitted_list:
return_statement_exists = 'return' in line_str
Expand All @@ -1183,6 +1190,79 @@ def return_val_exists_in_func(module_str, func_name):
return False


def _add_line_str(target_str, line_str):
"""
Add target line string for string concatenation.
Parameters
----------
target_str : str
The string to be concatenated.
line_str : str
String of target line.
Returns
-------
target_str : str
Concatenated string.
"""

if target_str != '':
target_str += '\n'
target_str += line_str
return target_str


def _remove_nested_func_str(func_str, func_indent_num):
"""
Remove the string of nested function part.
Parameters
----------
func_str : str
Target function string.
func_indent_num : int
Indent number of the target function (starting from 1).
Returns
-------
removed_func_str : str
The string after removed nested function part.
"""

removed_func_str = ''
line_splitted_list = func_str.split('\n')
is_initial_function_appeared = False
is_nested_func_line = False
for line_str in line_splitted_list:
is_func_statement_in = 'def ' in line_str
if not is_nested_func_line:
if not is_func_statement_in or not is_initial_function_appeared:
removed_func_str = _add_line_str(
target_str=removed_func_str, line_str=line_str)
if not is_initial_function_appeared and is_func_statement_in:
is_initial_function_appeared = True
continue
if not is_initial_function_appeared:
continue
line_indent_num = get_line_indent_num(line_str=line_str)
if is_nested_func_line:
if line_str.strip() == '' or '):' in line_str:
continue
if (line_indent_num <= func_indent_num
and not is_func_statement_in):
is_nested_func_line = False
removed_func_str = _add_line_str(
target_str=removed_func_str, line_str=line_str)
continue
if is_func_statement_in and not is_nested_func_line:
if line_indent_num < func_indent_num:
continue
is_nested_func_line = True
continue
return removed_func_str


def _remove_docstring_from_func_str(func_str, module_str, func_name):
"""
Remove the doctring from the function string.
Expand Down
65 changes: 65 additions & 0 deletions tests/test_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -1182,6 +1182,14 @@ def sample_func_6():
Sample function that return sample func.
"""
pass
def sample_func_7():
def sample_func_8():
return 100
pass
'''
result_bool = helper.return_val_exists_in_func(
module_str=module_str, func_name='sample_func_1')
Expand All @@ -1207,6 +1215,10 @@ def sample_func_6():
module_str=module_str, func_name='sample_func_6')
assert not result_bool

result_bool = helper.return_val_exists_in_func(
module_str=module_str, func_name='sample_func_7')
assert not result_bool


def test__parameters_exists_in_docstring():
docstring = """
Expand Down Expand Up @@ -1657,3 +1669,56 @@ def test__remove_type_str_from_arg_str():
after_arg_str = helper._remove_type_str_from_arg_str(
arg_str=' price: int=100 ')
assert after_arg_str == 'price=100'


def test__remove_nested_func_str():
func_str = """
def sample_func_1():
def sample_func_2():
print(100)
def sample_func_3():
return 300
return 200
def sample_func_4(
price=200
):
return price
print(200)
"""
removed_func_str = helper._remove_nested_func_str(
func_str=func_str, func_indent_num=1)
expected_removed_func_str = """def sample_func_1():
print(200)
"""
assert removed_func_str == expected_removed_func_str

func_str = """
def sample_func_2():
print(100)
def sample_func_3():
return 300
return 200
"""
removed_func_str = helper._remove_nested_func_str(
func_str=func_str, func_indent_num=2)
expected_removed_func_str = """ def sample_func_2():
print(100)
return 200
"""
assert removed_func_str == expected_removed_func_str


def test__add_line_str():
target_str = helper._add_line_str(target_str='', line_str='abc')
assert target_str == 'abc'
target_str = helper._add_line_str(target_str='abc', line_str='def')
assert target_str == 'abc\ndef'

0 comments on commit 18c648b

Please sign in to comment.