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 e1fddad
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion mypy/config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
Tuple,
Union,
)

from typing_extensions import TypeAlias as _TypeAlias

from mypy import defaults
Expand Down Expand Up @@ -229,6 +230,28 @@ 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
"""
# We start from the parent dir, since 'pyproject.toml' is already parsed
current_dir = os.path.abspath(os.path.join(os.path.curdir, os.path.pardir))
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 +271,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 e1fddad

Please sign in to comment.