From 63ed122ae4f70dd4f424ad94c89e3fec12d767be Mon Sep 17 00:00:00 2001 From: Mark Byrne Date: Wed, 12 Jun 2024 15:58:35 +0200 Subject: [PATCH 01/11] Fix a bug where a ``tox.ini`` file with pylint configuration was ignored and it exists in the current directory. Closes #9727 --- doc/whatsnew/fragments/9727.bugfix | 3 +++ pylint/config/find_default_config_files.py | 5 +++-- tests/config/test_find_default_config_files.py | 9 +++++++-- 3 files changed, 13 insertions(+), 4 deletions(-) create mode 100644 doc/whatsnew/fragments/9727.bugfix diff --git a/doc/whatsnew/fragments/9727.bugfix b/doc/whatsnew/fragments/9727.bugfix new file mode 100644 index 0000000000..b77e1a0624 --- /dev/null +++ b/doc/whatsnew/fragments/9727.bugfix @@ -0,0 +1,3 @@ +Fix a bug where a ``tox.ini`` file with pylint configuration was ignored and it exists in the current directory. + +Closes #9727 diff --git a/pylint/config/find_default_config_files.py b/pylint/config/find_default_config_files.py index 346393cf9a..e2fe2099fd 100644 --- a/pylint/config/find_default_config_files.py +++ b/pylint/config/find_default_config_files.py @@ -22,7 +22,8 @@ Path(".pylintrc.toml"), ) PYPROJECT_NAME = Path("pyproject.toml") -CONFIG_NAMES = (*RC_NAMES, PYPROJECT_NAME, Path("setup.cfg")) +TOX_NAME = Path("tox.ini") +CONFIG_NAMES = (*RC_NAMES, PYPROJECT_NAME, Path("setup.cfg"), TOX_NAME) def _find_pyproject() -> Path: @@ -71,7 +72,7 @@ def _yield_default_files() -> Iterator[Path]: if config_name.is_file(): if config_name.suffix == ".toml" and not _toml_has_config(config_name): continue - if config_name.suffix == ".cfg" and not _cfg_has_config(config_name): + if config_name.suffix in (".cfg", ".ini") and not _cfg_has_config(config_name): continue yield config_name.resolve() diff --git a/tests/config/test_find_default_config_files.py b/tests/config/test_find_default_config_files.py index ae879a10d0..c063c262b0 100644 --- a/tests/config/test_find_default_config_files.py +++ b/tests/config/test_find_default_config_files.py @@ -307,13 +307,18 @@ def test_toml_has_config(content: str, expected: bool, tmp_path: Path) -> None: ], ], ) -def test_cfg_has_config(content: str, expected: bool, tmp_path: Path) -> None: - """Test that a cfg file has a pylint config.""" +def test_has_config(content: str, expected: bool, tmp_path: Path) -> None: + """Test that a .cfg file or .ini file has a pylint config.""" fake_cfg = tmp_path / "fake.cfg" with open(fake_cfg, "w", encoding="utf8") as f: f.write(content) assert _cfg_has_config(fake_cfg) == expected + fake_ini = tmp_path / "tox.ini" + with open(fake_ini, "w", encoding="utf8") as f: + f.write(content) + assert _cfg_has_config(fake_ini) == expected + def test_non_existent_home() -> None: """Test that we handle a non-existent home directory. From b8b4444c42df3ee1f734ec165ca6732b010cc7de Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 12 Jun 2024 14:04:51 +0000 Subject: [PATCH 02/11] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- pylint/config/find_default_config_files.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pylint/config/find_default_config_files.py b/pylint/config/find_default_config_files.py index e2fe2099fd..ab812891c7 100644 --- a/pylint/config/find_default_config_files.py +++ b/pylint/config/find_default_config_files.py @@ -72,7 +72,9 @@ def _yield_default_files() -> Iterator[Path]: if config_name.is_file(): if config_name.suffix == ".toml" and not _toml_has_config(config_name): continue - if config_name.suffix in (".cfg", ".ini") and not _cfg_has_config(config_name): + if config_name.suffix in (".cfg", ".ini") and not _cfg_has_config( + config_name + ): continue yield config_name.resolve() From d5f4bbf38948b3b2e60a241d751cac2fcd675df0 Mon Sep 17 00:00:00 2001 From: Mark Byrne Date: Wed, 12 Jun 2024 16:13:46 +0200 Subject: [PATCH 03/11] Pylint: Use set for membership test. --- pylint/config/find_default_config_files.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pylint/config/find_default_config_files.py b/pylint/config/find_default_config_files.py index ab812891c7..0d8408bcef 100644 --- a/pylint/config/find_default_config_files.py +++ b/pylint/config/find_default_config_files.py @@ -72,7 +72,7 @@ def _yield_default_files() -> Iterator[Path]: if config_name.is_file(): if config_name.suffix == ".toml" and not _toml_has_config(config_name): continue - if config_name.suffix in (".cfg", ".ini") and not _cfg_has_config( + if config_name.suffix in {".cfg", ".ini"} and not _cfg_has_config( config_name ): continue From 307a4c3261e9026a54370ddcbf82017daa3a3bfb Mon Sep 17 00:00:00 2001 From: Mark Byrne Date: Wed, 12 Jun 2024 17:56:30 +0200 Subject: [PATCH 04/11] Refactor - no need for constant. --- pylint/config/find_default_config_files.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pylint/config/find_default_config_files.py b/pylint/config/find_default_config_files.py index 0d8408bcef..8733794a60 100644 --- a/pylint/config/find_default_config_files.py +++ b/pylint/config/find_default_config_files.py @@ -22,8 +22,7 @@ Path(".pylintrc.toml"), ) PYPROJECT_NAME = Path("pyproject.toml") -TOX_NAME = Path("tox.ini") -CONFIG_NAMES = (*RC_NAMES, PYPROJECT_NAME, Path("setup.cfg"), TOX_NAME) +CONFIG_NAMES = (*RC_NAMES, PYPROJECT_NAME, Path("setup.cfg"), Path("tox.ini")) def _find_pyproject() -> Path: From 30850c7f79c0df23d4a2efa4ab9c34b89a51a821 Mon Sep 17 00:00:00 2001 From: Mark Byrne <31762852+mbyrnepr2@users.noreply.github.com> Date: Wed, 12 Jun 2024 21:36:37 +0200 Subject: [PATCH 05/11] Update tests/config/test_find_default_config_files.py Co-authored-by: Pierre Sassoulas --- tests/config/test_find_default_config_files.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/config/test_find_default_config_files.py b/tests/config/test_find_default_config_files.py index c063c262b0..5bcfdd6ec4 100644 --- a/tests/config/test_find_default_config_files.py +++ b/tests/config/test_find_default_config_files.py @@ -309,10 +309,11 @@ def test_toml_has_config(content: str, expected: bool, tmp_path: Path) -> None: ) def test_has_config(content: str, expected: bool, tmp_path: Path) -> None: """Test that a .cfg file or .ini file has a pylint config.""" - fake_cfg = tmp_path / "fake.cfg" - with open(fake_cfg, "w", encoding="utf8") as f: - f.write(content) - assert _cfg_has_config(fake_cfg) == expected + for file_name in ("fake.cfg", "tox.ini"): + fake_conf = tmp_path / file_name + with open(fake_conf, "w", encoding="utf8") as f: + f.write(content) + assert _cfg_has_config(fake_conf) == expected fake_ini = tmp_path / "tox.ini" with open(fake_ini, "w", encoding="utf8") as f: From 64e2c6627aaf8baf3a3378b1a4ab0e30466848fd Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 12 Jun 2024 19:37:25 +0000 Subject: [PATCH 06/11] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/config/test_find_default_config_files.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/config/test_find_default_config_files.py b/tests/config/test_find_default_config_files.py index 5bcfdd6ec4..47a4717326 100644 --- a/tests/config/test_find_default_config_files.py +++ b/tests/config/test_find_default_config_files.py @@ -310,7 +310,7 @@ def test_toml_has_config(content: str, expected: bool, tmp_path: Path) -> None: def test_has_config(content: str, expected: bool, tmp_path: Path) -> None: """Test that a .cfg file or .ini file has a pylint config.""" for file_name in ("fake.cfg", "tox.ini"): - fake_conf = tmp_path / file_name + fake_conf = tmp_path / file_name with open(fake_conf, "w", encoding="utf8") as f: f.write(content) assert _cfg_has_config(fake_conf) == expected From 5084f0fe44089809b47a123f62b6959a08b01bb2 Mon Sep 17 00:00:00 2001 From: Mark Byrne Date: Wed, 12 Jun 2024 21:37:56 +0200 Subject: [PATCH 07/11] Refactor the test using Pierre's suggestion. Co-authored-by: Pierre Sassoulas --- tests/config/test_find_default_config_files.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/tests/config/test_find_default_config_files.py b/tests/config/test_find_default_config_files.py index 47a4717326..be1db4aadf 100644 --- a/tests/config/test_find_default_config_files.py +++ b/tests/config/test_find_default_config_files.py @@ -315,11 +315,6 @@ def test_has_config(content: str, expected: bool, tmp_path: Path) -> None: f.write(content) assert _cfg_has_config(fake_conf) == expected - fake_ini = tmp_path / "tox.ini" - with open(fake_ini, "w", encoding="utf8") as f: - f.write(content) - assert _cfg_has_config(fake_ini) == expected - def test_non_existent_home() -> None: """Test that we handle a non-existent home directory. From 6aeba714877b6dd11d1c8118ab3d4fbb3d259906 Mon Sep 17 00:00:00 2001 From: Mark Byrne Date: Wed, 12 Jun 2024 23:21:13 +0200 Subject: [PATCH 08/11] Identify Pylint configuration in `.ini` and `.cfg` files where the sections are named `[pylint]` or `[pylint.X]`, where `X` is an arbitrary value. --- pylint/config/find_default_config_files.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pylint/config/find_default_config_files.py b/pylint/config/find_default_config_files.py index 8733794a60..572cf2cd6a 100644 --- a/pylint/config/find_default_config_files.py +++ b/pylint/config/find_default_config_files.py @@ -61,7 +61,10 @@ def _cfg_has_config(path: Path | str) -> bool: parser.read(path, encoding="utf-8") except configparser.Error: return False - return any(section.startswith("pylint.") for section in parser.sections()) + return any( + section == "pylint" or section.startswith("pylint.") + for section in parser.sections() + ) def _yield_default_files() -> Iterator[Path]: From 99195b4b1218c5e95d292449de699e13fa5772ce Mon Sep 17 00:00:00 2001 From: Mark Byrne Date: Mon, 17 Jun 2024 16:03:44 +0200 Subject: [PATCH 09/11] Update news fragment to include the enhancement to the `Pylint` configuration section naming for `.cfg` and `.ini` files. --- doc/whatsnew/fragments/9727.bugfix | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doc/whatsnew/fragments/9727.bugfix b/doc/whatsnew/fragments/9727.bugfix index b77e1a0624..043541f75b 100644 --- a/doc/whatsnew/fragments/9727.bugfix +++ b/doc/whatsnew/fragments/9727.bugfix @@ -1,3 +1,5 @@ Fix a bug where a ``tox.ini`` file with pylint configuration was ignored and it exists in the current directory. +``.cfg`` and ``.ini`` files containing a ``Pylint`` configuration may now use a section named ``[pylint]``. This enhancement impacts the scenario where these file types are used as defaults when they are present and have not been explicitly referred to, using the ``--rcfile`` option. + Closes #9727 From 9e597be889f5cb578238a25b84256f30be8f6f1d Mon Sep 17 00:00:00 2001 From: Mark Byrne Date: Mon, 17 Jun 2024 16:05:15 +0200 Subject: [PATCH 10/11] Rename the function to be more explicit. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Daniƫl van Noord <13665637+DanielNoord@users.noreply.github.com> --- pylint/config/find_default_config_files.py | 4 ++-- tests/config/test_find_default_config_files.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pylint/config/find_default_config_files.py b/pylint/config/find_default_config_files.py index 572cf2cd6a..434c15cddf 100644 --- a/pylint/config/find_default_config_files.py +++ b/pylint/config/find_default_config_files.py @@ -55,7 +55,7 @@ def _toml_has_config(path: Path | str) -> bool: return "pylint" in content.get("tool", []) -def _cfg_has_config(path: Path | str) -> bool: +def _cfg_or_ini_has_config(path: Path | str) -> bool: parser = configparser.ConfigParser() try: parser.read(path, encoding="utf-8") @@ -74,7 +74,7 @@ def _yield_default_files() -> Iterator[Path]: if config_name.is_file(): if config_name.suffix == ".toml" and not _toml_has_config(config_name): continue - if config_name.suffix in {".cfg", ".ini"} and not _cfg_has_config( + if config_name.suffix in {".cfg", ".ini"} and not _cfg_or_ini_has_config( config_name ): continue diff --git a/tests/config/test_find_default_config_files.py b/tests/config/test_find_default_config_files.py index be1db4aadf..6ac43b5fd5 100644 --- a/tests/config/test_find_default_config_files.py +++ b/tests/config/test_find_default_config_files.py @@ -18,7 +18,7 @@ from pytest import CaptureFixture from pylint import config, testutils -from pylint.config.find_default_config_files import _cfg_has_config, _toml_has_config +from pylint.config.find_default_config_files import _cfg_or_ini_has_config, _toml_has_config from pylint.lint.run import Run @@ -313,7 +313,7 @@ def test_has_config(content: str, expected: bool, tmp_path: Path) -> None: fake_conf = tmp_path / file_name with open(fake_conf, "w", encoding="utf8") as f: f.write(content) - assert _cfg_has_config(fake_conf) == expected + assert _cfg_or_ini_has_config(fake_conf) == expected def test_non_existent_home() -> None: From e0a456d5e35ac3c0c67bf429c4448886d1307873 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 17 Jun 2024 14:11:33 +0000 Subject: [PATCH 11/11] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- pylint/config/find_default_config_files.py | 7 ++++--- tests/config/test_find_default_config_files.py | 5 ++++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/pylint/config/find_default_config_files.py b/pylint/config/find_default_config_files.py index 434c15cddf..7e53e77207 100644 --- a/pylint/config/find_default_config_files.py +++ b/pylint/config/find_default_config_files.py @@ -74,9 +74,10 @@ def _yield_default_files() -> Iterator[Path]: if config_name.is_file(): if config_name.suffix == ".toml" and not _toml_has_config(config_name): continue - if config_name.suffix in {".cfg", ".ini"} and not _cfg_or_ini_has_config( - config_name - ): + if config_name.suffix in { + ".cfg", + ".ini", + } and not _cfg_or_ini_has_config(config_name): continue yield config_name.resolve() diff --git a/tests/config/test_find_default_config_files.py b/tests/config/test_find_default_config_files.py index 6ac43b5fd5..2c50a55682 100644 --- a/tests/config/test_find_default_config_files.py +++ b/tests/config/test_find_default_config_files.py @@ -18,7 +18,10 @@ from pytest import CaptureFixture from pylint import config, testutils -from pylint.config.find_default_config_files import _cfg_or_ini_has_config, _toml_has_config +from pylint.config.find_default_config_files import ( + _cfg_or_ini_has_config, + _toml_has_config, +) from pylint.lint.run import Run