Skip to content

Commit 18c648b

Browse files
committed
Fix return value checking condition if nested function exists (#13).
1 parent cf63c07 commit 18c648b

2 files changed

Lines changed: 145 additions & 0 deletions

File tree

numdoclint/helper.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,6 +1169,13 @@ def return_val_exists_in_func(module_str, func_name):
11691169
func_str=func_str, module_str=module_str, func_name=func_name)
11701170
if func_str == '':
11711171
return False
1172+
func_indent_num = get_func_indent_num(
1173+
py_module_str=module_str,
1174+
func_name=func_name)
1175+
func_str = _remove_nested_func_str(
1176+
func_str=func_str, func_indent_num=func_indent_num)
1177+
if func_str == '':
1178+
return False
11721179
line_splitted_list = func_str.split('\n')
11731180
for line_str in line_splitted_list:
11741181
return_statement_exists = 'return' in line_str
@@ -1183,6 +1190,79 @@ def return_val_exists_in_func(module_str, func_name):
11831190
return False
11841191

11851192

1193+
def _add_line_str(target_str, line_str):
1194+
"""
1195+
Add target line string for string concatenation.
1196+
1197+
Parameters
1198+
----------
1199+
target_str : str
1200+
The string to be concatenated.
1201+
line_str : str
1202+
String of target line.
1203+
1204+
Returns
1205+
-------
1206+
target_str : str
1207+
Concatenated string.
1208+
"""
1209+
1210+
if target_str != '':
1211+
target_str += '\n'
1212+
target_str += line_str
1213+
return target_str
1214+
1215+
1216+
def _remove_nested_func_str(func_str, func_indent_num):
1217+
"""
1218+
Remove the string of nested function part.
1219+
1220+
Parameters
1221+
----------
1222+
func_str : str
1223+
Target function string.
1224+
func_indent_num : int
1225+
Indent number of the target function (starting from 1).
1226+
1227+
Returns
1228+
-------
1229+
removed_func_str : str
1230+
The string after removed nested function part.
1231+
"""
1232+
1233+
removed_func_str = ''
1234+
line_splitted_list = func_str.split('\n')
1235+
is_initial_function_appeared = False
1236+
is_nested_func_line = False
1237+
for line_str in line_splitted_list:
1238+
is_func_statement_in = 'def ' in line_str
1239+
if not is_nested_func_line:
1240+
if not is_func_statement_in or not is_initial_function_appeared:
1241+
removed_func_str = _add_line_str(
1242+
target_str=removed_func_str, line_str=line_str)
1243+
if not is_initial_function_appeared and is_func_statement_in:
1244+
is_initial_function_appeared = True
1245+
continue
1246+
if not is_initial_function_appeared:
1247+
continue
1248+
line_indent_num = get_line_indent_num(line_str=line_str)
1249+
if is_nested_func_line:
1250+
if line_str.strip() == '' or '):' in line_str:
1251+
continue
1252+
if (line_indent_num <= func_indent_num
1253+
and not is_func_statement_in):
1254+
is_nested_func_line = False
1255+
removed_func_str = _add_line_str(
1256+
target_str=removed_func_str, line_str=line_str)
1257+
continue
1258+
if is_func_statement_in and not is_nested_func_line:
1259+
if line_indent_num < func_indent_num:
1260+
continue
1261+
is_nested_func_line = True
1262+
continue
1263+
return removed_func_str
1264+
1265+
11861266
def _remove_docstring_from_func_str(func_str, module_str, func_name):
11871267
"""
11881268
Remove the doctring from the function string.

tests/test_helper.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,6 +1182,14 @@ def sample_func_6():
11821182
Sample function that return sample func.
11831183
"""
11841184
pass
1185+
1186+
1187+
def sample_func_7():
1188+
1189+
def sample_func_8():
1190+
return 100
1191+
1192+
pass
11851193
'''
11861194
result_bool = helper.return_val_exists_in_func(
11871195
module_str=module_str, func_name='sample_func_1')
@@ -1207,6 +1215,10 @@ def sample_func_6():
12071215
module_str=module_str, func_name='sample_func_6')
12081216
assert not result_bool
12091217

1218+
result_bool = helper.return_val_exists_in_func(
1219+
module_str=module_str, func_name='sample_func_7')
1220+
assert not result_bool
1221+
12101222

12111223
def test__parameters_exists_in_docstring():
12121224
docstring = """
@@ -1657,3 +1669,56 @@ def test__remove_type_str_from_arg_str():
16571669
after_arg_str = helper._remove_type_str_from_arg_str(
16581670
arg_str=' price: int=100 ')
16591671
assert after_arg_str == 'price=100'
1672+
1673+
1674+
def test__remove_nested_func_str():
1675+
func_str = """
1676+
def sample_func_1():
1677+
1678+
def sample_func_2():
1679+
print(100)
1680+
1681+
def sample_func_3():
1682+
return 300
1683+
1684+
return 200
1685+
1686+
def sample_func_4(
1687+
price=200
1688+
):
1689+
return price
1690+
1691+
print(200)
1692+
"""
1693+
removed_func_str = helper._remove_nested_func_str(
1694+
func_str=func_str, func_indent_num=1)
1695+
expected_removed_func_str = """def sample_func_1():
1696+
1697+
print(200)
1698+
"""
1699+
assert removed_func_str == expected_removed_func_str
1700+
1701+
func_str = """
1702+
def sample_func_2():
1703+
print(100)
1704+
1705+
def sample_func_3():
1706+
return 300
1707+
1708+
return 200
1709+
"""
1710+
removed_func_str = helper._remove_nested_func_str(
1711+
func_str=func_str, func_indent_num=2)
1712+
expected_removed_func_str = """ def sample_func_2():
1713+
print(100)
1714+
1715+
return 200
1716+
"""
1717+
assert removed_func_str == expected_removed_func_str
1718+
1719+
1720+
def test__add_line_str():
1721+
target_str = helper._add_line_str(target_str='', line_str='abc')
1722+
assert target_str == 'abc'
1723+
target_str = helper._add_line_str(target_str='abc', line_str='def')
1724+
assert target_str == 'abc\ndef'

0 commit comments

Comments
 (0)