From 509fb219870bb572e6cdb373d5f5bc175f5396fc Mon Sep 17 00:00:00 2001 From: Dennis Keck <26092524+fellhorn@users.noreply.github.com> Date: Sun, 25 Feb 2024 21:08:08 +0100 Subject: [PATCH 01/11] Fix: "All" should not disable pylint internal messages Signed-off-by: Dennis Keck <26092524+fellhorn@users.noreply.github.com> --- pylint/constants.py | 20 ++++++++++++++++++++ pylint/lint/message_state_handler.py | 6 ++++++ 2 files changed, 26 insertions(+) diff --git a/pylint/constants.py b/pylint/constants.py index e51022e654..b1c7c68eda 100644 --- a/pylint/constants.py +++ b/pylint/constants.py @@ -42,6 +42,26 @@ MSG_TYPES_LONG: dict[str, str] = {v: k for k, v in MSG_TYPES.items()} MSG_TYPES_STATUS = {"I": 0, "C": 16, "R": 8, "W": 4, "E": 2, "F": 1} +PYLINT_MSGS = [ + "F0001", + "F0002", + "F0010", + "F0011", + "I0001", + "I0010", + "I0011", + "I0013", + "I0020", + "I0021", + "I0022", + "E0001", + "E0011", + "W0012", + "R0022", + "E0013", + "E0014", + "E0015", +] # You probably don't want to change the MAIN_CHECKER_NAME # This would affect rcfile generation and retro-compatibility diff --git a/pylint/lint/message_state_handler.py b/pylint/lint/message_state_handler.py index 26028f0fab..84a563abb2 100644 --- a/pylint/lint/message_state_handler.py +++ b/pylint/lint/message_state_handler.py @@ -15,6 +15,7 @@ MSG_STATE_SCOPE_MODULE, MSG_TYPES, MSG_TYPES_LONG, + PYLINT_MSGS, ) from pylint.interfaces import HIGH from pylint.message import MessageDefinition @@ -84,6 +85,11 @@ def _get_messages_to_set( message_definitions.extend( self._get_messages_to_set(_msgid, enable, ignore_unknown) ) + if not enable: + # "all" should not disable pylint's own warnings + message_definitions = list( + filter(lambda m: m.msgid not in PYLINT_MSGS, message_definitions) + ) return message_definitions # msgid is a category? From 54979847b5a369cf033882bac728710d0f1129a5 Mon Sep 17 00:00:00 2001 From: Dennis Keck <26092524+fellhorn@users.noreply.github.com> Date: Sat, 9 Mar 2024 11:07:01 +0100 Subject: [PATCH 02/11] Cleanup tests Signed-off-by: Dennis Keck <26092524+fellhorn@users.noreply.github.com> --- tests/test_self.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/tests/test_self.py b/tests/test_self.py index 023fbd8c99..62c867dbdb 100644 --- a/tests/test_self.py +++ b/tests/test_self.py @@ -215,8 +215,13 @@ def test_error_missing_arguments(self) -> None: def test_disable_all(self) -> None: out = StringIO() - self._runtest([UNNECESSARY_LAMBDA, "--disable=all"], out=out, code=32) - assert "No files to lint: exiting." in out.getvalue().strip() + self._runtest([UNNECESSARY_LAMBDA, "--disable=all"], out=out, code=0) + assert "Your code has been rated at 10.00/10" in out.getvalue().strip() + + def test_disable_all_enable_invalid(self) -> None: + out = StringIO() + self._runtest([UNNECESSARY_LAMBDA, "--disable=all", "--enable=foo"], out=out, code=0) + assert "W0012: Unknown option value for '--enable', expected a valid pylint message and got 'foo'" in out.getvalue().strip() def test_output_with_verbose(self) -> None: out = StringIO() From 302e5135fb9d0a1f556422a0273a99010c5e1a74 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 9 Mar 2024 10:39:30 +0000 Subject: [PATCH 03/11] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_self.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/tests/test_self.py b/tests/test_self.py index 62c867dbdb..405ba5a1c3 100644 --- a/tests/test_self.py +++ b/tests/test_self.py @@ -220,8 +220,13 @@ def test_disable_all(self) -> None: def test_disable_all_enable_invalid(self) -> None: out = StringIO() - self._runtest([UNNECESSARY_LAMBDA, "--disable=all", "--enable=foo"], out=out, code=0) - assert "W0012: Unknown option value for '--enable', expected a valid pylint message and got 'foo'" in out.getvalue().strip() + self._runtest( + [UNNECESSARY_LAMBDA, "--disable=all", "--enable=foo"], out=out, code=0 + ) + assert ( + "W0012: Unknown option value for '--enable', expected a valid pylint message and got 'foo'" + in out.getvalue().strip() + ) def test_output_with_verbose(self) -> None: out = StringIO() From 8b0e21d03834f26779c703be0f54e401950d4aba Mon Sep 17 00:00:00 2001 From: Dennis Keck <26092524+fellhorn@users.noreply.github.com> Date: Sun, 2 Jun 2024 13:33:18 +0200 Subject: [PATCH 04/11] Fix test_disable_all Signed-off-by: Dennis Keck <26092524+fellhorn@users.noreply.github.com> --- pylint/lint/run.py | 8 +++----- tests/test_self.py | 4 ++-- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/pylint/lint/run.py b/pylint/lint/run.py index 1a8d594a04..fbd72ab2ca 100644 --- a/pylint/lint/run.py +++ b/pylint/lint/run.py @@ -20,7 +20,7 @@ from pylint.config.config_initialization import _config_initialization from pylint.config.exceptions import ArgumentPreprocessingError from pylint.config.utils import _preprocess_options -from pylint.constants import full_version +from pylint.constants import full_version, PYLINT_MSGS from pylint.lint.base_options import _make_run_options from pylint.lint.pylinter import MANAGER, PyLinter from pylint.reporters.base_reporter import BaseReporter @@ -175,10 +175,8 @@ def __init__( sys.exit(code) return - # Display help if there are no files to lint or no checks enabled - if not args or len(linter.config.disable) == len( - linter.msgs_store._messages_definitions - ): + # Display help if there are no files to lint or only internal checks enabled (`--disable=all`) + if not args or set([m.msgid for m in linter.messages]) == set(PYLINT_MSGS): print("No files to lint: exiting.") sys.exit(32) diff --git a/tests/test_self.py b/tests/test_self.py index 405ba5a1c3..bdc8b4e383 100644 --- a/tests/test_self.py +++ b/tests/test_self.py @@ -215,8 +215,8 @@ def test_error_missing_arguments(self) -> None: def test_disable_all(self) -> None: out = StringIO() - self._runtest([UNNECESSARY_LAMBDA, "--disable=all"], out=out, code=0) - assert "Your code has been rated at 10.00/10" in out.getvalue().strip() + self._runtest([UNNECESSARY_LAMBDA, "--disable=all"], out=out, code=32) + assert "No files to lint: exiting." in out.getvalue().strip() def test_disable_all_enable_invalid(self) -> None: out = StringIO() From 40482735f2c2ff67f38c409c6f1f09c2e8f47085 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 2 Jun 2024 11:39:20 +0000 Subject: [PATCH 05/11] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- pylint/lint/run.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pylint/lint/run.py b/pylint/lint/run.py index fbd72ab2ca..da16054d0d 100644 --- a/pylint/lint/run.py +++ b/pylint/lint/run.py @@ -20,7 +20,7 @@ from pylint.config.config_initialization import _config_initialization from pylint.config.exceptions import ArgumentPreprocessingError from pylint.config.utils import _preprocess_options -from pylint.constants import full_version, PYLINT_MSGS +from pylint.constants import PYLINT_MSGS, full_version from pylint.lint.base_options import _make_run_options from pylint.lint.pylinter import MANAGER, PyLinter from pylint.reporters.base_reporter import BaseReporter From 350927040858e8755d4d8ef74b70cca7b56adf10 Mon Sep 17 00:00:00 2001 From: Dennis Keck <26092524+fellhorn@users.noreply.github.com> Date: Sun, 2 Jun 2024 16:53:31 +0200 Subject: [PATCH 06/11] Reduce pylint messages Signed-off-by: Dennis Keck <26092524+fellhorn@users.noreply.github.com> --- pylint/constants.py | 6 ------ pylint/lint/run.py | 4 +++- tests/test_self.py | 12 +++++++++++- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/pylint/constants.py b/pylint/constants.py index a87568a10e..a6f4b84405 100644 --- a/pylint/constants.py +++ b/pylint/constants.py @@ -48,12 +48,6 @@ "F0002", "F0010", "F0011", - "I0001", - "I0010", - "I0011", - "I0013", - "I0020", - "I0021", "I0022", "E0001", "E0011", diff --git a/pylint/lint/run.py b/pylint/lint/run.py index da16054d0d..76d8c15bb8 100644 --- a/pylint/lint/run.py +++ b/pylint/lint/run.py @@ -176,7 +176,9 @@ def __init__( return # Display help if there are no files to lint or only internal checks enabled (`--disable=all`) - if not args or set([m.msgid for m in linter.messages]) == set(PYLINT_MSGS): + if not args or len(linter.msgs_store.messages) - len( + linter.config.disable + ) == len(PYLINT_MSGS): print("No files to lint: exiting.") sys.exit(32) diff --git a/tests/test_self.py b/tests/test_self.py index 3add00bf01..821d051ba2 100644 --- a/tests/test_self.py +++ b/tests/test_self.py @@ -220,9 +220,19 @@ def test_disable_all(self) -> None: assert "No files to lint: exiting." in out.getvalue().strip() def test_disable_all_enable_invalid(self) -> None: + # Reproduces issue #9403. If disable=all is used no error was raised for invalid messages unless + # unknown-option-value was manually enabled. out = StringIO() self._runtest( - [UNNECESSARY_LAMBDA, "--disable=all", "--enable=foo"], out=out, code=0 + # Enable one valid message to not run into "No files to lint: exiting." + [ + UNNECESSARY_LAMBDA, + "--disable=all", + "--enable=import-error", + "--enable=foo", + ], + out=out, + code=0, ) assert ( "W0012: Unknown option value for '--enable', expected a valid pylint message and got 'foo'" From 7809902c713da9fd8adecdf585c6467727fc5412 Mon Sep 17 00:00:00 2001 From: Dennis Keck <26092524+fellhorn@users.noreply.github.com> Date: Sun, 16 Jun 2024 10:48:59 +0200 Subject: [PATCH 07/11] Fix No files to lint Signed-off-by: Dennis Keck <26092524+fellhorn@users.noreply.github.com> --- pylint/constants.py | 1 - pylint/lint/run.py | 8 +++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/pylint/constants.py b/pylint/constants.py index a6f4b84405..7cb210c196 100644 --- a/pylint/constants.py +++ b/pylint/constants.py @@ -48,7 +48,6 @@ "F0002", "F0010", "F0011", - "I0022", "E0001", "E0011", "W0012", diff --git a/pylint/lint/run.py b/pylint/lint/run.py index 76d8c15bb8..9c47b1da03 100644 --- a/pylint/lint/run.py +++ b/pylint/lint/run.py @@ -176,9 +176,11 @@ def __init__( return # Display help if there are no files to lint or only internal checks enabled (`--disable=all`) - if not args or len(linter.msgs_store.messages) - len( - linter.config.disable - ) == len(PYLINT_MSGS): + if not args or ( + len(linter.config.enable) == 0 + and len(linter.config.disable) + == len(linter.msgs_store.messages) - len(PYLINT_MSGS) + ): print("No files to lint: exiting.") sys.exit(32) From 453c9e10a75975d13cc9d029c43e424f4a34c503 Mon Sep 17 00:00:00 2001 From: Dennis Keck <26092524+fellhorn@users.noreply.github.com> Date: Sun, 14 Jul 2024 21:06:06 +0200 Subject: [PATCH 08/11] Remove constant Signed-off-by: Dennis Keck <26092524+fellhorn@users.noreply.github.com> --- pylint/constants.py | 13 ------------- pylint/lint/message_state_handler.py | 13 ++++++++++--- pylint/lint/run.py | 4 ++-- 3 files changed, 12 insertions(+), 18 deletions(-) diff --git a/pylint/constants.py b/pylint/constants.py index 7cb210c196..f147e5189a 100644 --- a/pylint/constants.py +++ b/pylint/constants.py @@ -43,19 +43,6 @@ MSG_TYPES_LONG: dict[str, str] = {v: k for k, v in MSG_TYPES.items()} MSG_TYPES_STATUS = {"I": 0, "C": 16, "R": 8, "W": 4, "E": 2, "F": 1} -PYLINT_MSGS = [ - "F0001", - "F0002", - "F0010", - "F0011", - "E0001", - "E0011", - "W0012", - "R0022", - "E0013", - "E0014", - "E0015", -] # You probably don't want to change the MAIN_CHECKER_NAME # This would affect rcfile generation and retro-compatibility diff --git a/pylint/lint/message_state_handler.py b/pylint/lint/message_state_handler.py index 7cb54afc7c..5c4498af96 100644 --- a/pylint/lint/message_state_handler.py +++ b/pylint/lint/message_state_handler.py @@ -15,11 +15,10 @@ MSG_STATE_SCOPE_MODULE, MSG_TYPES, MSG_TYPES_LONG, - PYLINT_MSGS, ) from pylint.interfaces import HIGH from pylint.message import MessageDefinition -from pylint.typing import ManagedMessage +from pylint.typing import ManagedMessage, MessageDefinitionTuple from pylint.utils.pragma_parser import ( OPTION_PO, InvalidPragmaError, @@ -38,6 +37,11 @@ class _MessageStateHandler: def __init__(self, linter: PyLinter) -> None: self.linter = linter + self.default_enabled_messages: dict[str, MessageDefinitionTuple] = { + k: v + for k, v in self.linter.msgs.items() + if len(v) == 3 or v[3].get("default_enabled", True) + } self._msgs_state: dict[str, bool] = {} self._options_methods = { "enable": self.enable, @@ -88,7 +92,10 @@ def _get_messages_to_set( if not enable: # "all" should not disable pylint's own warnings message_definitions = list( - filter(lambda m: m.msgid not in PYLINT_MSGS, message_definitions) + filter( + lambda m: m.msgid not in self.default_enabled_messages, + message_definitions, + ) ) return message_definitions diff --git a/pylint/lint/run.py b/pylint/lint/run.py index 9c47b1da03..a266157151 100644 --- a/pylint/lint/run.py +++ b/pylint/lint/run.py @@ -20,7 +20,7 @@ from pylint.config.config_initialization import _config_initialization from pylint.config.exceptions import ArgumentPreprocessingError from pylint.config.utils import _preprocess_options -from pylint.constants import PYLINT_MSGS, full_version +from pylint.constants import full_version from pylint.lint.base_options import _make_run_options from pylint.lint.pylinter import MANAGER, PyLinter from pylint.reporters.base_reporter import BaseReporter @@ -179,7 +179,7 @@ def __init__( if not args or ( len(linter.config.enable) == 0 and len(linter.config.disable) - == len(linter.msgs_store.messages) - len(PYLINT_MSGS) + == len(linter.msgs_store.messages) - len(linter.default_enabled_messages) ): print("No files to lint: exiting.") sys.exit(32) From 397f9eb0c7d7dc1b80806ac23652cfc1cc9339a3 Mon Sep 17 00:00:00 2001 From: Dennis Keck <26092524+fellhorn@users.noreply.github.com> Date: Mon, 29 Jul 2024 09:53:42 +0200 Subject: [PATCH 09/11] Handle 401 disabled messages correctly Signed-off-by: Dennis Keck <26092524+fellhorn@users.noreply.github.com> --- pylint/lint/run.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pylint/lint/run.py b/pylint/lint/run.py index a266157151..2bbbb337b9 100644 --- a/pylint/lint/run.py +++ b/pylint/lint/run.py @@ -176,10 +176,12 @@ def __init__( return # Display help if there are no files to lint or only internal checks enabled (`--disable=all`) + disable_all_msg_set = set( + msg.symbol for msg in linter.msgs_store.messages + ) - set(msg[1] for msg in linter.default_enabled_messages.values()) if not args or ( len(linter.config.enable) == 0 - and len(linter.config.disable) - == len(linter.msgs_store.messages) - len(linter.default_enabled_messages) + and set(linter.config.disable) == disable_all_msg_set ): print("No files to lint: exiting.") sys.exit(32) From 6548d2c4cb3dd8e5296ac432bfecbcd532a44508 Mon Sep 17 00:00:00 2001 From: Dennis Keck <26092524+fellhorn@users.noreply.github.com> Date: Mon, 29 Jul 2024 11:53:00 +0200 Subject: [PATCH 10/11] Add whatsnew fragment Signed-off-by: Dennis Keck <26092524+fellhorn@users.noreply.github.com> --- doc/whatsnew/fragments/9403.bugfix | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 doc/whatsnew/fragments/9403.bugfix diff --git a/doc/whatsnew/fragments/9403.bugfix b/doc/whatsnew/fragments/9403.bugfix new file mode 100644 index 0000000000..475c3bd4c8 --- /dev/null +++ b/doc/whatsnew/fragments/9403.bugfix @@ -0,0 +1,3 @@ +`--enable` with `--disable=all` now produces an error, when an unknown msg code is used. Internal `pylint` messages are no longer affected by `--disable=all`. + +Refs #9403 From f5e528c0c8949090fcc11e0e2a13485e4059c929 Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Mon, 29 Jul 2024 07:15:05 -0400 Subject: [PATCH 11/11] [skip ci] Refs -> Closes --- doc/whatsnew/fragments/9403.bugfix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/whatsnew/fragments/9403.bugfix b/doc/whatsnew/fragments/9403.bugfix index 475c3bd4c8..478f5978fe 100644 --- a/doc/whatsnew/fragments/9403.bugfix +++ b/doc/whatsnew/fragments/9403.bugfix @@ -1,3 +1,3 @@ `--enable` with `--disable=all` now produces an error, when an unknown msg code is used. Internal `pylint` messages are no longer affected by `--disable=all`. -Refs #9403 +Closes #9403