From 18b290573b44b041f275a2f8fff1bcf1845cb0cc Mon Sep 17 00:00:00 2001 From: "Mikhail f. Shiryaev" Date: Thu, 29 Feb 2024 15:01:29 +0100 Subject: [PATCH] Add a function to search for pyproject.toml in a project root --- mypy/config_parser.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/mypy/config_parser.py b/mypy/config_parser.py index a6bf021000c1a..069f7f1ca69ac 100644 --- a/mypy/config_parser.py +++ b/mypy/config_parser.py @@ -229,6 +229,27 @@ def split_commas(value: str) -> list[str]: ) +def _find_pyproject() -> list[str]: + """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 = os.path.abspath(os.path.curdir) + is_root = False + while not is_root: + for pyproject_name in defaults.PYPROJECT_CONFIG_FILES: + config_file = os.path.join(current_dir, pyproject_name) + if os.path.isfile(config_file): + return [os.path.abspath(config_file)] + parent = os.path.abspath(os.path.join(current_dir, os.path.pardir)) + is_root = current_dir == parent or any( + os.path.isdir(os.path.join(current_dir, cvs_root)) for cvs_root in (".git", ".hg") + ) + current_dir = parent + + return [] + + def parse_config_file( options: Options, set_strict_flags: Callable[[], None], @@ -248,7 +269,9 @@ def parse_config_file( if filename is not None: config_files: tuple[str, ...] = (filename,) else: - config_files_iter: Iterable[str] = map(os.path.expanduser, defaults.CONFIG_FILES) + config_files_iter: Iterable[str] = map( + os.path.expanduser, defaults.CONFIG_FILES + _find_pyproject() + ) config_files = tuple(config_files_iter) config_parser = configparser.RawConfigParser()