Skip to content

Commit 655ef69

Browse files
author
Aleksey Petryankin
committed
Review-changes 3: add verbose messages about local configs
1 parent 949346f commit 655ef69

File tree

5 files changed

+31
-9
lines changed

5 files changed

+31
-9
lines changed

pylint/config/config_file_parser.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def parse_config_file(
106106
raise OSError(f"The config file {file_path} doesn't exist!")
107107

108108
if verbose:
109-
print(f"Using config file {file_path}", file=sys.stderr)
109+
print(f"Loading config file {file_path}", file=sys.stderr)
110110

111111
if file_path.suffix == ".toml":
112112
return _RawConfParser.parse_toml_file(file_path)

pylint/config/config_initialization.py

+3
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ def _config_initialization(
8383
args_list = _order_all_first(args_list, joined=True)
8484
parsed_args_list = linter._parse_command_line_configuration(args_list)
8585

86+
# save preprocessed Runner.verbose to config
87+
linter.config.verbose = verbose_mode
88+
8689
# Remove the positional arguments separator from the list of arguments if it exists
8790
try:
8891
parsed_args_list.remove("--")

pylint/lint/pylinter.py

+16-7
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,11 @@ def register_local_config(self, file_or_dir: str) -> None:
639639
original_config_ref = self.config
640640
self.config = copy.deepcopy(self.config)
641641
_config_initialization(
642-
self, self._cli_args, reporter=self.reporter, config_file=local_conf
642+
self,
643+
self._cli_args,
644+
reporter=self.reporter,
645+
config_file=local_conf,
646+
verbose_mode=self.config.verbose,
643647
)
644648
self._directory_namespaces[basedir.resolve()] = (self.config, {})
645649
# keep dict keys reverse-sorted so that
@@ -958,24 +962,29 @@ def set_current_module(self, modname: str, filepath: str | None = None) -> None:
958962
# If there is an actual filepath we might need to update the config attribute
959963
if filepath and self.config.use_local_configs:
960964
self.register_local_config(filepath)
961-
namespace = self._get_namespace_for_file(
965+
config_path, namespace = self._get_namespace_for_file(
962966
Path(filepath), self._directory_namespaces
963967
)
964968
if namespace:
965-
self.config = namespace or self._base_config
969+
self.config = namespace
970+
if self.config.verbose:
971+
print(
972+
f"Using config from {config_path} for {filepath}",
973+
file=sys.stderr,
974+
)
966975

967976
def _get_namespace_for_file(
968977
self, filepath: Path, namespaces: DirectoryNamespaceDict
969-
) -> argparse.Namespace | None:
978+
) -> tuple[Path | None, argparse.Namespace | None]:
970979
filepath = filepath.resolve()
971980
for directory in namespaces:
972981
if _is_relative_to(filepath, directory):
973-
namespace = self._get_namespace_for_file(
982+
_, namespace = self._get_namespace_for_file(
974983
filepath, namespaces[directory][1]
975984
)
976985
if namespace is None:
977-
return namespaces[directory][0]
978-
return None
986+
return directory, namespaces[directory][0]
987+
return None, None
979988

980989
@contextlib.contextmanager
981990
def _astroid_module_checker(

tests/config/test_config.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ def test_short_verbose(capsys: CaptureFixture) -> None:
178178
"""Check that we correctly handle the -v flag."""
179179
Run([str(EMPTY_MODULE), "-v"], exit=False)
180180
output = capsys.readouterr()
181-
assert "Using config file" in output.err
181+
assert "Loading config file" in output.err
182182

183183

184184
def test_argument_separator() -> None:

tests/config/test_per_directory_config.py

+10
Original file line numberDiff line numberDiff line change
@@ -240,3 +240,13 @@ def test_register_local_config_accepts_directory(
240240
assert level1_dir.is_dir()
241241
linter.register_local_config(str(level1_dir))
242242
assert level1_dir in linter._directory_namespaces.keys()
243+
244+
245+
def test_local_config_verbose(
246+
_create_subconfig_test_fs: tuple[Path, ...], capsys: CaptureFixture
247+
) -> None:
248+
"""Check --verbose flag prints message about current config for each file."""
249+
level1_dir, *tmp_files = _create_subconfig_test_fs
250+
LintRun(["--verbose", "--use-local-configs=y", str(tmp_files[1])], exit=False)
251+
output = capsys.readouterr()
252+
assert f"Using config from {level1_dir / 'sub'}" in output.err

0 commit comments

Comments
 (0)