Skip to content

Commit

Permalink
Unify use-parent-configs and use-local-configs
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksey Petryankin committed Feb 10, 2024
1 parent 66f2cf1 commit 6a5b411
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 37 deletions.
11 changes: 1 addition & 10 deletions doc/user_guide/configuration/all-options.rst
Original file line number Diff line number Diff line change
Expand Up @@ -218,14 +218,7 @@ Standard Checkers

--use-local-configs
"""""""""""""""""""
*When some of the linted files or modules have pylint config in the same directory, use their local configs for checking these files.*

**Default:** ``False``


--use-parent-configs
""""""""""""""""""""
*Search for local pylint configs up until current working directory or root.*
*When some of the linted modules have a pylint config in the same directory (or one of the parent directories), use this config for checking these files.*

**Default:** ``False``

Expand Down Expand Up @@ -301,8 +294,6 @@ Standard Checkers
use-local-configs = false
use-parent-configs = false
.. raw:: html
Expand Down
8 changes: 3 additions & 5 deletions doc/whatsnew/fragments/618.feature
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
Add 2 new command line options: use-local-configs and use-parent-configs.
Add new command line option: use-local-configs.

use-local-configs enables searching for local pylint configurations in the same directory where linted file is located.
use-local-configs enables searching for local pylint configurations in the same directory where linted file is located and upwards until $PWD or root.
For example:
if there exists package/pylintrc, then
pylint --use-local-configs=y package/file.py
will use package/pylintrc instead of default config from $PWD.

use-parent-configs enables searching for local pylint configurations upwards from the directory where linted file is located.
For example:
if there exists package/pylintrc, and doesn't exist package/subpackage/pylintrc, then
pylint --use-local-configs=y --use-parent-configs=y package/subpackage/file.py
pylint --use-local-configs=y package/subpackage/file.py
will use package/pylintrc instead of default config from $PWD.

Closes #618
13 changes: 2 additions & 11 deletions pylint/lint/base_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,17 +420,8 @@ def _make_linter_options(linter: PyLinter) -> Options:
"default": False,
"type": "yn",
"metavar": "<y or n>",
"help": "When some of the linted files or modules have pylint config in the same directory, "
"use their local configs for checking these files.",
},
),
(
"use-parent-configs",
{
"default": False,
"type": "yn",
"metavar": "<y or n>",
"help": "Search for local pylint configs up until current working directory or root.",
"help": "When some of the linted modules have a pylint config in the same directory "
"(or one of the parent directories), use this config for checking these files.",
},
),
)
Expand Down
5 changes: 1 addition & 4 deletions pylint/lint/pylinter.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,10 +624,7 @@ def register_local_config(self, file_or_dir: str) -> None:
else:
basedir = Path(os.path.dirname(file_or_dir))

if self.config.use_parent_configs is False:
# exit loop after first iteration
scan_root_dir = basedir
elif _is_relative_to(basedir, Path(os.getcwd())):
if _is_relative_to(basedir, Path(os.getcwd())):
scan_root_dir = Path(os.getcwd())
else:
scan_root_dir = Path("/")
Expand Down
10 changes: 3 additions & 7 deletions tests/config/test_per_directory_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,9 @@ def _create_subconfig_test_fs(tmp_path: Path) -> tuple[Path, ...]:
return level1_dir, test_file1, test_file2, test_file3, test_file4


# check that use-parent-configs doesn't break anything
@pytest.mark.parametrize(
"local_config_args",
[["--use-local-configs=y"], ["--use-local-configs=y", "--use-parent-configs=y"]],
[["--use-local-configs=y"]],
)
# check modules and use of configuration files from top-level package or subpackage
@pytest.mark.parametrize("test_file_index", [0, 1, 2])
Expand Down Expand Up @@ -119,10 +118,9 @@ def test_subconfig_vs_root_config(
assert "LEVEL1" not in output[2], assert_message


# check that use-parent-configs doesn't break anything
@pytest.mark.parametrize(
"local_config_args",
[["--use-local-configs=y"], ["--use-local-configs=y", "--use-parent-configs=y"]],
[["--use-local-configs=y"]],
)
# check cases when test_file without local config belongs to cwd subtree or not
@pytest.mark.parametrize(
Expand Down Expand Up @@ -203,9 +201,7 @@ def test_subconfig_in_parent(tmp_path: Path, capsys: CaptureFixture) -> None:
test_file = _create_parent_subconfig_fs(tmp_path)
orig_cwd = os.getcwd()
os.chdir(tmp_path)
LintRun(
["--use-parent-configs=y", "--use-local-configs=y", str(test_file)], exit=False
)
LintRun(["--use-local-configs=y", str(test_file)], exit=False)
output1 = capsys.readouterr().out.replace("\\n", "\n")
os.chdir(orig_cwd)

Expand Down

0 comments on commit 6a5b411

Please sign in to comment.