Skip to content

Commit 55a2145

Browse files
committed
Find pyproject.toml only once, and use it in CONFIG_FILES
1 parent 795c8bd commit 55a2145

File tree

2 files changed

+34
-26
lines changed

2 files changed

+34
-26
lines changed

mypy/config_parser.py

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -217,28 +217,6 @@ 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-
242220
def parse_config_file(
243221
options: Options,
244222
set_strict_flags: Callable[[], None],
@@ -258,9 +236,7 @@ def parse_config_file(
258236
if filename is not None:
259237
config_files: tuple[str, ...] = (filename,)
260238
else:
261-
config_files_iter: Iterable[str] = map(
262-
os.path.expanduser, defaults.CONFIG_FILES + _find_pyproject()
263-
)
239+
config_files_iter: Iterable[str] = map(os.path.expanduser, defaults.CONFIG_FILES)
264240
config_files = tuple(config_files_iter)
265241

266242
config_parser = configparser.RawConfigParser()

mypy/defaults.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,41 @@
1212
# mypy, at least version PYTHON3_VERSION is needed.
1313
PYTHON3_VERSION_MIN: Final = (3, 8) # Keep in sync with typeshed's python support
1414

15+
16+
def find_pyproject() -> str:
17+
"""Search for file pyproject.toml in the parent directories recursively.
18+
19+
It resolves symlinks, so if there is any symlink up in the tree, it does not respect them
20+
21+
If the file is not found until the root of FS or repository, PYPROJECT_FILE is used
22+
"""
23+
24+
def is_root(current_dir: str) -> bool:
25+
parent = os.path.join(current_dir, os.path.pardir)
26+
return os.path.samefile(current_dir, parent) or any(
27+
os.path.isdir(os.path.join(current_dir, cvs_root)) for cvs_root in (".git", ".hg")
28+
)
29+
30+
# Preserve the original behavior, returning PYPROJECT_FILE if exists
31+
if os.path.isfile(PYPROJECT_FILE) or is_root(os.path.curdir):
32+
return PYPROJECT_FILE
33+
34+
# And iterate over the tree
35+
current_dir = os.path.pardir
36+
while not is_root(current_dir):
37+
config_file = os.path.join(current_dir, PYPROJECT_FILE)
38+
if os.path.isfile(config_file):
39+
return config_file
40+
parent = os.path.join(current_dir, os.path.pardir)
41+
current_dir = parent
42+
43+
return PYPROJECT_FILE
44+
45+
1546
CACHE_DIR: Final = ".mypy_cache"
1647
CONFIG_FILE: Final = ["mypy.ini", ".mypy.ini"]
17-
PYPROJECT_CONFIG_FILES: Final = ["pyproject.toml"]
48+
PYPROJECT_FILE: Final = "pyproject.toml"
49+
PYPROJECT_CONFIG_FILES: Final = [find_pyproject()]
1850
SHARED_CONFIG_FILES: Final = ["setup.cfg"]
1951
USER_CONFIG_FILES: Final = ["~/.config/mypy/config", "~/.mypy.ini"]
2052
if os.environ.get("XDG_CONFIG_HOME"):

0 commit comments

Comments
 (0)