Skip to content

Commit 44a3d96

Browse files
Fix the use of abbreviations for preprocessable options on the CLI (#6820)
Co-authored-by: Pierre Sassoulas <[email protected]>
1 parent 76dcd07 commit 44a3d96

File tree

3 files changed

+52
-10
lines changed

3 files changed

+52
-10
lines changed

doc/whatsnew/2/2.14/full.rst

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ Release date: TBA
1313

1414
Closes #6802
1515

16+
* Fixed the use of abbreviations for some special options on the command line.
17+
18+
Closes #6810
19+
1620

1721
What's New in Pylint 2.14.0?
1822
----------------------------

pylint/config/utils.py

+33-10
Original file line numberDiff line numberDiff line change
@@ -201,16 +201,30 @@ def _enable_all_extensions(run: Run, value: str | None) -> None:
201201

202202

203203
PREPROCESSABLE_OPTIONS: dict[
204-
str, tuple[bool, Callable[[Run, str | None], None]]
204+
str, tuple[bool, Callable[[Run, str | None], None], int]
205205
] = { # pylint: disable=consider-using-namedtuple-or-dataclass
206-
"--init-hook": (True, _init_hook),
207-
"--rcfile": (True, _set_rcfile),
208-
"--output": (True, _set_output),
209-
"--load-plugins": (True, _add_plugins),
210-
"--verbose": (False, _set_verbose_mode),
211-
"-v": (False, _set_verbose_mode),
212-
"--enable-all-extensions": (False, _enable_all_extensions),
206+
# pylint: disable=wrong-spelling-in-comment
207+
# Argparse by default allows abbreviations. It behaves differently
208+
# if you turn this off, so we also turn it on. We mimick this
209+
# by allowing some abbreviations or incorrect spelling here.
210+
# The integer at the end of the tuple indicates how many letters
211+
# should match, include the '-'. 0 indicates a full match.
212+
#
213+
# Clashes with --init-(import)
214+
"--init-hook": (True, _init_hook, 8),
215+
# Clashes with --r(ecursive)
216+
"--rcfile": (True, _set_rcfile, 4),
217+
# Clashes with --output(-format)
218+
"--output": (True, _set_output, 0),
219+
# Clashes with --lo(ng-help)
220+
"--load-plugins": (True, _add_plugins, 5),
221+
# Clashes with --v(ariable-rgx)
222+
"--verbose": (False, _set_verbose_mode, 4),
223+
"-v": (False, _set_verbose_mode, 2),
224+
# Clashes with --enable
225+
"--enable-all-extensions": (False, _enable_all_extensions, 9),
213226
}
227+
# pylint: enable=wrong-spelling-in-comment
214228

215229

216230
def _preprocess_options(run: Run, args: Sequence[str]) -> list[str]:
@@ -230,12 +244,21 @@ def _preprocess_options(run: Run, args: Sequence[str]) -> list[str]:
230244
except ValueError:
231245
option, value = argument, None
232246

233-
if option not in PREPROCESSABLE_OPTIONS:
247+
matched_option = None
248+
for option_name, data in PREPROCESSABLE_OPTIONS.items():
249+
to_match = data[2]
250+
if to_match == 0:
251+
if option == option_name:
252+
matched_option = option_name
253+
elif option.startswith(option_name[:to_match]):
254+
matched_option = option_name
255+
256+
if matched_option is None:
234257
processed_args.append(argument)
235258
i += 1
236259
continue
237260

238-
takearg, cb = PREPROCESSABLE_OPTIONS[option]
261+
takearg, cb, _ = PREPROCESSABLE_OPTIONS[matched_option]
239262

240263
if takearg and value is None:
241264
i += 1

tests/config/test_find_default_config_files.py

+15
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,21 @@ def test_verbose_output_no_config(capsys: CaptureFixture) -> None:
165165
assert "No config file found, using default configuration" in out.err
166166

167167

168+
@pytest.mark.usefixtures("pop_pylintrc")
169+
def test_verbose_abbreviation(capsys: CaptureFixture) -> None:
170+
"""Test that we correctly handle an abbreviated pre-processable option."""
171+
with tempdir() as chroot:
172+
with fake_home():
173+
chroot_path = Path(chroot)
174+
testutils.create_files(["a/b/c/d/__init__.py"])
175+
os.chdir(chroot_path / "a/b/c")
176+
with pytest.raises(SystemExit):
177+
Run(["--ve"])
178+
out = capsys.readouterr()
179+
# This output only exists when launched in verbose mode
180+
assert "No config file found, using default configuration" in out.err
181+
182+
168183
@pytest.mark.parametrize(
169184
"content,expected",
170185
[

0 commit comments

Comments
 (0)