Skip to content

Commit 59a3ac6

Browse files
committed
feat(numpydoc/hooks/validate_docstrings.py): Added a pyproject.toml config option exclude_files that allows regex path exclusions
Solution to Issue #497
1 parent 8c66165 commit 59a3ac6

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed

doc/validation.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ the pre-commit hook as follows:
3636
expressions ``\.undocumented_method$`` or ``\.__repr__$``. This
3737
maps to ``numpydoc_validation_exclude`` from the
3838
:ref:`Sphinx build configuration <validation_during_sphinx_build>`.
39+
* ``exclude_files``: Exclude file paths matching the regular expressions
40+
``^tests/.*`` or ``^module/gui.*``.
3941
* ``override_SS05``: Allow docstrings to start with "Process ", "Assess ",
4042
or "Access ". To override different checks, add a field for each code in
4143
the form of ``override_<code>`` with a collection of regular expression(s)
@@ -57,6 +59,10 @@ the pre-commit hook as follows:
5759
'\.undocumented_method$',
5860
'\.__repr__$',
5961
]
62+
exclude_files = [ # don't process filepaths that match these regex
63+
'^tests/.*',
64+
'^module/gui.*',
65+
]
6066
override_SS05 = [ # override SS05 to allow docstrings starting with these words
6167
'^Process ',
6268
'^Assess ',

numpydoc/hooks/validate_docstrings.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,12 @@ def parse_config(dir_path: os.PathLike = None) -> dict:
273273
dict
274274
Config options for the numpydoc validation hook.
275275
"""
276-
options = {"checks": {"all"}, "exclude": set(), "overrides": {}}
276+
options = {
277+
"checks": {"all"},
278+
"exclude": set(),
279+
"overrides": {},
280+
"exclude_files": set(),
281+
}
277282
dir_path = Path(dir_path).expanduser().resolve()
278283

279284
toml_path = dir_path / "pyproject.toml"
@@ -306,6 +311,13 @@ def extract_check_overrides(options, config_items):
306311
else [global_exclusions]
307312
)
308313

314+
file_exclusions = config.get("exclude_files", options["exclude_files"])
315+
options["exclude_files"] = set(
316+
file_exclusions
317+
if not isinstance(file_exclusions, str)
318+
else [file_exclusions]
319+
)
320+
309321
extract_check_overrides(options, config.items())
310322

311323
elif cfg_path.is_file():
@@ -332,6 +344,16 @@ def extract_check_overrides(options, config_items):
332344
except configparser.NoOptionError:
333345
pass
334346

347+
try:
348+
options["exclude_files"] = set(
349+
config.get(numpydoc_validation_config_section, "exclude_files")
350+
.rstrip(",")
351+
.split(",")
352+
or options["exclude_files"]
353+
)
354+
except configparser.NoOptionError:
355+
pass
356+
335357
extract_check_overrides(
336358
options, config.items(numpydoc_validation_config_section)
337359
)
@@ -341,6 +363,7 @@ def extract_check_overrides(options, config_items):
341363

342364
options["checks"] = validate.get_validation_checks(options["checks"])
343365
options["exclude"] = compile_regex(options["exclude"])
366+
options["exclude_files"] = compile_regex(options["exclude_files"])
344367
return options
345368

346369

@@ -395,9 +418,12 @@ def run_hook(
395418
project_root, _ = find_project_root(files)
396419
config_options = parse_config(config or project_root)
397420
config_options["checks"] -= set(ignore or [])
421+
exclude_re = config_options["exclude_files"]
398422

399423
findings = False
400424
for file in files:
425+
if exclude_re and exclude_re.match(file):
426+
continue
401427
if file_issues := process_file(file, config_options):
402428
findings = True
403429

numpydoc/tests/hooks/test_validate_hook.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,45 @@ def test_validate_hook_with_toml_config(example_module, tmp_path, capsys):
139139
assert capsys.readouterr().err.strip() == expected
140140

141141

142+
def test_validate_hook_with_toml_config_exclude_files(example_module, tmp_path, capsys):
143+
"""
144+
Test that a file is correctly processed in the absence of config files
145+
with command line ignore options.
146+
"""
147+
148+
with open(tmp_path / "pyproject.toml", "w") as config_file:
149+
config_file.write(
150+
inspect.cleandoc(
151+
"""
152+
[tool.numpydoc_validation]
153+
checks = [
154+
"all",
155+
"EX01",
156+
"SA01",
157+
"ES01",
158+
]
159+
exclude = '\\.__init__$'
160+
override_SS05 = [
161+
'^Creates',
162+
]
163+
164+
exclude_files = [
165+
'.*/example.*\.py',
166+
]
167+
"""
168+
)
169+
)
170+
171+
expected = inspect.cleandoc(
172+
"""
173+
"""
174+
)
175+
176+
return_code = run_hook([example_module], config=tmp_path)
177+
assert return_code == 1
178+
assert capsys.readouterr().err.strip() == expected
179+
180+
142181
def test_validate_hook_with_setup_cfg(example_module, tmp_path, capsys):
143182
"""
144183
Test that a file is correctly processed with the config coming from

0 commit comments

Comments
 (0)