Skip to content

Commit 65324b8

Browse files
committed
Add a function to search for pyproject.toml in a project root
1 parent f19b5d3 commit 65324b8

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

mypy/config_parser.py

+25-1
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,28 @@ def split_commas(value: str) -> list[str]:
229229
)
230230

231231

232+
def _find_pyproject() -> list[str]:
233+
"""Search for file pyproject.toml in the parent directories recursively.
234+
235+
It resolves symlinks, so if there is any symlink up in the tree, it does not respect them
236+
"""
237+
# We start from the parent dir, since 'pyproject.toml' is already parsed
238+
current_dir = os.path.abspath(os.path.join(os.path.curdir, os.path.pardir))
239+
is_root = False
240+
while not is_root:
241+
for pyproject_name in defaults.PYPROJECT_CONFIG_FILES:
242+
config_file = os.path.join(current_dir, pyproject_name)
243+
if os.path.isfile(config_file):
244+
return [os.path.abspath(config_file)]
245+
parent = os.path.abspath(os.path.join(current_dir, os.path.pardir))
246+
is_root = current_dir == parent or any(
247+
os.path.isdir(os.path.join(current_dir, cvs_root)) for cvs_root in (".git", ".hg")
248+
)
249+
current_dir = parent
250+
251+
return []
252+
253+
232254
def parse_config_file(
233255
options: Options,
234256
set_strict_flags: Callable[[], None],
@@ -248,7 +270,9 @@ def parse_config_file(
248270
if filename is not None:
249271
config_files: tuple[str, ...] = (filename,)
250272
else:
251-
config_files_iter: Iterable[str] = map(os.path.expanduser, defaults.CONFIG_FILES)
273+
config_files_iter: Iterable[str] = map(
274+
os.path.expanduser, defaults.CONFIG_FILES + _find_pyproject()
275+
)
252276
config_files = tuple(config_files_iter)
253277

254278
config_parser = configparser.RawConfigParser()

0 commit comments

Comments
 (0)