-
Notifications
You must be signed in to change notification settings - Fork 33
Add filtering of issues by type/ID or by file location #1743
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
candleindark
wants to merge
13
commits into
master
Choose a base branch
from
enh-validate
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
487680a
feat: add click callback to parse sequence of regexes
candleindark 40670eb
feat: provide util func to filter validation result by ID with regex
candleindark 2d9f2c6
feat: add --match option to `dandi validate` command
candleindark 2e5b2bc
feat: change return type of `parse_regexes` callback
candleindark a80987a
test: refactor two tests in `TestParseRegexes` into one
candleindark 09c1489
fix: provide type annotation to fix mypy errs
candleindark cdf9dea
feat: add helper func to filter validation results by associated paths
candleindark 0530591
feat: add helper func to filter validation results by associated paths
candleindark 0f1b924
feat: add `--include-path` option to `dandi validate` command
candleindark 5cbceb7
doc: document `--include-path` option in `dandi validate` command
candleindark 05e0a5b
docs: correct typo in docs
candleindark f0c6d86
doc: correct typo in docstring
candleindark 9c67a46
doc: correct typo in docstring
candleindark File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,14 +3,20 @@ | |
| from collections.abc import Iterable | ||
| import logging | ||
| import os | ||
| from pathlib import Path | ||
| import re | ||
| from typing import cast | ||
| from typing import Optional, cast | ||
| import warnings | ||
|
|
||
| import click | ||
|
|
||
| from .base import devel_debug_option, devel_option, map_to_click_exceptions | ||
| from ..utils import pluralize | ||
| from .base import ( | ||
| devel_debug_option, | ||
| devel_option, | ||
| map_to_click_exceptions, | ||
| parse_regexes, | ||
| ) | ||
| from ..utils import filter_by_id_patterns, filter_by_paths, pluralize | ||
| from ..validate import validate as validate_ | ||
| from ..validate_types import Severity, ValidationResult | ||
|
|
||
|
|
@@ -80,6 +86,26 @@ def validate_bids( | |
| default="none", | ||
| ) | ||
| @click.option("--ignore", metavar="REGEX", help="Regex matching error IDs to ignore") | ||
| @click.option( | ||
| "--match", | ||
| metavar="REGEX,REGEX,...", | ||
| help=( | ||
| "Comma-separated regex patterns used to filter issues in validation results " | ||
| "by their ID. Only issues with an ID matching at least one of the given " | ||
| "patterns are included in the eventual result. " | ||
| "(No pattern should contain a comma.)" | ||
| ), | ||
| callback=parse_regexes, | ||
| ) | ||
| @click.option( | ||
| "--include-path", | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| multiple=True, | ||
| type=click.Path(exists=True, resolve_path=True, path_type=Path), | ||
| help=( | ||
| "Filter issues in the validation results to only those associated with the " | ||
| "given path(s). This option can be specified multiple times." | ||
| ), | ||
| ) | ||
| @click.option( | ||
| "--min-severity", | ||
| help="Only display issues with severities above this level.", | ||
|
|
@@ -92,6 +118,8 @@ def validate_bids( | |
| def validate( | ||
| paths: tuple[str, ...], | ||
| ignore: str | None, | ||
| match: Optional[set[re.Pattern]], | ||
| include_path: tuple[Path, ...], | ||
| grouping: str, | ||
| min_severity: str, | ||
| schema: str | None = None, | ||
|
|
@@ -134,17 +162,28 @@ def validate( | |
| if i.severity is not None and i.severity.value >= min_severity_value | ||
| ] | ||
|
|
||
| _process_issues(filtered_results, grouping, ignore) | ||
| _process_issues(filtered_results, grouping, ignore, match, include_path) | ||
|
|
||
|
|
||
| def _process_issues( | ||
| validator_result: Iterable[ValidationResult], | ||
| grouping: str, | ||
| ignore: str | None = None, | ||
| match: Optional[set[re.Pattern]] = None, | ||
| include_path: tuple[Path, ...] = (), | ||
| ) -> None: | ||
| issues = [i for i in validator_result if i.severity is not None] | ||
| if ignore is not None: | ||
| issues = [i for i in issues if not re.search(ignore, i.id)] | ||
|
|
||
| # Filter issues by ID patterns if provided | ||
| if match is not None: | ||
| issues = filter_by_id_patterns(issues, match) | ||
|
|
||
| # Filter issues by included paths if provided | ||
| if include_path: | ||
| issues = filter_by_paths(issues, include_path) | ||
|
|
||
| purviews = [i.purview for i in issues] | ||
| if grouping == "none": | ||
| display_errors( | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| import re | ||
|
|
||
| import click | ||
| import pytest | ||
|
|
||
| from dandi.cli.base import _compile_regex, parse_regexes | ||
|
|
||
| DUMMY_CTX = click.Context(click.Command("dummy")) | ||
| DUMMY_PARAM = click.Option(["--dummy"]) | ||
|
|
||
|
|
||
| class TestCompileRegex: | ||
| @pytest.mark.parametrize( | ||
| "pattern", | ||
| [ | ||
| "abc", | ||
| "[a-z]+", | ||
| "^start$", | ||
| r"a\.b", | ||
| ], | ||
| ) | ||
| def test_valid_patterns_return_pattern(self, pattern): | ||
| compiled = _compile_regex(pattern) | ||
| assert isinstance(compiled, re.Pattern) | ||
| assert compiled.pattern == pattern | ||
|
|
||
| @pytest.mark.parametrize("pattern", ["(", "[a-z", "\\"]) | ||
| def test_invalid_patterns_raise_bad_parameter(self, pattern): | ||
| with pytest.raises(click.BadParameter) as exc_info: | ||
| _compile_regex(pattern) | ||
| msg = str(exc_info.value) | ||
| assert "Invalid regex pattern" in msg | ||
| assert repr(pattern) in msg | ||
|
|
||
|
|
||
| class TestParseRegexes: | ||
| def test_none_returns_none(self): | ||
| assert parse_regexes(DUMMY_CTX, DUMMY_PARAM, None) is None | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "value, expected_patterns_in_strs", | ||
| [ | ||
| # Single patterns | ||
| ("abc", {"abc"}), | ||
| ("[a-z]+", {"[a-z]+"}), | ||
| (r"a\.b", {r"a\.b"}), | ||
| (r"", {r""}), | ||
| # Multiple patterns | ||
| ("foo,,bar", {"foo", "", "bar"}), | ||
| ("^start$,end$", {"^start$", "end$"}), | ||
| (r"a\.b,c+d", {r"a\.b", r"c+d"}), | ||
| # duplicates should be collapsed by the internal set() | ||
| ("foo,foo,bar", {"foo", "bar"}), | ||
| ], | ||
| ) | ||
| def test_parse_patterns( | ||
| self, value: str, expected_patterns_in_strs: set[str] | ||
| ) -> None: | ||
| result = parse_regexes(DUMMY_CTX, DUMMY_PARAM, value) | ||
| assert isinstance(result, set) | ||
|
|
||
| assert {p.pattern for p in result} == expected_patterns_in_strs | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "value, bad_pattern", [("(", "("), ("foo,(", "("), ("good,[a-z", "[a-z")] | ||
| ) | ||
| def test_invalid_pattern_raises_bad_parameter( | ||
| self, value: str, bad_pattern: str | ||
| ) -> None: | ||
| with pytest.raises(click.BadParameter, match=re.escape(bad_pattern)): | ||
| parse_regexes(DUMMY_CTX, DUMMY_PARAM, value) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
--matchsuggests me to match as limiting some work... here we are talking only report filtering... I also wonder if we could join here to support various elements of an issue, not have them separate, so could be smth likewhich should serve as AND so should match all of the above