Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tox.ini to the default discovered config files (right after setup.cfg) #9728

Merged
merged 11 commits into from
Jun 18, 2024
Merged
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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably include the change about accepted sections here as well.


``.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
Loading