Skip to content

Commit 513bb30

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent b186008 commit 513bb30

3 files changed

Lines changed: 46 additions & 77 deletions

File tree

mypy/installtypes.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
def normalize_distribution_name(name: str) -> str:
2222
return _DIST_NORMALIZE_RE.sub("-", name).lower()
2323

24-
DIST_TO_MODULE_NAME: dict[str, str] = {
24+
25+
DIST_TO_MODULE_NAME: dict[str, str] = {
2526
"python-dateutil": "dateutil",
2627
"pyyaml": "yaml",
2728
"python-xlib": "Xlib",
@@ -72,10 +73,7 @@ def resolve_stub_packages_from_lock(locked: Mapping[str, str | None]) -> list[st
7273
if dist_name.startswith("types-"):
7374
continue
7475

75-
candidates = {
76-
dist_name,
77-
dist_name.replace("-", "_"),
78-
}
76+
candidates = {dist_name, dist_name.replace("-", "_")}
7977

8078
mapped_module = DIST_TO_MODULE_NAME.get(dist_name)
8179
if mapped_module is not None:
@@ -98,4 +96,4 @@ def make_runtime_constraints(locked: Mapping[str, str | None]) -> list[str]:
9896
for name, version in sorted(locked.items()):
9997
if version:
10098
constraints.append(f"{name}=={version}")
101-
return constraints
99+
return constraints

mypy/main.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@
3030
from mypy.errors import CompileError
3131
from mypy.find_sources import InvalidSourceList, create_source_list
3232
from mypy.fscache import FileSystemCache
33-
from mypy.installtypes import make_runtime_constraints, read_locked_packages, resolve_stub_packages_from_lock
33+
from mypy.installtypes import (
34+
make_runtime_constraints,
35+
read_locked_packages,
36+
resolve_stub_packages_from_lock,
37+
)
3438
from mypy.modulefinder import (
3539
BuildSource,
3640
FindModuleCache,
@@ -136,11 +140,7 @@ def main(
136140
if options.install_types_from_pylock is not None and not os.path.isfile(
137141
options.install_types_from_pylock
138142
):
139-
fail(
140-
f"error: Can't find lock file '{options.install_types_from_pylock}'",
141-
stderr,
142-
options,
143-
)
143+
fail(f"error: Can't find lock file '{options.install_types_from_pylock}'", stderr, options)
144144

145145
if options.install_types and not options.incremental:
146146
fail(

mypy/test/testinstalltypes.py

Lines changed: 36 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
11
from __future__ import annotations
22

33
import os
4+
import sys
45
import tempfile
56
import textwrap
67
import unittest
7-
import sys
8-
from unittest.mock import MagicMock, patch
9-
from mypy.options import Options
10-
from mypy.util import FancyFormatter
11-
8+
from unittest.mock import MagicMock, patch
129

1310
from mypy.installtypes import (
1411
make_runtime_constraints,
1512
read_locked_packages,
1613
resolve_stub_packages_from_lock,
1714
)
18-
from mypy.main import install_types
15+
from mypy.main import install_types
16+
from mypy.options import Options
17+
from mypy.util import FancyFormatter
1918

2019

2120
class TestInstallTypesFromPylock(unittest.TestCase):
2221
def test_read_locked_packages(self) -> None:
23-
content = textwrap.dedent(
24-
"""
22+
content = textwrap.dedent("""
2523
[[package]]
2624
name = "requests"
2725
version = "2.32.3"
@@ -33,8 +31,7 @@ def test_read_locked_packages(self) -> None:
3331
[[package]]
3432
name = "types-requests"
3533
version = "2.32.0"
36-
"""
37-
)
34+
""")
3835
with tempfile.NamedTemporaryFile("w", suffix=".toml", delete=False, encoding="utf-8") as f:
3936
f.write(content)
4037
path = f.name
@@ -91,27 +88,19 @@ def test_read_locked_packages_empty_file(self) -> None:
9188
assert locked == {}
9289

9390
def test_resolve_stub_packages_from_lock(self) -> None:
94-
locked = {
95-
"requests": "2.32.3",
96-
"python-dateutil": "2.9.0",
97-
"types-requests": "2.32.0",
98-
}
91+
locked = {"requests": "2.32.3", "python-dateutil": "2.9.0", "types-requests": "2.32.0"}
9992
stubs = resolve_stub_packages_from_lock(locked)
10093
assert "types-requests" in stubs
101-
assert "types-python-dateutil" in stubs
94+
assert "types-python-dateutil" in stubs
10295

103-
#TEST: checks explicit distribution->module mapping
96+
# TEST: checks explicit distribution->module mapping
10497
def test_resolve_stub_packages_from_lock_handles_distribution_module_mismatch(self) -> None:
105-
locked = {
106-
"python-dateutil": "2.9.0",
107-
}
98+
locked = {"python-dateutil": "2.9.0"}
10899
stubs = resolve_stub_packages_from_lock(locked)
109100
assert "types-python-dateutil" in stubs
110-
101+
111102
def test_resolve_stub_packages_skips_types_packages(self) -> None:
112-
locked = {
113-
"types-requests": "2.32.0",
114-
}
103+
locked = {"types-requests": "2.32.0"}
115104
stubs = resolve_stub_packages_from_lock(locked)
116105
# Should not produce "types-types-requests"
117106
assert "types-types-requests" not in stubs
@@ -131,11 +120,7 @@ def test_resolve_stub_packages_pyyaml_mapping(self) -> None:
131120
assert "types-PyYAML" in stubs
132121

133122
def test_make_runtime_constraints(self) -> None:
134-
locked = {
135-
"requests": "2.32.3",
136-
"python-dateutil": "2.9.0",
137-
"no-version": None,
138-
}
123+
locked = {"requests": "2.32.3", "python-dateutil": "2.9.0", "no-version": None}
139124
constraints = make_runtime_constraints(locked)
140125
assert constraints == ["python-dateutil==2.9.0", "requests==2.32.3"]
141126

@@ -154,45 +139,40 @@ def test_make_runtime_constraints_is_sorted(self) -> None:
154139
constraints = make_runtime_constraints(locked)
155140
assert constraints == sorted(constraints)
156141

157-
#TEST: integrations tests
142+
143+
# TEST: integrations tests
158144
class TestInstallTypesFromPylockIntegration(unittest.TestCase):
159145
def make_options(self) -> Options:
160146
options = Options()
161147
options.python_executable = "python"
162148
options.cache_dir = "unused"
163149
return options
150+
164151
def make_formatter(self) -> FancyFormatter:
165152
return FancyFormatter(sys.stdout, sys.stderr, False)
166153

167154
@patch("mypy.main.subprocess.run")
168155
def test_install_types_builds_correct_pip_command(self, mock_run: MagicMock) -> None:
169-
content = textwrap.dedent(
170-
"""
156+
content = textwrap.dedent("""
171157
[[package]]
172158
name = "requests"
173159
version = "2.32.3"
174160
175161
[[package]]
176162
name = "python-dateutil"
177163
version = "2.9.0"
178-
"""
179-
)
164+
""")
180165

181-
with tempfile.NamedTemporaryFile(
182-
"w", suffix=".toml", delete=False, encoding="utf-8"
183-
) as f:
166+
with tempfile.NamedTemporaryFile("w", suffix=".toml", delete=False, encoding="utf-8") as f:
184167
f.write(content)
185168
path = f.name
186169

187170
try:
188171
options = self.make_options()
189-
formatter = self.make_formatter()
172+
formatter = self.make_formatter()
190173

191174
result = install_types(
192-
formatter=formatter,
193-
options=options,
194-
non_interactive=True,
195-
pylock_path=path,
175+
formatter=formatter, options=options, non_interactive=True, pylock_path=path
196176
)
197177

198178
self.assertTrue(result)
@@ -215,30 +195,23 @@ def test_install_types_builds_correct_pip_command(self, mock_run: MagicMock) ->
215195
os.unlink(path)
216196

217197
@patch("mypy.main.subprocess.run")
218-
def test_no_stubs_found_skips_install(self, mock_run: MagicMock) -> None:
219-
content = textwrap.dedent(
220-
"""
198+
def test_no_stubs_found_skips_install(self, mock_run: MagicMock) -> None:
199+
content = textwrap.dedent("""
221200
[[package]]
222201
name = "unknown-lib"
223202
version = "1.0.0"
224-
"""
225-
)
203+
""")
226204

227-
with tempfile.NamedTemporaryFile(
228-
"w", suffix=".toml", delete=False, encoding="utf-8"
229-
) as f:
205+
with tempfile.NamedTemporaryFile("w", suffix=".toml", delete=False, encoding="utf-8") as f:
230206
f.write(content)
231207
path = f.name
232208

233209
try:
234210
options = self.make_options()
235-
formatter = self.make_formatter()
211+
formatter = self.make_formatter()
236212

237213
result = install_types(
238-
formatter=formatter,
239-
options=options,
240-
non_interactive=True,
241-
pylock_path=path,
214+
formatter=formatter, options=options, non_interactive=True, pylock_path=path
242215
)
243216

244217
self.assertFalse(result)
@@ -249,13 +222,11 @@ def test_no_stubs_found_skips_install(self, mock_run: MagicMock) -> None:
249222

250223
@patch("mypy.main.subprocess.run")
251224
def test_constraint_file_cleaned_up_after_success(self, mock_run: MagicMock) -> None:
252-
content = textwrap.dedent(
253-
"""
225+
content = textwrap.dedent("""
254226
[[package]]
255227
name = "requests"
256228
version = "2.32.3"
257-
"""
258-
)
229+
""")
259230
with tempfile.NamedTemporaryFile("w", suffix=".toml", delete=False, encoding="utf-8") as f:
260231
f.write(content)
261232
path = f.name
@@ -285,14 +256,14 @@ def capture_cmd(cmd: list[str], **kwargs: object) -> None:
285256
)
286257

287258
@patch("mypy.main.subprocess.run")
288-
def test_constraint_file_cleaned_up_even_if_subprocess_fails(self, mock_run: MagicMock) -> None:
289-
content = textwrap.dedent(
290-
"""
259+
def test_constraint_file_cleaned_up_even_if_subprocess_fails(
260+
self, mock_run: MagicMock
261+
) -> None:
262+
content = textwrap.dedent("""
291263
[[package]]
292264
name = "requests"
293265
version = "2.32.3"
294-
"""
295-
)
266+
""")
296267
with tempfile.NamedTemporaryFile("w", suffix=".toml", delete=False, encoding="utf-8") as f:
297268
f.write(content)
298269
path = f.name
@@ -321,4 +292,4 @@ def capture_and_raise(cmd: list[str], **kwargs: object) -> None:
321292
self.assertFalse(
322293
os.path.exists(captured[0]),
323294
"Temp constraint file should be deleted even when subprocess raises",
324-
)
295+
)

0 commit comments

Comments
 (0)