Add `tox.ini` to the default discovered config files (right after setup.cfg) (#9728)
Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
Co-authored-by: Daniƫl van Noord <13665637+DanielNoord@users.noreply.github.com>
diff --git a/doc/whatsnew/fragments/9727.bugfix b/doc/whatsnew/fragments/9727.bugfix
new file mode 100644
index 0000000..043541f
--- /dev/null
+++ b/doc/whatsnew/fragments/9727.bugfix
@@ -0,0 +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
diff --git a/pylint/config/find_default_config_files.py b/pylint/config/find_default_config_files.py
index 346393c..7e53e77 100644
--- a/pylint/config/find_default_config_files.py
+++ b/pylint/config/find_default_config_files.py
@@ -22,7 +22,7 @@
Path(".pylintrc.toml"),
)
PYPROJECT_NAME = Path("pyproject.toml")
-CONFIG_NAMES = (*RC_NAMES, PYPROJECT_NAME, Path("setup.cfg"))
+CONFIG_NAMES = (*RC_NAMES, PYPROJECT_NAME, Path("setup.cfg"), Path("tox.ini"))
def _find_pyproject() -> Path:
@@ -55,13 +55,16 @@
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")
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]:
@@ -71,7 +74,10 @@
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_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 ae879a1..2c50a55 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_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
@@ -307,12 +310,13 @@
],
],
)
-def test_cfg_has_config(content: str, expected: bool, tmp_path: Path) -> None:
- """Test that a cfg 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
+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
+ with open(fake_conf, "w", encoding="utf8") as f:
+ f.write(content)
+ assert _cfg_or_ini_has_config(fake_conf) == expected
def test_non_existent_home() -> None: