From 49358d4e91e4cee6d38d08baab574a6ce971ca09 Mon Sep 17 00:00:00 2001 From: Matej Spiller Muys Date: Mon, 13 Nov 2023 11:38:53 +0100 Subject: [PATCH] Support for resolving external toml files --- doc/user_guide/usage/run.rst | 4 +++ doc/whatsnew/fragments/9228.feature | 3 ++ pylint/config/find_default_config_files.py | 7 +++- .../config/test_find_default_config_files.py | 35 +++++++++++++++++++ 4 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 doc/whatsnew/fragments/9228.feature diff --git a/doc/user_guide/usage/run.rst b/doc/user_guide/usage/run.rst index 081517a39b..3136181254 100644 --- a/doc/user_guide/usage/run.rst +++ b/doc/user_guide/usage/run.rst @@ -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.``, diff --git a/doc/whatsnew/fragments/9228.feature b/doc/whatsnew/fragments/9228.feature new file mode 100644 index 0000000000..e7b984014c --- /dev/null +++ b/doc/whatsnew/fragments/9228.feature @@ -0,0 +1,3 @@ +Support for resolving external toml files named pylintrc.toml and .pylintrc.toml. + +Closes #9228 diff --git a/pylint/config/find_default_config_files.py b/pylint/config/find_default_config_files.py index 3b03f6357b..346393cf9a 100644 --- a/pylint/config/find_default_config_files.py +++ b/pylint/config/find_default_config_files.py @@ -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")) diff --git a/tests/config/test_find_default_config_files.py b/tests/config/test_find_default_config_files.py index 0b513a3d5f..6a8095c5d7 100644 --- a/tests/config/test_find_default_config_files.py +++ b/tests/config/test_find_default_config_files.py @@ -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"""