Skip to content

Commit ff8db01

Browse files
committed
Expand override logic for pre-commit hook to any check.
1 parent 4a6c2ec commit ff8db01

File tree

2 files changed

+39
-22
lines changed

2 files changed

+39
-22
lines changed

numpydoc/hooks/validate_docstrings.py

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,12 @@ def _ignore_issue(self, node: ast.AST, check: str) -> bool:
182182
return True
183183

184184
if self.config["overrides"]:
185-
if check == "SS05":
186-
pattern = self.config["overrides"].get("SS05")
187-
if pattern and re.match(pattern, ast.get_docstring(node)) is not None:
185+
try:
186+
pattern = self.config["overrides"][check]
187+
if re.search(pattern, ast.get_docstring(node)) is not None:
188188
return True
189+
except KeyError:
190+
pass
189191

190192
try:
191193
if check in self.numpydoc_ignore_comments[getattr(node, "lineno", 1)]:
@@ -265,6 +267,20 @@ def parse_config(dir_path: os.PathLike = None) -> dict:
265267
toml_path = dir_path / "pyproject.toml"
266268
cfg_path = dir_path / "setup.cfg"
267269

270+
def compile_regex(expressions):
271+
return (
272+
re.compile(r"|".join(exp for exp in expressions if exp))
273+
if expressions
274+
else None
275+
)
276+
277+
def extract_check_overrides(options, config_items):
278+
for option, value in config_items:
279+
if option.startswith("override_"):
280+
_, check = option.split("_")
281+
if value:
282+
options["overrides"][check.upper()] = compile_regex(value)
283+
268284
if toml_path.is_file():
269285
with open(toml_path, "rb") as toml_file:
270286
pyproject_toml = tomllib.load(toml_file)
@@ -278,10 +294,8 @@ def parse_config(dir_path: os.PathLike = None) -> dict:
278294
else [global_exclusions]
279295
)
280296

281-
for check in ["SS05"]:
282-
regex = config.get(f"override_{check}")
283-
if regex:
284-
options["overrides"][check] = re.compile(regex)
297+
extract_check_overrides(options, config.items())
298+
285299
elif cfg_path.is_file():
286300
config = configparser.ConfigParser()
287301
config.read(cfg_path)
@@ -305,21 +319,16 @@ def parse_config(dir_path: os.PathLike = None) -> dict:
305319
)
306320
except configparser.NoOptionError:
307321
pass
308-
try:
309-
options["overrides"]["SS05"] = re.compile(
310-
config.get(numpydoc_validation_config_section, "override_SS05")
311-
)
312-
except configparser.NoOptionError:
313-
pass
322+
323+
extract_check_overrides(
324+
options, config.items(numpydoc_validation_config_section)
325+
)
326+
314327
except configparser.NoSectionError:
315328
pass
316329

317330
options["checks"] = get_validation_checks(options["checks"])
318-
options["exclude"] = (
319-
re.compile(r"|".join(exp for exp in options["exclude"]))
320-
if options["exclude"]
321-
else None
322-
)
331+
options["exclude"] = compile_regex(options["exclude"])
323332
return options
324333

325334

numpydoc/tests/hooks/test_validate_hook.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,12 @@ def test_validate_hook_with_toml_config(example_module, tmp_path, capsys):
131131
"SA01",
132132
"ES01",
133133
]
134-
override_SS05 = '^((Process|Assess|Access) )'
135134
exclude = '\\.__init__$'
135+
override_SS05 = [
136+
'^Process',
137+
'^Assess',
138+
'^Access',
139+
]
136140
"""
137141
)
138142
)
@@ -171,7 +175,7 @@ def test_validate_hook_with_setup_cfg(example_module, tmp_path, capsys):
171175
[tool:numpydoc_validation]
172176
checks = all,EX01,SA01,ES01
173177
exclude = \\.__init__$
174-
override_SS05 = ^((Process|Assess|Access) )
178+
override_SS05 = ^Process,^Assess,^Access
175179
"""
176180
)
177181
)
@@ -226,11 +230,15 @@ def test_validate_hook_exclude_option_pyproject(example_module, tmp_path, capsys
226230
"SA01",
227231
"ES01",
228232
]
229-
override_SS05 = '^((Process|Assess|Access) )'
230233
exclude = [
231234
'\.do_something$',
232235
'\.__init__$',
233236
]
237+
override_SS05 = [
238+
'^Process',
239+
'^Assess',
240+
'^Access',
241+
]
234242
"""
235243
)
236244
)
@@ -264,8 +272,8 @@ def test_validate_hook_exclude_option_setup_cfg(example_module, tmp_path, capsys
264272
"""
265273
[tool:numpydoc_validation]
266274
checks = all,EX01,SA01,ES01
267-
override_SS05 = ^((Process|Assess|Access) )
268275
exclude = \\.NewClass$,\\.__init__$
276+
override_SS05 = ^Process,^Assess,^Access
269277
"""
270278
)
271279
)

0 commit comments

Comments
 (0)