Skip to content

Commit ac89c5f

Browse files
Add bad-option-value
Co-authored-by: Pierre Sassoulas <[email protected]>
1 parent 5d07894 commit ac89c5f

File tree

10 files changed

+47
-13
lines changed

10 files changed

+47
-13
lines changed

ChangeLog

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ Release date: TBA
9090

9191
Closes #5205
9292

93+
* ``bad-option-value`` will be emitted whenever a configuration value or command line invocation
94+
includes an unknown message.
95+
96+
Closes #4324
97+
98+
9399
* Added the ``generate-toml-config`` option.
94100

95101
Ref #5462

doc/whatsnew/2.14.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ Other Changes
104104

105105
Ref #5462
106106

107+
* ``bad-option-value`` will be emitted whenever a configuration value or command line invocation
108+
includes an unknown message.
109+
110+
Closes #4324
111+
107112
* Fix false negative for ``bad-string-format-type`` if the value to be formatted is passed in
108113
as a variable holding a constant.
109114

pylint/config/callback_actions.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -382,9 +382,8 @@ def __call__(
382382
try:
383383
self.linter.disable(msgid)
384384
except exceptions.UnknownMessageError:
385-
# pylint: disable-next=fixme
386-
# TODO: Optparse: Raise an informational warning here
387-
pass
385+
msg = f"{option_string}. Don't recognize message {msgid}."
386+
self.linter.add_message("bad-option-value", args=msg, line=0)
388387

389388

390389
class _EnableAction(_AccessLinterObjectAction):
@@ -403,9 +402,8 @@ def __call__(
403402
try:
404403
self.linter.enable(msgid)
405404
except exceptions.UnknownMessageError:
406-
# pylint: disable-next=fixme
407-
# TODO: Optparse: Raise an informational warning here
408-
pass
405+
msg = f"{option_string}. Don't recognize message {msgid}."
406+
self.linter.add_message("bad-option-value", args=msg, line=0)
409407

410408

411409
class _OutputFormatAction(_AccessLinterObjectAction):

pylint/config/config_initialization.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,18 @@ def _config_initialization(
5858
if reporter:
5959
linter.set_reporter(reporter)
6060

61+
# Set the current module to the command line
62+
# to allow raising messages on it
63+
linter.set_current_module("Command line")
64+
6165
# Now we parse any options from the command line, so they can override
6266
# the configuration file
6367
parsed_args_list = linter._parse_command_line_configuration(args_list)
6468

69+
# Set the current module to configuration as we don't know where
70+
# the --load-plugins key is coming from
71+
linter.set_current_module("Command line or configuration file")
72+
6573
# We have loaded configuration from config file and command line. Now, we can
6674
# load plugin specific configuration.
6775
linter.load_plugin_configuration()

pylint/lint/pylinter.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ def _load_reporter_by_class(reporter_class: str) -> type:
167167
"Used when an unknown inline option is encountered.",
168168
),
169169
"E0012": (
170-
"Bad option value %r",
170+
"Bad option value for %s",
171171
"bad-option-value",
172172
"Used when a bad value for an inline option is encountered.",
173173
),
@@ -936,8 +936,9 @@ def process_tokens(self, tokens):
936936
try:
937937
meth(msgid, "module", l_start)
938938
except exceptions.UnknownMessageError:
939+
msg = f"{pragma_repr.action}. Don't recognize message {msgid}."
939940
self.add_message(
940-
"bad-option-value", args=msgid, line=start[0]
941+
"bad-option-value", args=msg, line=start[0]
941942
)
942943
except UnRecognizedOptionError as err:
943944
self.add_message(
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
************* Module {abspath}
2-
{relpath}:1:0: E0013: Plugin 'pylint_websockets' is impossible to load, is it installed ? ('No module named 'pylint_websockets'') (bad-plugin-value)
1+
************* Module Command line or configuration file
2+
Command line or configuration file:1:0: E0013: Plugin 'pylint_websockets' is impossible to load, is it installed ? ('No module named 'pylint_websockets'') (bad-plugin-value)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
************* Module {abspath}
2+
{relpath}:1:0: E0012: Bad option value for --disable. Don't recognize message logging-not-layzy. (bad-option-value)
3+
{relpath}:1:0: E0012: Bad option value for --enable. Don't recognize message C00000. (bad-option-value)

tests/config/test_config.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,15 @@
77
import os
88
from pathlib import Path
99

10-
from pylint.lint.run import Run
10+
from pytest import CaptureFixture
11+
12+
from pylint.lint import Run
1113
from pylint.testutils.configuration_test import run_using_a_configuration_file
1214

15+
HERE = Path(__file__).parent.absolute()
16+
REGRTEST_DATA_DIR = HERE / ".." / "regrtest_data"
17+
EMPTY_MODULE = REGRTEST_DATA_DIR / "empty.py"
18+
1319

1420
def check_configuration_file_reader(
1521
runner: Run,
@@ -45,3 +51,10 @@ def test_can_read_toml_env_variable(tmp_path: Path, file_to_lint_path: str) -> N
4551
)
4652
mock_exit.assert_called_once_with(0)
4753
check_configuration_file_reader(runner)
54+
55+
56+
def test_unknown_message_id(capsys: CaptureFixture) -> None:
57+
"""Check that we correctly raise a message on an unknown id."""
58+
Run([str(EMPTY_MODULE), "--disable=12345"], exit=False)
59+
output = capsys.readouterr()
60+
assert "Command line:1:0: E0012: Bad option value for --disable." in output.out
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
bad-option-value:1:0:None:None::Bad option value 'W04044':UNDEFINED
1+
bad-option-value:1:0:None:None::Bad option value for enable. Don't recognize message W04044.:UNDEFINED

tests/functional/u/use/use_symbolic_message_instead.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
bad-option-value:1:0:None:None::Bad option value 'T1234':UNDEFINED
1+
bad-option-value:1:0:None:None::Bad option value for disable. Don't recognize message T1234.:UNDEFINED
22
use-symbolic-message-instead:1:0:None:None::"'C0111' is cryptic: use '# pylint: disable=missing-docstring' instead":UNDEFINED
33
use-symbolic-message-instead:1:0:None:None::"'R0903' is cryptic: use '# pylint: disable=too-few-public-methods' instead":UNDEFINED
44
use-symbolic-message-instead:2:0:None:None::"'c0111' is cryptic: use '# pylint: enable=missing-docstring' instead":UNDEFINED

0 commit comments

Comments
 (0)