Skip to content

Commit

Permalink
Add a function to search for pyproject.toml in a project root
Browse files Browse the repository at this point in the history
  • Loading branch information
Felixoid committed Feb 29, 2024
1 parent f19b5d3 commit 18b2905
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion mypy/config_parser.py
Expand Up @@ -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],
Expand All @@ -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()
Expand Down

0 comments on commit 18b2905

Please sign in to comment.