Skip to content

Commit

Permalink
Support for resolving external toml files
Browse files Browse the repository at this point in the history
  • Loading branch information
Matej Spiller Muys authored and DanielNoord committed Nov 14, 2023
1 parent d42df4b commit 49358d4
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
4 changes: 4 additions & 0 deletions doc/user_guide/usage/run.rst
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,11 @@ command line using the ``--rcfile`` option. Otherwise, Pylint searches for a
configuration file in the following order and uses the first one it finds:

#. ``pylintrc`` in the current working directory
#. ``pylintrc.toml`` in the current working directory,
providing it has at least one ``tool.pylint.`` section.
#. ``.pylintrc`` in the current working directory
#. ``.pylintrc.toml`` in the current working directory,
providing it has at least one ``tool.pylint.`` section.
#. ``pyproject.toml`` in the current working directory,
providing it has at least one ``tool.pylint.`` section.
The ``pyproject.toml`` must prepend section names with ``tool.pylint.``,
Expand Down
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/9228.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Support for resolving external toml files named pylintrc.toml and .pylintrc.toml.

Closes #9228
7 changes: 6 additions & 1 deletion pylint/config/find_default_config_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@
else:
import tomli as tomllib

RC_NAMES = (Path("pylintrc"), Path(".pylintrc"))
RC_NAMES = (
Path("pylintrc"),
Path("pylintrc.toml"),
Path(".pylintrc"),
Path(".pylintrc.toml"),
)
PYPROJECT_NAME = Path("pyproject.toml")
CONFIG_NAMES = (*RC_NAMES, PYPROJECT_NAME, Path("setup.cfg"))

Expand Down
35 changes: 35 additions & 0 deletions tests/config/test_find_default_config_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,41 @@ def test_pylintrc_parentdir() -> None:
assert next(config.find_default_config_files()) == expected


@pytest.mark.usefixtures("pop_pylintrc")
def test_pylintrc_toml_parentdir() -> None:
"""Test that the first pylintrc.toml we find is the first parent directory."""
# pylint: disable=duplicate-code
with tempdir() as chroot:
chroot_path = Path(chroot)
files = [
"a/pylintrc.toml",
"a/b/__init__.py",
"a/b/pylintrc.toml",
"a/b/c/__init__.py",
"a/b/c/d/__init__.py",
"a/b/c/d/e/.pylintrc.toml",
]
testutils.create_files(files)
for config_file in files:
if config_file.endswith("pylintrc.toml"):
with open(config_file, "w", encoding="utf-8") as fd:
fd.write('[tool.pylint."messages control"]\n')

with fake_home():
assert not list(config.find_default_config_files())

results = {
"a": chroot_path / "a" / "pylintrc.toml",
"a/b": chroot_path / "a" / "b" / "pylintrc.toml",
"a/b/c": chroot_path / "a" / "b" / "pylintrc.toml",
"a/b/c/d": chroot_path / "a" / "b" / "pylintrc.toml",
"a/b/c/d/e": chroot_path / "a" / "b" / "c" / "d" / "e" / ".pylintrc.toml",
}
for basedir, expected in results.items():
os.chdir(chroot_path / basedir)
assert next(config.find_default_config_files()) == expected


@pytest.mark.usefixtures("pop_pylintrc")
def test_pyproject_toml_parentdir() -> None:
"""Test the search of pyproject.toml file in parent directories"""
Expand Down

0 comments on commit 49358d4

Please sign in to comment.