Skip to content

Commit

Permalink
Add tox.ini to the default discovered config files (right after set…
Browse files Browse the repository at this point in the history
…up.cfg) (#9728)

Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
Co-authored-by: Daniël van Noord <13665637+DanielNoord@users.noreply.github.com>
  • Loading branch information
3 people committed Jun 18, 2024
1 parent 255ed0b commit fc9bdeb
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
5 changes: 5 additions & 0 deletions doc/whatsnew/fragments/9727.bugfix
Original file line number Diff line number Diff line change
@@ -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
14 changes: 10 additions & 4 deletions pylint/config/find_default_config_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -55,13 +55,16 @@ 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")
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]:
Expand All @@ -71,7 +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 == ".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()
Expand Down
18 changes: 11 additions & 7 deletions tests/config/test_find_default_config_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -307,12 +310,13 @@ 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."""
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:
Expand Down

0 comments on commit fc9bdeb

Please sign in to comment.