Skip to content

Commit c694ea6

Browse files
committed
.
1 parent f1d0d91 commit c694ea6

File tree

2 files changed

+114
-35
lines changed

2 files changed

+114
-35
lines changed

mypy/test/test_config_parser.py

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
from __future__ import annotations
2+
import contextlib
3+
import os
4+
import tempfile
5+
import unittest
6+
from collections.abc import Iterator
7+
from pathlib import Path
8+
9+
from mypy.defaults import CONFIG_NAMES, SHARED_CONFIG_NAMES
10+
from mypy.config_parser import _find_config_file
11+
12+
13+
@contextlib.contextmanager
14+
def chdir(target: Path) -> Iterator[None]:
15+
# Replace with contextlib.chdir in Python 3.11
16+
dir = os.getcwd()
17+
os.chdir(target)
18+
try:
19+
yield
20+
finally:
21+
os.chdir(dir)
22+
23+
24+
def write_config(path: Path, content: str | None = None) -> None:
25+
if path.suffix == ".toml":
26+
if content is None:
27+
content = "[tool.mypy]\nstrict = true"
28+
path.write_text(content)
29+
else:
30+
if content is None:
31+
content = "[mypy]\nstrict = True"
32+
path.write_text(content)
33+
34+
35+
class FindConfigFileSuite(unittest.TestCase):
36+
37+
def test_no_config(self) -> None:
38+
with tempfile.TemporaryDirectory() as _tmpdir:
39+
tmpdir = Path(_tmpdir)
40+
(tmpdir / ".git").touch()
41+
with contextlib.chdir(tmpdir):
42+
result = _find_config_file()
43+
assert result is None
44+
45+
def test_parent_config_with_and_without_git(self) -> None:
46+
for name in CONFIG_NAMES + SHARED_CONFIG_NAMES:
47+
with tempfile.TemporaryDirectory() as _tmpdir:
48+
tmpdir = Path(_tmpdir)
49+
50+
config = tmpdir / name
51+
write_config(config)
52+
53+
child = tmpdir / "child"
54+
child.mkdir()
55+
56+
with chdir(child):
57+
result = _find_config_file()
58+
assert result is not None
59+
assert Path(result[2]).resolve() == config.resolve()
60+
61+
(child / ".git").touch()
62+
63+
result = _find_config_file()
64+
assert result is None
65+
66+
def test_precedence_basic(self) -> None:
67+
with tempfile.TemporaryDirectory() as _tmpdir:
68+
tmpdir = Path(_tmpdir)
69+
70+
pyproject = tmpdir / "pyproject.toml"
71+
setup_cfg = tmpdir / "setup.cfg"
72+
mypy_ini = tmpdir / "mypy.ini"
73+
dot_mypy = tmpdir / ".mypy.ini"
74+
write_config(pyproject)
75+
write_config(setup_cfg)
76+
write_config(mypy_ini)
77+
write_config(dot_mypy)
78+
79+
with contextlib.chdir(tmpdir):
80+
result = _find_config_file()
81+
assert result is not None
82+
assert result[2] == "mypy.ini"
83+
84+
mypy_ini.unlink()
85+
result = _find_config_file()
86+
assert result is not None
87+
assert result[2] == ".mypy.ini"
88+
89+
dot_mypy.unlink()
90+
result = _find_config_file()
91+
assert result is not None
92+
assert result[2] == "pyproject.toml"
93+
94+
pyproject.unlink()
95+
result = _find_config_file()
96+
assert result is not None
97+
assert result[2] == "setup.cfg"
98+
99+
def test_precedence_missing_section(self) -> None:
100+
with tempfile.TemporaryDirectory() as _tmpdir:
101+
tmpdir = Path(_tmpdir)
102+
103+
child = tmpdir / "child"
104+
child.mkdir()
105+
106+
parent_mypy = tmpdir / "mypy.ini"
107+
child_pyproject = child / "pyproject.toml"
108+
write_config(parent_mypy)
109+
write_config(child_pyproject, content="")
110+
111+
with contextlib.chdir(child):
112+
result = _find_config_file()
113+
assert result is not None
114+
assert Path(result[2]).resolve() == parent_mypy.resolve()

test-data/unit/cmdline.pyproject.test

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -133,38 +133,3 @@ Neither is this!
133133
description = "Factory ⸻ A code generator 🏭"
134134
\[tool.mypy]
135135
[file x.py]
136-
137-
[case testSearchRecursively]
138-
# cmd: mypy x.py
139-
[file ../pyproject.toml]
140-
\[tool.mypy]
141-
\[tool.mypy.overrides]
142-
module = "x"
143-
disallow_untyped_defs = false
144-
[file x.py]
145-
pass
146-
[out]
147-
../pyproject.toml: tool.mypy.overrides sections must be an array. Please make sure you are using double brackets like so: [[tool.mypy.overrides]]
148-
== Return code: 0
149-
150-
[case testSearchRecursivelyStopsGit]
151-
# cmd: mypy x.py
152-
[file .git/test]
153-
[file ../pyproject.toml]
154-
\[tool.mypy]
155-
\[tool.mypy.overrides]
156-
module = "x"
157-
disallow_untyped_defs = false
158-
[file x.py]
159-
i: int = 0
160-
161-
[case testSearchRecursivelyStopsHg]
162-
# cmd: mypy x.py
163-
[file .hg/test]
164-
[file ../pyproject.toml]
165-
\[tool.mypy]
166-
\[tool.mypy.overrides]
167-
module = "x"
168-
disallow_untyped_defs = false
169-
[file x.py]
170-
i: int = 0

0 commit comments

Comments
 (0)