From 495f324b8f1784305fcb70e9309f336bdc562608 Mon Sep 17 00:00:00 2001 From: David Lawson Date: Mon, 12 Dec 2022 18:05:41 +0000 Subject: [PATCH 1/6] pylint: add failing test when invalid arguments are passed Returning 2 here is confusing as it doesn't match the documentation: https://pylint.pycqa.org/en/latest/user_guide/usage/run.html#exit-codes --- tests/lint/test_run_pylint.py | 18 ++++++++++++++++++ tests/lint/unittest_expand_modules.py | 9 +++++++++ 2 files changed, 27 insertions(+) create mode 100644 tests/lint/test_run_pylint.py diff --git a/tests/lint/test_run_pylint.py b/tests/lint/test_run_pylint.py new file mode 100644 index 0000000000..df51405806 --- /dev/null +++ b/tests/lint/test_run_pylint.py @@ -0,0 +1,18 @@ +# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html +# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE +# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt + + +import pytest +from _pytest.capture import CaptureFixture + +from pylint import run_pylint + + +def test_run_pylint_with_invalid_argument(capsys: CaptureFixture[str]) -> None: + """Check that appropriate exit code is used with invalid argument.""" + with pytest.raises(SystemExit) as ex: + run_pylint(["--never-use-this"]) + captured = capsys.readouterr() + assert captured.err.startswith("usage: pylint [options]") + assert ex.value.code == 32 diff --git a/tests/lint/unittest_expand_modules.py b/tests/lint/unittest_expand_modules.py index 88f058b1ec..1c3f23b00c 100644 --- a/tests/lint/unittest_expand_modules.py +++ b/tests/lint/unittest_expand_modules.py @@ -69,6 +69,14 @@ def test__is_in_ignore_list_re_match() -> None: "path": str(TEST_DIRECTORY / "lint/test_utils.py"), } +test_run_pylint = { + "basename": "lint", + "basepath": INIT_PATH, + "isarg": False, + "name": "lint.test_run_pylint", + "path": str(TEST_DIRECTORY / "lint/test_run_pylint.py"), +} + test_pylinter = { "basename": "lint", "basepath": INIT_PATH, @@ -102,6 +110,7 @@ def _list_expected_package_modules( init_of_package, test_caching, test_pylinter, + test_run_pylint, test_utils, this_file_from_init_deduplicated if deduplicating else this_file_from_init, unittest_lint, From 4eb56dfc1b8f81f8556feb0bc1417560bdba62cb Mon Sep 17 00:00:00 2001 From: David Lawson Date: Mon, 12 Dec 2022 23:01:40 +0000 Subject: [PATCH 2/6] pylint: use exit code 32 when invalid arguments are passed --- pylint/config/config_initialization.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pylint/config/config_initialization.py b/pylint/config/config_initialization.py index b903a58026..d26f0e8c58 100644 --- a/pylint/config/config_initialization.py +++ b/pylint/config/config_initialization.py @@ -87,7 +87,10 @@ def _config_initialization( unrecognized_options.append(opt[1:]) if unrecognized_options: msg = ", ".join(unrecognized_options) - linter._arg_parser.error(f"Unrecognized option found: {msg}") + try: + linter._arg_parser.error(f"Unrecognized option found: {msg}") + except SystemExit: + sys.exit(32) # Now that config file and command line options have been loaded # with all disables, it is safe to emit messages From f2bbe46f0fb20e3bc0594a0982eb70cfae36a146 Mon Sep 17 00:00:00 2001 From: David Lawson Date: Mon, 12 Dec 2022 23:13:03 +0000 Subject: [PATCH 3/6] pylint: add failing test when ambiguous abbreviated parameters are set in a config file This is confusing behaviour. The output is: ``` usage: pylint [options] pylint: error: ambiguous option: --no could match --notes, --notes-rgx, --no-docstring-rgx ``` The exit code is 2 which doesn't match the documentation: https://pylint.pycqa.org/en/latest/user_guide/usage/run.html#exit-codes --- tests/lint/test_run_pylint.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/lint/test_run_pylint.py b/tests/lint/test_run_pylint.py index df51405806..73dc26331b 100644 --- a/tests/lint/test_run_pylint.py +++ b/tests/lint/test_run_pylint.py @@ -2,6 +2,7 @@ # For details: https://github.com/PyCQA/pylint/blob/main/LICENSE # Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt +from pathlib import Path import pytest from _pytest.capture import CaptureFixture @@ -16,3 +17,20 @@ def test_run_pylint_with_invalid_argument(capsys: CaptureFixture[str]) -> None: captured = capsys.readouterr() assert captured.err.startswith("usage: pylint [options]") assert ex.value.code == 32 + + +def test_run_pylint_with_invalid_argument_in_config( + capsys: CaptureFixture[str], tmp_path: Path +) -> None: + """Check that appropriate exit code is used with an ambiguous + argument in a config file. + """ + test_file = tmp_path / "testpylintrc" + with open(test_file, "w", encoding="utf-8") as f: + f.write("[MASTER]\nno=") + + with pytest.raises(SystemExit) as ex: + run_pylint(["--rcfile", f"{test_file}"]) + captured = capsys.readouterr() + assert captured.err.startswith("usage: pylint [options]") + assert ex.value.code == 32 From 60097b9af2fd44e29b9a1dd2f87e88a44ef0ef0a Mon Sep 17 00:00:00 2001 From: David Lawson Date: Mon, 12 Dec 2022 23:19:54 +0000 Subject: [PATCH 4/6] pylint: use exit code 32 when ambiguous abbreviated parameters are set in a config file --- pylint/config/arguments_manager.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/pylint/config/arguments_manager.py b/pylint/config/arguments_manager.py index c771ad3550..2151aefde4 100644 --- a/pylint/config/arguments_manager.py +++ b/pylint/config/arguments_manager.py @@ -256,9 +256,12 @@ def _load_default_argument_values(self) -> None: def _parse_configuration_file(self, arguments: list[str]) -> None: """Parse the arguments found in a configuration file into the namespace.""" - self.config, parsed_args = self._arg_parser.parse_known_args( - arguments, self.config - ) + try: + self.config, parsed_args = self._arg_parser.parse_known_args( + arguments, self.config + ) + except SystemExit: + sys.exit(32) unrecognized_options: list[str] = [] for opt in parsed_args: if opt.startswith("--"): From b71f6adf170c81f6498ceecd18e517ac89dca5f9 Mon Sep 17 00:00:00 2001 From: David Lawson Date: Wed, 14 Dec 2022 21:56:18 +0000 Subject: [PATCH 5/6] Update changelog --- doc/whatsnew/fragments/7931.bugfix | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 doc/whatsnew/fragments/7931.bugfix diff --git a/doc/whatsnew/fragments/7931.bugfix b/doc/whatsnew/fragments/7931.bugfix new file mode 100644 index 0000000000..19789aa007 --- /dev/null +++ b/doc/whatsnew/fragments/7931.bugfix @@ -0,0 +1,3 @@ +Fix exit code when argparse raises SystemExit due to bad arguments. + +Refs #7931 From 244e1cf8a9f6065fd4dfe9546b1310d121f209d4 Mon Sep 17 00:00:00 2001 From: Pierre Sassoulas Date: Wed, 14 Dec 2022 23:07:23 +0100 Subject: [PATCH 6/6] Update doc/whatsnew/fragments/7931.bugfix --- doc/whatsnew/fragments/7931.bugfix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/whatsnew/fragments/7931.bugfix b/doc/whatsnew/fragments/7931.bugfix index 19789aa007..fe42346f43 100644 --- a/doc/whatsnew/fragments/7931.bugfix +++ b/doc/whatsnew/fragments/7931.bugfix @@ -1,3 +1,3 @@ -Fix exit code when argparse raises SystemExit due to bad arguments. +When pylint exit due to bad arguments being provided the exit code will now be the expected ``32``. Refs #7931