Skip to content

Commit d04de61

Browse files
committed
Find pyproject.toml only once, and use it in CONFIG_FILES
1 parent 65324b8 commit d04de61

File tree

3 files changed

+30
-30
lines changed

3 files changed

+30
-30
lines changed

mypy/config_parser.py

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -229,28 +229,6 @@ 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-
254232
def parse_config_file(
255233
options: Options,
256234
set_strict_flags: Callable[[], None],
@@ -270,9 +248,7 @@ def parse_config_file(
270248
if filename is not None:
271249
config_files: tuple[str, ...] = (filename,)
272250
else:
273-
config_files_iter: Iterable[str] = map(
274-
os.path.expanduser, defaults.CONFIG_FILES + _find_pyproject()
275-
)
251+
config_files_iter: Iterable[str] = map(os.path.expanduser, defaults.CONFIG_FILES)
276252
config_files = tuple(config_files_iter)
277253

278254
config_parser = configparser.RawConfigParser()

mypy/defaults.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,33 @@
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+
current_dir = os.path.curdir
24+
is_root = False
25+
while not is_root:
26+
config_file = os.path.join(current_dir, PYPROJECT_FILE)
27+
if os.path.isfile(config_file):
28+
return config_file
29+
parent = os.path.join(current_dir, os.path.pardir)
30+
is_root = os.path.samefile(current_dir, parent) or any(
31+
os.path.isdir(os.path.join(current_dir, cvs_root)) for cvs_root in (".git", ".hg")
32+
)
33+
current_dir = parent
34+
35+
return PYPROJECT_FILE
36+
37+
1538
CACHE_DIR: Final = ".mypy_cache"
1639
CONFIG_FILE: Final = ["mypy.ini", ".mypy.ini"]
17-
PYPROJECT_CONFIG_FILES: Final = ["pyproject.toml"]
40+
PYPROJECT_FILE: Final = "pyproject.toml"
41+
PYPROJECT_CONFIG_FILES: Final = [find_pyproject()]
1842
SHARED_CONFIG_FILES: Final = ["setup.cfg"]
1943
USER_CONFIG_FILES: Final = ["~/.config/mypy/config", "~/.mypy.ini"]
2044
if os.environ.get("XDG_CONFIG_HOME"):

test-data/unit/cmdline.pyproject.test

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def f(a):
2828
def g(a: int) -> int:
2929
return f(a)
3030
[out]
31-
pyproject.toml: tool.mypy.overrides sections must be an array. Please make sure you are using double brackets like so: [[tool.mypy.overrides]]
31+
./pyproject.toml: tool.mypy.overrides sections must be an array. Please make sure you are using double brackets like so: [[tool.mypy.overrides]]
3232
== Return code: 0
3333

3434
[case testNoModuleInOverridePyprojectTOML]
@@ -43,7 +43,7 @@ def f(a):
4343
def g(a: int) -> int:
4444
return f(a)
4545
[out]
46-
pyproject.toml: toml config file contains a [[tool.mypy.overrides]] section, but no module to override was specified.
46+
./pyproject.toml: toml config file contains a [[tool.mypy.overrides]] section, but no module to override was specified.
4747
== Return code: 0
4848

4949
[case testInvalidModuleInOverridePyprojectTOML]
@@ -59,7 +59,7 @@ def f(a):
5959
def g(a: int) -> int:
6060
return f(a)
6161
[out]
62-
pyproject.toml: toml config file contains a [[tool.mypy.overrides]] section with a module value that is not a string or a list of strings
62+
./pyproject.toml: toml config file contains a [[tool.mypy.overrides]] section with a module value that is not a string or a list of strings
6363
== Return code: 0
6464

6565
[case testConflictingModuleInOverridesPyprojectTOML]
@@ -78,7 +78,7 @@ def f(a):
7878
def g(a: int) -> int:
7979
return f(a)
8080
[out]
81-
pyproject.toml: toml config file contains [[tool.mypy.overrides]] sections with conflicting values. Module 'x' has two different values for 'disallow_untyped_defs'
81+
./pyproject.toml: toml config file contains [[tool.mypy.overrides]] sections with conflicting values. Module 'x' has two different values for 'disallow_untyped_defs'
8282
== Return code: 0
8383

8484
[case testMultilineLiteralExcludePyprojectTOML]

0 commit comments

Comments
 (0)