Skip to content

Commit 5a4c88b

Browse files
committed
Change pre-commit hook behavior from check exclusion to check selection like sphinx side.
1 parent 61b83cf commit 5a4c88b

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

numpydoc/hooks/validate_docstrings.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
from .. import docscrape, validate
2222
from .utils import find_project_root
23+
from ..utils import get_validation_checks
2324

2425

2526
# inline comments that can suppress individual checks per line
@@ -172,7 +173,7 @@ def _ignore_issue(self, node: ast.AST, check: str) -> bool:
172173
bool
173174
Whether the issue should be exluded from the report.
174175
"""
175-
if check in self.config["exclusions"]:
176+
if check not in self.config["checks"]:
176177
return True
177178

178179
if self.config["overrides"]:
@@ -261,7 +262,7 @@ def parse_config(dir_path: os.PathLike = None) -> dict:
261262
dict
262263
Config options for the numpydoc validation hook.
263264
"""
264-
options = {"exclusions": [], "overrides": {}}
265+
options = {"checks": {"all"}, "overrides": {}}
265266
dir_path = Path(dir_path).expanduser().resolve()
266267

267268
toml_path = dir_path / "pyproject.toml"
@@ -271,7 +272,7 @@ def parse_config(dir_path: os.PathLike = None) -> dict:
271272
with open(toml_path, "rb") as toml_file:
272273
pyproject_toml = tomllib.load(toml_file)
273274
config = pyproject_toml.get("tool", {}).get("numpydoc_validation", {})
274-
options["exclusions"] = config.get("ignore", [])
275+
options["checks"] = set(config.get("checks", options["checks"]))
275276
for check in ["SS05", "GL08"]:
276277
regex = config.get(f"override_{check}")
277278
if regex:
@@ -282,9 +283,10 @@ def parse_config(dir_path: os.PathLike = None) -> dict:
282283
numpydoc_validation_config_section = "tool:numpydoc_validation"
283284
try:
284285
try:
285-
options["exclusions"] = config.get(
286-
numpydoc_validation_config_section, "ignore"
287-
).split(",")
286+
options["checks"] = set(
287+
config.get(numpydoc_validation_config_section, "checks").split(",")
288+
or options["checks"]
289+
)
288290
except configparser.NoOptionError:
289291
pass
290292
try:
@@ -302,6 +304,7 @@ def parse_config(dir_path: os.PathLike = None) -> dict:
302304
except configparser.NoSectionError:
303305
pass
304306

307+
options["checks"] = get_validation_checks(options["checks"])
305308
return options
306309

307310

@@ -357,7 +360,7 @@ def main(argv: Union[Sequence[str], None] = None) -> int:
357360
+ "\n ".join(
358361
[
359362
f"- {check}: {validate.ERROR_MSGS[check]}"
360-
for check in config_options["exclusions"]
363+
for check in set(validate.ERROR_MSGS.keys()) - config_options["checks"]
361364
]
362365
)
363366
+ "\n"
@@ -391,15 +394,15 @@ def main(argv: Union[Sequence[str], None] = None) -> int:
391394
' Currently ignoring the following from '
392395
f'{Path(project_root_from_cwd) / config_file}: {ignored_checks}'
393396
'Values provided here will be in addition to the above, unless an alternate config is provided.'
394-
if config_options["exclusions"] else ''
397+
if config_options["checks"] else ''
395398
}"""
396399
),
397400
)
398401

399402
args = parser.parse_args(argv)
400403
project_root, _ = find_project_root(args.files)
401404
config_options = parse_config(args.config or project_root)
402-
config_options["exclusions"].extend(args.ignore or [])
405+
config_options["checks"] -= set(args.ignore or [])
403406

404407
findings = []
405408
for file in args.files:

numpydoc/tests/hooks/test_validate_hook.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ def test_validate_hook_with_toml_config(example_module, tmp_path, capsys):
121121
inspect.cleandoc(
122122
"""
123123
[tool.numpydoc_validation]
124-
ignore = [
124+
checks = [
125+
"all",
125126
"EX01",
126127
"SA01",
127128
"ES01",
@@ -162,7 +163,7 @@ def test_validate_hook_with_setup_cfg(example_module, tmp_path, capsys):
162163
inspect.cleandoc(
163164
"""
164165
[tool:numpydoc_validation]
165-
ignore = EX01,SA01,ES01
166+
checks = all,EX01,SA01,ES01
166167
override_SS05 = ^((Process|Assess|Access) )
167168
override_GL08 = ^(__init__)$
168169
"""

0 commit comments

Comments
 (0)