Skip to content

Commit 75fdce0

Browse files
DanielNoordPierre-Sassoulas
authored andcommitted
Add the files option
1 parent 03bd452 commit 75fdce0

File tree

5 files changed

+72
-7
lines changed

5 files changed

+72
-7
lines changed

doc/whatsnew/fragments/5701.other

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
You can now set the ``files`` option in configuration files and on the command line.
2+
Passing files without the ``--files`` flag is still supported. This allows to set
3+
``files`` to ``files = my_source_directory`` and invoking ``pylint`` with only
4+
the ``pylint`` command similar to how other CLI tools allow to do so.
5+
The help message can always be invoked with ``pylint -h`` or ``pylint --help``.
6+
7+
Closes #5701

pylint/config/config_initialization.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def _config_initialization(
2323
reporter: reporters.BaseReporter | reporters.MultiReporter | None = None,
2424
config_file: None | str | Path = None,
2525
verbose_mode: bool = False,
26-
) -> list[str]:
26+
) -> None:
2727
"""Parse all available options, read config files and command line arguments and
2828
set options accordingly.
2929
"""
@@ -119,5 +119,7 @@ def _config_initialization(
119119
linter._directory_namespaces[Path(".").resolve()] = (linter.config, {})
120120

121121
# parsed_args_list should now only be a list of files/directories to lint.
122-
# All other options have been removed from the list.
123-
return parsed_args_list
122+
# All other options have been removed from the list. If there is anything
123+
# left we overwrite any 'files' as given by the configuration file.
124+
if parsed_args_list:
125+
linter.config.files = parsed_args_list

pylint/lint/base_options.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,18 @@ def _make_linter_options(linter: PyLinter) -> Options:
401401
"Useful if running pylint in a server-like mode.",
402402
},
403403
),
404+
(
405+
"files",
406+
{
407+
"type": "csv",
408+
"default": [],
409+
"help": "The files to lint. The flag can also be omitted as pylint will "
410+
"try to lint any file passed as argument. This can be used to set files "
411+
"to a directory in a configuration file and invoke pylint by only typing "
412+
"pylint on the command line. Any file passed as argument will overwrite any "
413+
"file set in the configuration file.",
414+
},
415+
),
404416
)
405417

406418

pylint/lint/run.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ def __init__(
163163
if self._is_pylint_config:
164164
_register_generate_config_options(linter._arg_parser)
165165

166-
args = _config_initialization(
166+
_config_initialization(
167167
linter, args, reporter, config_file=self._rcfile, verbose_mode=self.verbose
168168
)
169169

@@ -179,7 +179,7 @@ def __init__(
179179
return
180180

181181
# Display help messages if there are no files to lint
182-
if not args:
182+
if not linter.config.files:
183183
print(linter.help())
184184
sys.exit(32)
185185

@@ -203,13 +203,13 @@ def __init__(
203203
try:
204204
with open(self._output, "w", encoding="utf-8") as output:
205205
linter.reporter.out = output
206-
linter.check(args)
206+
linter.check(linter.config.files)
207207
score_value = linter.generate_reports()
208208
except OSError as ex:
209209
print(ex, file=sys.stderr)
210210
sys.exit(32)
211211
else:
212-
linter.check(args)
212+
linter.check(linter.config.files)
213213
score_value = linter.generate_reports()
214214

215215
if do_exit is not UNUSED_PARAM_SENTINEL:

tests/config/unittest_config.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@
77
from __future__ import annotations
88

99
import re
10+
from pathlib import Path
1011

1112
import pytest
1213

1314
from pylint import config
1415
from pylint.checkers import BaseChecker
16+
from pylint.lint import Run as _Run
1517
from pylint.testutils import CheckerTestCase, set_config
18+
from pylint.testutils._run import _Run as Run
19+
from pylint.testutils.utils import _test_cwd
1620
from pylint.typing import MessageDefinitionTuple
1721

1822

@@ -86,3 +90,43 @@ def test_ignore_paths_with_no_value(self) -> None:
8690
options = self.linter.config.ignore_paths
8791

8892
assert options == []
93+
94+
95+
def test_files_can_be_set_in_config(tmp_path: Path) -> None:
96+
"""Test that the files option can be set in a config file."""
97+
with _test_cwd(tmp_path):
98+
bad_file = tmp_path / "bad.py"
99+
bad_file.write_text("1")
100+
good_file = tmp_path / "good.py"
101+
good_file.write_text("'''My module docstring'''\n")
102+
init_file = tmp_path / "__init__.py"
103+
init_file.write_text("")
104+
105+
# Test that we run on files set in the config file
106+
config_file = tmp_path / "pylintrc"
107+
config_file.write_text("[MASTER]\nfiles=good.py,bad.py")
108+
runner = Run(["--rcfile", str(config_file)], exit=False)
109+
assert runner.linter.stats.by_msg
110+
# Test that we can overrun the configuration file with a command line argument
111+
runner = Run(["good.py"], exit=False)
112+
assert not runner.linter.stats.by_msg
113+
# Or by supplying --files directly
114+
runner = Run(["--files", "good.py"], exit=False)
115+
assert not runner.linter.stats.by_msg
116+
117+
# Test that we can run on the current directory by specifying it
118+
config_file = tmp_path / "pylintrc"
119+
config_file.write_text("[MASTER]\nfiles=" + str(tmp_path))
120+
runner = Run(["--rcfile", str(config_file)], exit=False)
121+
assert runner.linter.stats.by_msg
122+
# Test that we can also use just the command 'pylint'. Using _Run
123+
# makes sure that the --rcfile option doesn't get patched.
124+
other_runner = _Run([], exit=False)
125+
assert other_runner.linter.stats.by_msg
126+
127+
# Test that we can also run on a directory set as files even if it is
128+
# not our current cwd
129+
config_file = tmp_path / "pylintrc"
130+
config_file.write_text("[MASTER]\nfiles=" + str(tmp_path))
131+
runner = Run(["--rcfile", str(config_file)], exit=False)
132+
assert runner.linter.stats.by_msg

0 commit comments

Comments
 (0)