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

Search for pyproject.toml config file in parent dirs #7163

Merged
merged 4 commits into from
Apr 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .pyenchant_pylint_custom_dict.txt
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ pylint
pylintdict
pylintrc
pylint's
pyproject
pypy
pyreverse
pytest
Expand Down Expand Up @@ -318,6 +319,8 @@ subtree
supcls
superclass
symilar
symlink
symlinks
sys
tbump
tempfile
Expand Down
3 changes: 3 additions & 0 deletions doc/user_guide/usage/run.rst
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ configuration file in the following order and uses the first one it finds:
providing it has at least one ``pylint.`` section
#. ``tox.ini`` in the current working directory,
providing it has at least one ``pylint.`` section
#. Pylint will search for the ``pyproject.toml`` file up the directories hierarchy
unless it's found, or a ``.git``/``.hg`` directory is found, or the file system root
is approached.
#. If the current working directory is in a Python package, Pylint searches \
up the hierarchy of Python packages until it finds a ``pylintrc`` file. \
This allows you to specify coding standards on a module-by-module \
Expand Down
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/7163.other
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Search for ``pyproject.toml`` recursively in parent directories up to a project or file system root.

Refs #7163, Closes #3289
30 changes: 29 additions & 1 deletion pylint/config/find_default_config_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,28 @@
import tomli as tomllib

RC_NAMES = (Path("pylintrc"), Path(".pylintrc"))
CONFIG_NAMES = (*RC_NAMES, Path("pyproject.toml"), Path("setup.cfg"))
PYPROJECT_NAME = Path("pyproject.toml")
Pierre-Sassoulas marked this conversation as resolved.
Show resolved Hide resolved
CONFIG_NAMES = (*RC_NAMES, PYPROJECT_NAME, Path("setup.cfg"))


def _find_pyproject() -> Path:
"""Search for file pyproject.toml in the parent directories recursively.

It resolves symlinks, so if there is any symlink up in the tree, it does not respect them
"""
current_dir = Path.cwd().resolve()
is_root = False
while not is_root:
if (current_dir / PYPROJECT_NAME).is_file():
return current_dir / PYPROJECT_NAME
is_root = (
current_dir == current_dir.parent
or (current_dir / ".git").is_dir()
or (current_dir / ".hg").is_dir()
)
current_dir = current_dir.parent

return current_dir


def _toml_has_config(path: Path | str) -> bool:
Expand Down Expand Up @@ -99,6 +120,13 @@ def find_default_config_files() -> Iterator[Path]:
except OSError:
pass

try:
parent_pyproject = _find_pyproject()
if parent_pyproject.is_file() and _toml_has_config(parent_pyproject):
yield parent_pyproject.resolve()
except OSError:
pass

try:
yield from _find_config_in_home_or_environment()
except OSError:
Expand Down
41 changes: 41 additions & 0 deletions tests/config/test_find_default_config_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,47 @@ def test_pylintrc_parentdir() -> None:
assert next(config.find_default_config_files()) == expected


@pytest.mark.usefixtures("pop_pylintrc")
def test_pyproject_toml_parentdir() -> None:
"""Test the search of pyproject.toml file in parent directories"""
with tempdir() as chroot:
with fake_home():
chroot_path = Path(chroot)
files = [
"pyproject.toml",
"git/pyproject.toml",
"git/a/pyproject.toml",
"git/a/.git",
"git/a/b/c/__init__.py",
"hg/pyproject.toml",
"hg/a/pyproject.toml",
"hg/a/.hg",
"hg/a/b/c/__init__.py",
"none/sub/__init__.py",
]
Pierre-Sassoulas marked this conversation as resolved.
Show resolved Hide resolved
testutils.create_files(files)
for config_file in files:
if config_file.endswith("pyproject.toml"):
with open(config_file, "w", encoding="utf-8") as fd:
fd.write('[tool.pylint."messages control"]\n')
results = {
"": chroot_path / "pyproject.toml",
"git": chroot_path / "git" / "pyproject.toml",
"git/a": chroot_path / "git" / "a" / "pyproject.toml",
"git/a/b": chroot_path / "git" / "a" / "pyproject.toml",
"git/a/b/c": chroot_path / "git" / "a" / "pyproject.toml",
"hg": chroot_path / "hg" / "pyproject.toml",
"hg/a": chroot_path / "hg" / "a" / "pyproject.toml",
"hg/a/b": chroot_path / "hg" / "a" / "pyproject.toml",
"hg/a/b/c": chroot_path / "hg" / "a" / "pyproject.toml",
"none": chroot_path / "pyproject.toml",
"none/sub": chroot_path / "pyproject.toml",
}
for basedir, expected in results.items():
os.chdir(chroot_path / basedir)
assert next(config.find_default_config_files(), None) == expected


@pytest.mark.usefixtures("pop_pylintrc")
def test_pylintrc_parentdir_no_package() -> None:
"""Test that we don't find a pylintrc in sub-packages."""
Expand Down