Skip to content

Commit 12a99fd

Browse files
committed
#26 Adjust to exclude callables in a string.
1 parent 30ae9ea commit 12a99fd

3 files changed

Lines changed: 156 additions & 3 deletions

File tree

numdoclint/cli.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ def _exec_numdoclint(
166166

167167
def main(
168168
args: Optional[argparse.Namespace] = None,
169-
return_list: bool = False) -> List[dict]:
169+
return_list: bool = False) -> Optional[List[dict]]:
170170
"""
171171
The function of command line entry point.
172172
@@ -181,7 +181,7 @@ def main(
181181
182182
Returns
183183
-------
184-
info_list : list of dicts
184+
info_list : list of dicts or None
185185
List of check results.
186186
"""
187187
description: str = 'NumPy style docstring checking in Python code.'
@@ -250,4 +250,5 @@ def main(
250250
enable_default_or_optional_doc_check=enable_def_or_opt_check,
251251
skip_decorator_name_list=args.skip_decorator_name_list,
252252
)
253-
return info_list
253+
if return_list:
254+
return info_list

numdoclint/helper.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ def get_func_name_list(code_str: str) -> List[str]:
5858
func_name_list : list of str
5959
List containing function names.
6060
"""
61+
code_str = _remove_strs(code_str=code_str)
6162
code_str = code_str.replace('\n', '')
6263
search_pattern: str = r'def .*?\(.*?\)'
6364
searched_result_list: List[str] = re.findall(
@@ -81,6 +82,102 @@ def get_func_name_list(code_str: str) -> List[str]:
8182
return func_name_list
8283

8384

85+
def _remove_strs(*, code_str: str) -> str:
86+
"""
87+
Remove strings line's callables from a specified code string.
88+
89+
Parameters
90+
----------
91+
code_str : str
92+
A target code string.
93+
94+
Returns
95+
-------
96+
result_code_str : str
97+
A result code string.
98+
"""
99+
is_quoted_area: bool = False
100+
quote_str: str = ''
101+
result_code_str: str = ''
102+
for i, char in enumerate(code_str):
103+
prev_char: str = _get_prev_char(code_str=code_str, index=i)
104+
following_3_chars: str = _get_following_3_chars(
105+
code_str=code_str, index=i)
106+
if prev_char == '\\':
107+
result_code_str += char
108+
continue
109+
110+
if is_quoted_area:
111+
if char == quote_str or following_3_chars == quote_str:
112+
quote_str = ''
113+
is_quoted_area = False
114+
continue
115+
116+
if following_3_chars == "'''":
117+
is_quoted_area = True
118+
quote_str = "'''"
119+
continue
120+
if following_3_chars == '"""':
121+
is_quoted_area = True
122+
quote_str = '"""'
123+
continue
124+
if char == "'" or char == '"':
125+
is_quoted_area = True
126+
quote_str = char
127+
continue
128+
129+
result_code_str += char
130+
return result_code_str
131+
132+
133+
def _get_prev_char(*, code_str: str, index: int) -> str:
134+
"""
135+
Get a previous index's character in a code string.
136+
137+
Parameters
138+
----------
139+
code_str : str
140+
A target code string.
141+
index : int
142+
A target index. This interface returns a blank string
143+
if a specified index is zero.
144+
145+
Returns
146+
-------
147+
char : str
148+
A target character.
149+
"""
150+
if index == 0:
151+
return ''
152+
return code_str[index - 1]
153+
154+
155+
def _get_following_3_chars(*, code_str: str, index: int) -> str:
156+
"""
157+
Get following characters (including a specified index)
158+
from a code string.
159+
160+
Parameters
161+
----------
162+
code_str : str
163+
A target code string.
164+
index : int
165+
A target index.
166+
167+
Returns
168+
-------
169+
three_chars : str
170+
An extracted three characters.
171+
"""
172+
str_len: int = len(code_str)
173+
three_chars: str = code_str[index]
174+
if index + 1 < str_len:
175+
three_chars += code_str[index + 1]
176+
if index + 2 < str_len:
177+
three_chars += code_str[index + 2]
178+
return three_chars
179+
180+
84181
def _get_args_str(code_str: str, func_name: str) -> str:
85182
"""
86183
Get the string of the arguments.

tests/test_helper.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def sample_func
4646
4747
4848
sample_str = r'def .*?\\(.*?\\)'
49+
sample_str = 'def sample_func_4(a: int) -> None\n pass'
4950
"""
5051
func_name_list: List[str] = helper.get_func_name_list(
5152
code_str=code_str)
@@ -54,6 +55,7 @@ def sample_func
5455
assert 'sample_func_2' in func_name_list
5556
assert 'sample_func_3' in func_name_list
5657
assert 'sample_func' in func_name_list
58+
assert 'sample_func_4' not in func_name_list
5759

5860

5961
def test_get_arg_name_list() -> None:
@@ -1852,3 +1854,56 @@ def test__remove_type_bracket_block_from_args_str() -> None:
18521854
'list_val_1: List, list_val_2: Optional = [100, 200]'
18531855
)
18541856
assert result_str == expected_str
1857+
1858+
1859+
def test__get_prev_char() -> None:
1860+
code_str: str = 'import os'
1861+
char: str = helper._get_prev_char(code_str=code_str, index=0)
1862+
assert char == ''
1863+
1864+
char = helper._get_prev_char(code_str=code_str, index=1)
1865+
assert char == 'i'
1866+
1867+
1868+
def test__get_following_3_chars() -> None:
1869+
code_str: str = 'import os'
1870+
three_chars: str = helper._get_following_3_chars(
1871+
code_str=code_str, index=8)
1872+
assert three_chars == 's'
1873+
1874+
three_chars = helper._get_following_3_chars(
1875+
code_str=code_str, index=7)
1876+
assert three_chars == 'os'
1877+
1878+
three_chars = helper._get_following_3_chars(
1879+
code_str=code_str, index=6)
1880+
assert three_chars == ' os'
1881+
1882+
three_chars = helper._get_following_3_chars(
1883+
code_str=code_str, index=5)
1884+
assert three_chars == 't o'
1885+
1886+
1887+
def test___remove_strs() -> None:
1888+
code_str: str = (
1889+
'import os'
1890+
'\n\na: int = 10'
1891+
'\nb: str = "\ndef any_func_1() -> int:\n ..."'
1892+
'\ndef any_func_2() -> int:'
1893+
'\n '
1894+
"c: str = '\n\n def any_func_3() -> None:\n ...'"
1895+
'\nd = """\nLorem ipsum\ndolor sit amet\n"""'
1896+
"\ne = '''Hello\n'''"
1897+
)
1898+
code_str = helper._remove_strs(code_str=code_str)
1899+
expected_str: str = (
1900+
'import os'
1901+
'\n\na: int = 10'
1902+
'\nb: str = '
1903+
'\ndef any_func_2() -> int:'
1904+
'\n '
1905+
"c: str = "
1906+
'\nd = '
1907+
"\ne = "
1908+
)
1909+
assert code_str == expected_str

0 commit comments

Comments
 (0)