Skip to content

Commit 748d0cc

Browse files
committed
#16 Add combination pattern of type annotation and default value (e.g., 'Dict[str, int] = {}').
1 parent f85b8f2 commit 748d0cc

2 files changed

Lines changed: 58 additions & 0 deletions

File tree

numdoclint/helper.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,7 @@ def get_arg_default_val_info_dict(py_module_str, func_name):
808808
"""
809809
args_str = _get_args_str(
810810
code_str=py_module_str, func_name=func_name)
811+
args_str = _remove_type_bracket_block_from_args_str(args_str=args_str)
811812
if args_str == '':
812813
return {}
813814
splitted_arg_list = args_str.split(',')
@@ -828,6 +829,39 @@ def get_arg_default_val_info_dict(py_module_str, func_name):
828829
return default_val_info_dict
829830

830831

832+
def _remove_type_bracket_block_from_args_str(args_str: str):
833+
"""
834+
Remove type annotation block bracket from arguments string.
835+
836+
Parameters
837+
----------
838+
args_str : str
839+
The target arguments string.
840+
e.g., 'dict_val: Dict[str, int] == {}'
841+
842+
Returns
843+
-------
844+
result_str : str
845+
The converted arguments string.
846+
e.g., 'dict_val: Dict == {}'
847+
"""
848+
if '[' not in args_str:
849+
return args_str
850+
bracket_count: int = 0
851+
result_str: str = ''
852+
for char in args_str:
853+
if char == '[':
854+
bracket_count += 1
855+
continue
856+
if char == ']':
857+
bracket_count -= 1
858+
continue
859+
if bracket_count != 0:
860+
continue
861+
result_str += char
862+
return result_str
863+
864+
831865
def _remove_type_str_from_arg_str(arg_str):
832866
"""
833867
Remove the string of type information from the argument string.

tests/test_helper.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,10 @@ def sample_func_4(
795795
796796
def sample_func_5(name: str = "apple") -> str:
797797
print(300)
798+
799+
800+
def sample_func_6(dict_value: Optional[Dict[str, int]] = None) -> str:
801+
print(300)
798802
"""
799803
default_val_info_dict = helper.get_arg_default_val_info_dict(
800804
py_module_str=py_module_str, func_name='sample_func_1')
@@ -832,6 +836,13 @@ def sample_func_5(name: str = "apple") -> str:
832836
}
833837
assert default_val_info_dict == expected_dict
834838

839+
default_val_info_dict = helper.get_arg_default_val_info_dict(
840+
py_module_str=py_module_str, func_name='sample_func_6')
841+
expected_dict = {
842+
'dict_value': 'None',
843+
}
844+
assert default_val_info_dict == expected_dict
845+
835846

836847
def test__get_return_value_docstring():
837848
docstring = """
@@ -1785,3 +1796,16 @@ def test__type_anotation_comment_exists():
17851796
result = helper._type_anotation_comment_exists(
17861797
line_str=' # type: (int) -> str')
17871798
assert result
1799+
1800+
1801+
def test__remove_type_bracket_block_from_args_str():
1802+
args_str: str = (
1803+
'dict_val: Optional[Dict[str, int]] = None,'
1804+
' tuple_val: Optional[Tuple[int, str, int]]=None'
1805+
)
1806+
result_str = helper._remove_type_bracket_block_from_args_str(
1807+
args_str=args_str)
1808+
expected_str = (
1809+
'dict_val: Optional = None, tuple_val: Optional=None'
1810+
)
1811+
assert result_str == expected_str

0 commit comments

Comments
 (0)