Skip to content

Commit

Permalink
Review-changes 3: add verbose messages about parsed and used local co…
Browse files Browse the repository at this point in the history
…nfigs
  • Loading branch information
Aleksey Petryankin committed Mar 2, 2024
1 parent d7674fb commit 0993f8d
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pylint/config/config_file_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def parse_config_file(
raise OSError(f"The config file {file_path} doesn't exist!")

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

if file_path.suffix == ".toml":
return _RawConfParser.parse_toml_file(file_path)
Expand Down
3 changes: 3 additions & 0 deletions pylint/config/config_initialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ def _config_initialization(
args_list = _order_all_first(args_list, joined=True)
parsed_args_list = linter._parse_command_line_configuration(args_list)

# save preprocessed Runner.verbose to config
linter.config.verbose = verbose_mode

# Remove the positional arguments separator from the list of arguments if it exists
try:
parsed_args_list.remove("--")
Expand Down
23 changes: 16 additions & 7 deletions pylint/lint/pylinter.py
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,11 @@ def register_local_config(self, file_or_dir: str) -> None:
original_config_ref = self.config
self.config = copy.deepcopy(self.config)
_config_initialization(
self, self._cli_args, reporter=self.reporter, config_file=local_conf
self,
self._cli_args,
reporter=self.reporter,
config_file=local_conf,
verbose_mode=self.config.verbose,
)
self._directory_namespaces[basedir.resolve()] = (self.config, {})
# keep dict keys reverse-sorted so that
Expand Down Expand Up @@ -958,24 +962,29 @@ def set_current_module(self, modname: str, filepath: str | None = None) -> None:
# If there is an actual filepath we might need to update the config attribute
if filepath and self.config.use_local_configs:
self.register_local_config(filepath)
namespace = self._get_namespace_for_file(
config_path, namespace = self._get_namespace_for_file(
Path(filepath), self._directory_namespaces
)
if namespace:
self.config = namespace or self._base_config
self.config = namespace
if self.config.verbose:
print(
f"Using config from {config_path} for {modname}",
file=sys.stderr,
)

def _get_namespace_for_file(
self, filepath: Path, namespaces: DirectoryNamespaceDict
) -> argparse.Namespace | None:
) -> tuple[Path | None, argparse.Namespace | None]:
filepath = filepath.resolve()
for directory in namespaces:
if _is_relative_to(filepath, directory):
namespace = self._get_namespace_for_file(
_, namespace = self._get_namespace_for_file(
filepath, namespaces[directory][1]
)
if namespace is None:
return namespaces[directory][0]
return None
return directory, namespaces[directory][0]
return None, None

@contextlib.contextmanager
def _astroid_module_checker(
Expand Down
2 changes: 1 addition & 1 deletion tests/config/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def test_short_verbose(capsys: CaptureFixture) -> None:
"""Check that we correctly handle the -v flag."""
Run([str(EMPTY_MODULE), "-v"], exit=False)
output = capsys.readouterr()
assert "Using config file" in output.err
assert "Loading config file" in output.err


def test_argument_separator() -> None:
Expand Down
10 changes: 10 additions & 0 deletions tests/config/test_per_directory_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,13 @@ def test_register_local_config_accepts_directory(
assert level1_dir.is_dir()
linter.register_local_config(str(level1_dir))
assert level1_dir in linter._directory_namespaces.keys()


def test_local_config_verbose(
_create_subconfig_test_fs: tuple[Path, ...], capsys: CaptureFixture
) -> None:
"""Check --verbose flag prints message about current config for each file."""
level1_dir, *tmp_files = _create_subconfig_test_fs
LintRun(["--verbose", "--use-local-configs=y", str(tmp_files[1])], exit=False)
output = capsys.readouterr()
assert f"Using config from {level1_dir / 'sub'}" in output.err

0 comments on commit 0993f8d

Please sign in to comment.