Skip to content

Commit fc9bdeb

Browse files
mbyrnepr2Pierre-SassoulasDanielNoord
authored
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>
1 parent 255ed0b commit fc9bdeb

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

doc/whatsnew/fragments/9727.bugfix

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Fix a bug where a ``tox.ini`` file with pylint configuration was ignored and it exists in the current directory.
2+
3+
``.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.
4+
5+
Closes #9727

pylint/config/find_default_config_files.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
Path(".pylintrc.toml"),
2323
)
2424
PYPROJECT_NAME = Path("pyproject.toml")
25-
CONFIG_NAMES = (*RC_NAMES, PYPROJECT_NAME, Path("setup.cfg"))
25+
CONFIG_NAMES = (*RC_NAMES, PYPROJECT_NAME, Path("setup.cfg"), Path("tox.ini"))
2626

2727

2828
def _find_pyproject() -> Path:
@@ -55,13 +55,16 @@ def _toml_has_config(path: Path | str) -> bool:
5555
return "pylint" in content.get("tool", [])
5656

5757

58-
def _cfg_has_config(path: Path | str) -> bool:
58+
def _cfg_or_ini_has_config(path: Path | str) -> bool:
5959
parser = configparser.ConfigParser()
6060
try:
6161
parser.read(path, encoding="utf-8")
6262
except configparser.Error:
6363
return False
64-
return any(section.startswith("pylint.") for section in parser.sections())
64+
return any(
65+
section == "pylint" or section.startswith("pylint.")
66+
for section in parser.sections()
67+
)
6568

6669

6770
def _yield_default_files() -> Iterator[Path]:
@@ -71,7 +74,10 @@ def _yield_default_files() -> Iterator[Path]:
7174
if config_name.is_file():
7275
if config_name.suffix == ".toml" and not _toml_has_config(config_name):
7376
continue
74-
if config_name.suffix == ".cfg" and not _cfg_has_config(config_name):
77+
if config_name.suffix in {
78+
".cfg",
79+
".ini",
80+
} and not _cfg_or_ini_has_config(config_name):
7581
continue
7682

7783
yield config_name.resolve()

tests/config/test_find_default_config_files.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818
from pytest import CaptureFixture
1919

2020
from pylint import config, testutils
21-
from pylint.config.find_default_config_files import _cfg_has_config, _toml_has_config
21+
from pylint.config.find_default_config_files import (
22+
_cfg_or_ini_has_config,
23+
_toml_has_config,
24+
)
2225
from pylint.lint.run import Run
2326

2427

@@ -307,12 +310,13 @@ def test_toml_has_config(content: str, expected: bool, tmp_path: Path) -> None:
307310
],
308311
],
309312
)
310-
def test_cfg_has_config(content: str, expected: bool, tmp_path: Path) -> None:
311-
"""Test that a cfg file has a pylint config."""
312-
fake_cfg = tmp_path / "fake.cfg"
313-
with open(fake_cfg, "w", encoding="utf8") as f:
314-
f.write(content)
315-
assert _cfg_has_config(fake_cfg) == expected
313+
def test_has_config(content: str, expected: bool, tmp_path: Path) -> None:
314+
"""Test that a .cfg file or .ini file has a pylint config."""
315+
for file_name in ("fake.cfg", "tox.ini"):
316+
fake_conf = tmp_path / file_name
317+
with open(fake_conf, "w", encoding="utf8") as f:
318+
f.write(content)
319+
assert _cfg_or_ini_has_config(fake_conf) == expected
316320

317321

318322
def test_non_existent_home() -> None:

0 commit comments

Comments
 (0)