From f8288842e58a8b6007ff15ab380c84b00eb9b7a3 Mon Sep 17 00:00:00 2001 From: "Mikhail f. Shiryaev" Date: Sun, 30 Apr 2023 22:48:34 +0200 Subject: Search for pyproject.toml config file in parent dirs (#7163) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Pierre Sassoulas Co-authored-by: Daniƫl van Noord <13665637+DanielNoord@users.noreply.github.com> --- pylint/config/find_default_config_files.py | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) (limited to 'pylint/config/find_default_config_files.py') diff --git a/pylint/config/find_default_config_files.py b/pylint/config/find_default_config_files.py index a121b32a3..3b03f6357 100644 --- a/pylint/config/find_default_config_files.py +++ b/pylint/config/find_default_config_files.py @@ -16,7 +16,28 @@ else: 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") +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: @@ -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: -- cgit v1.2.1