Skip to content

Commit abedeed

Browse files
yarikopticclaude
andcommitted
refactor: extract _collect_results() and _filter_results() from validate CLI
Decompose the monolithic validate() click command into helpers: - _collect_results(): runs validation and collects results - _filter_results(): applies min-severity and ignore filters - _process_issues(): simplified, no longer handles ignore (moved to _filter) No behavior changes; all existing tests pass unchanged. Co-Authored-By: Claude Code 2.1.63 / Claude Opus 4.6 <noreply@anthropic.com>
1 parent 20a97a5 commit abedeed

1 file changed

Lines changed: 55 additions & 39 deletions

File tree

dandi/cli/cmd_validate.py

Lines changed: 55 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import annotations
22

3-
from collections.abc import Iterable
43
import logging
54
import os
65
import re
@@ -15,6 +14,57 @@
1514
from ..validate.types import Severity, ValidationResult
1615

1716

17+
def _collect_results(
18+
paths: tuple[str, ...],
19+
schema: str | None,
20+
devel_debug: bool,
21+
allow_any_path: bool,
22+
) -> list[ValidationResult]:
23+
"""Run validation and collect all results into a list."""
24+
# Avoid heavy import by importing within function:
25+
from ..pynwb_utils import ignore_benign_pynwb_warnings
26+
27+
# Don't log validation warnings, as this command reports them to the user
28+
# anyway:
29+
root = logging.getLogger()
30+
for h in root.handlers:
31+
h.addFilter(lambda r: not getattr(r, "validating", False))
32+
33+
if not paths:
34+
paths = (os.curdir,)
35+
# below we are using load_namespaces but it causes HDMF to whine if there
36+
# is no cached name spaces in the file. It is benign but not really useful
37+
# at this point, so we ignore it although ideally there should be a formal
38+
# way to get relevant warnings (not errors) from PyNWB
39+
ignore_benign_pynwb_warnings()
40+
41+
return list(
42+
validate_(
43+
*paths,
44+
schema_version=schema,
45+
devel_debug=devel_debug,
46+
allow_any_path=allow_any_path,
47+
)
48+
)
49+
50+
51+
def _filter_results(
52+
results: list[ValidationResult],
53+
min_severity: str,
54+
ignore: str | None,
55+
) -> list[ValidationResult]:
56+
"""Filter results by minimum severity and ignore pattern."""
57+
min_severity_value = Severity[min_severity].value
58+
filtered = [
59+
r
60+
for r in results
61+
if r.severity is not None and r.severity.value >= min_severity_value
62+
]
63+
if ignore is not None:
64+
filtered = [r for r in filtered if not re.search(ignore, r.id)]
65+
return filtered
66+
67+
1868
@click.command()
1969
@click.option(
2070
"--schema", help="Validate against new BIDS schema version.", metavar="VERSION"
@@ -102,49 +152,15 @@ def validate(
102152
103153
Exits with non-0 exit code if any file is not compliant.
104154
"""
105-
# Avoid heavy import by importing within function:
106-
from ..pynwb_utils import ignore_benign_pynwb_warnings
107-
108-
# Don't log validation warnings, as this command reports them to the user
109-
# anyway:
110-
root = logging.getLogger()
111-
for h in root.handlers:
112-
h.addFilter(lambda r: not getattr(r, "validating", False))
113-
114-
if not paths:
115-
paths = (os.curdir,)
116-
# below we are using load_namespaces but it causes HDMF to whine if there
117-
# is no cached name spaces in the file. It is benign but not really useful
118-
# at this point, so we ignore it although ideally there should be a formal
119-
# way to get relevant warnings (not errors) from PyNWB
120-
ignore_benign_pynwb_warnings()
121-
122-
validator_result = validate_(
123-
*paths,
124-
schema_version=schema,
125-
devel_debug=devel_debug,
126-
allow_any_path=allow_any_path,
127-
)
128-
129-
min_severity_value = Severity[min_severity].value
130-
131-
filtered_results = [
132-
i
133-
for i in validator_result
134-
if i.severity is not None and i.severity.value >= min_severity_value
135-
]
136-
137-
_process_issues(filtered_results, grouping, ignore)
155+
results = _collect_results(paths, schema, devel_debug, allow_any_path)
156+
filtered = _filter_results(results, min_severity, ignore)
157+
_process_issues(filtered, grouping)
138158

139159

140160
def _process_issues(
141-
validator_result: Iterable[ValidationResult],
161+
issues: list[ValidationResult],
142162
grouping: str,
143-
ignore: str | None = None,
144163
) -> None:
145-
issues = [i for i in validator_result if i.severity is not None]
146-
if ignore is not None:
147-
issues = [i for i in issues if not re.search(ignore, i.id)]
148164
purviews = [i.purview for i in issues]
149165
if grouping == "none":
150166
display_errors(

0 commit comments

Comments
 (0)