From 37e97725062c2de770308739b4180483cf90b927 Mon Sep 17 00:00:00 2001 From: Pierre Sassoulas Date: Sat, 8 Jun 2024 08:59:37 +0200 Subject: [PATCH] Gracefully handle invalid paths csv regex in conf/args Fix 'ignore-paths', in particular, but others using this function too. Closes #9680 (cherry picked from commit 3f1f7b8bb6440fc7a2e5e1e9adf0ba6c3fec6079) --- doc/whatsnew/fragments/9680.bugfix | 4 ++++ pylint/config/argument.py | 4 ++-- .../toml/issue_9680/bad_regex_in_ignore_paths.32.out | 2 ++ .../functional/toml/issue_9680/bad_regex_in_ignore_paths.toml | 3 +++ 4 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 doc/whatsnew/fragments/9680.bugfix create mode 100644 tests/config/functional/toml/issue_9680/bad_regex_in_ignore_paths.32.out create mode 100644 tests/config/functional/toml/issue_9680/bad_regex_in_ignore_paths.toml diff --git a/doc/whatsnew/fragments/9680.bugfix b/doc/whatsnew/fragments/9680.bugfix new file mode 100644 index 00000000000..cfec07a6d79 --- /dev/null +++ b/doc/whatsnew/fragments/9680.bugfix @@ -0,0 +1,4 @@ +Impossible to compile regexes for paths in the configuration or argument given to pylint won't crash anymore but +raise an argparse error and display the error message from ``re.compile`` instead. + +Closes #9680 diff --git a/pylint/config/argument.py b/pylint/config/argument.py index 2d2a46a3fda..95cdde4ecbf 100644 --- a/pylint/config/argument.py +++ b/pylint/config/argument.py @@ -103,7 +103,7 @@ def _py_version_transformer(value: str) -> tuple[int, ...]: def _regex_transformer(value: str) -> Pattern[str]: - """Return `re.compile(value)`.""" + """Prevents 're.error' from propagating and crash pylint.""" try: return re.compile(value) except re.error as e: @@ -124,7 +124,7 @@ def _regexp_paths_csv_transfomer(value: str) -> Sequence[Pattern[str]]: patterns: list[Pattern[str]] = [] for pattern in _csv_transformer(value): patterns.append( - re.compile( + _regex_transformer( str(pathlib.PureWindowsPath(pattern)).replace("\\", "\\\\") + "|" + pathlib.PureWindowsPath(pattern).as_posix() diff --git a/tests/config/functional/toml/issue_9680/bad_regex_in_ignore_paths.32.out b/tests/config/functional/toml/issue_9680/bad_regex_in_ignore_paths.32.out new file mode 100644 index 00000000000..b28c8921038 --- /dev/null +++ b/tests/config/functional/toml/issue_9680/bad_regex_in_ignore_paths.32.out @@ -0,0 +1,2 @@ +usage: pylint [options] +pylint: error: argument --ignore-paths: Error in provided regular expression: project\\tooling_context\\**|project/tooling_context/** beginning at index 27: multiple repeat diff --git a/tests/config/functional/toml/issue_9680/bad_regex_in_ignore_paths.toml b/tests/config/functional/toml/issue_9680/bad_regex_in_ignore_paths.toml new file mode 100644 index 00000000000..528a6da5dd2 --- /dev/null +++ b/tests/config/functional/toml/issue_9680/bad_regex_in_ignore_paths.toml @@ -0,0 +1,3 @@ +# Check that we report regex error in configuration file properly +[tool.pylint."main"] +ignore-paths = ['project/tooling_context/**']