Skip to content

Commit 795c8bd

Browse files
committed
Add a function to search for pyproject.toml in a project root
1 parent c9ed867 commit 795c8bd

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

mypy/config_parser.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,28 @@ def split_commas(value: str) -> list[str]:
217217
)
218218

219219

220+
def _find_pyproject() -> list[str]:
221+
"""Search for file pyproject.toml in the parent directories recursively.
222+
223+
It resolves symlinks, so if there is any symlink up in the tree, it does not respect them
224+
"""
225+
# We start from the parent dir, since 'pyproject.toml' is already parsed
226+
current_dir = os.path.abspath(os.path.join(os.path.curdir, os.path.pardir))
227+
is_root = False
228+
while not is_root:
229+
for pyproject_name in defaults.PYPROJECT_CONFIG_FILES:
230+
config_file = os.path.join(current_dir, pyproject_name)
231+
if os.path.isfile(config_file):
232+
return [os.path.abspath(config_file)]
233+
parent = os.path.abspath(os.path.join(current_dir, os.path.pardir))
234+
is_root = current_dir == parent or any(
235+
os.path.isdir(os.path.join(current_dir, cvs_root)) for cvs_root in (".git", ".hg")
236+
)
237+
current_dir = parent
238+
239+
return []
240+
241+
220242
def parse_config_file(
221243
options: Options,
222244
set_strict_flags: Callable[[], None],
@@ -236,7 +258,9 @@ def parse_config_file(
236258
if filename is not None:
237259
config_files: tuple[str, ...] = (filename,)
238260
else:
239-
config_files_iter: Iterable[str] = map(os.path.expanduser, defaults.CONFIG_FILES)
261+
config_files_iter: Iterable[str] = map(
262+
os.path.expanduser, defaults.CONFIG_FILES + _find_pyproject()
263+
)
240264
config_files = tuple(config_files_iter)
241265

242266
config_parser = configparser.RawConfigParser()

0 commit comments

Comments
 (0)