Skip to content

Commit 92c0cdb

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 0952dc8 commit 92c0cdb

3 files changed

Lines changed: 43 additions & 72 deletions

File tree

mypy/installtypes.py

Lines changed: 5 additions & 7 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] = { #FIX
24+
25+
DIST_TO_MODULE_NAME: dict[str, str] = { # FIX
2526
"python-dateutil": "dateutil",
2627
"pyyaml": "yaml",
2728
"python-xlib": "Xlib",
@@ -76,7 +77,7 @@ def resolve_stub_packages_from_lock(locked: Mapping[str, str | None]) -> list[st
7677
# dist_name,
7778
# dist_name.replace("-", "_"),
7879
# }
79-
80+
8081
# for module_name in candidates:
8182
# stub = stub_distribution_name(module_name)
8283
# if stub:
@@ -89,10 +90,7 @@ def resolve_stub_packages_from_lock(locked: Mapping[str, str | None]) -> list[st
8990
if dist_name.startswith("types-"):
9091
continue
9192

92-
candidates = {
93-
dist_name,
94-
dist_name.replace("-", "_"),
95-
}
93+
candidates = {dist_name, dist_name.replace("-", "_")}
9694

9795
mapped_module = DIST_TO_MODULE_NAME.get(dist_name)
9896
if mapped_module is not None:
@@ -115,4 +113,4 @@ def make_runtime_constraints(locked: Mapping[str, str | None]) -> list[str]:
115113
for name, version in sorted(locked.items()):
116114
if version:
117115
constraints.append(f"{name}=={version}")
118-
return constraints
116+
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: 32 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,20 @@
44
import tempfile
55
import textwrap
66
import unittest
7-
from types import SimpleNamespace #ADDED
8-
from unittest.mock import patch #ADDED
7+
from types import SimpleNamespace # ADDED
8+
from unittest.mock import patch # ADDED
99

1010
from mypy.installtypes import (
1111
make_runtime_constraints,
1212
read_locked_packages,
1313
resolve_stub_packages_from_lock,
1414
)
15-
from mypy.main import install_types #ADDED
15+
from mypy.main import install_types # ADDED
1616

1717

1818
class TestInstallTypesFromPylock(unittest.TestCase):
1919
def test_read_locked_packages(self) -> None:
20-
content = textwrap.dedent(
21-
"""
20+
content = textwrap.dedent("""
2221
[[package]]
2322
name = "requests"
2423
version = "2.32.3"
@@ -30,8 +29,7 @@ def test_read_locked_packages(self) -> None:
3029
[[package]]
3130
name = "types-requests"
3231
version = "2.32.0"
33-
"""
34-
)
32+
""")
3533
with tempfile.NamedTemporaryFile("w", suffix=".toml", delete=False, encoding="utf-8") as f:
3634
f.write(content)
3735
path = f.name
@@ -88,32 +86,24 @@ def test_read_locked_packages_empty_file(self) -> None:
8886
assert locked == {}
8987

9088
def test_resolve_stub_packages_from_lock(self) -> None:
91-
locked = {
92-
"requests": "2.32.3",
93-
"python-dateutil": "2.9.0",
94-
"types-requests": "2.32.0",
95-
}
89+
locked = {"requests": "2.32.3", "python-dateutil": "2.9.0", "types-requests": "2.32.0"}
9690
stubs = resolve_stub_packages_from_lock(locked)
9791
# requests already worked before fix because distribution name == module name.
9892
assert "types-requests" in stubs
9993

10094
# FIX: Before my fix, this failed because the lock file used the distribution name
10195
# "python-dateutil" but stubinfo.py knows the module/import name "dateutil".
10296
# After adding the explicit distribution->module mapping, it should resolve.
103-
assert "types-python-dateutil" in stubs #FIX
97+
assert "types-python-dateutil" in stubs # FIX
10498

105-
#TEST: checks explicit distribution->module mapping
99+
# TEST: checks explicit distribution->module mapping
106100
def test_resolve_stub_packages_from_lock_handles_distribution_module_mismatch(self) -> None:
107-
locked = {
108-
"python-dateutil": "2.9.0",
109-
}
101+
locked = {"python-dateutil": "2.9.0"}
110102
stubs = resolve_stub_packages_from_lock(locked)
111103
assert "types-python-dateutil" in stubs
112-
104+
113105
def test_resolve_stub_packages_skips_types_packages(self) -> None:
114-
locked = {
115-
"types-requests": "2.32.0",
116-
}
106+
locked = {"types-requests": "2.32.0"}
117107
stubs = resolve_stub_packages_from_lock(locked)
118108
# Should not produce "types-types-requests"
119109
assert "types-types-requests" not in stubs
@@ -133,11 +123,7 @@ def test_resolve_stub_packages_pyyaml_mapping(self) -> None:
133123
assert "types-PyYAML" in stubs
134124

135125
def test_make_runtime_constraints(self) -> None:
136-
locked = {
137-
"requests": "2.32.3",
138-
"python-dateutil": "2.9.0",
139-
"no-version": None,
140-
}
126+
locked = {"requests": "2.32.3", "python-dateutil": "2.9.0", "no-version": None}
141127
constraints = make_runtime_constraints(locked)
142128
assert constraints == ["python-dateutil==2.9.0", "requests==2.32.3"]
143129

@@ -156,46 +142,41 @@ def test_make_runtime_constraints_is_sorted(self) -> None:
156142
constraints = make_runtime_constraints(locked)
157143
assert constraints == sorted(constraints)
158144

159-
#FIX: stub/mock object that pretends to be a formatter so tests don’t crash.
145+
146+
# FIX: stub/mock object that pretends to be a formatter so tests don’t crash.
160147
class DummyFormatter:
161148
def style(self, text: str, *args: object, **kwargs: object) -> str:
162149
return text
163150

164-
#TEST: integrations tests
151+
152+
# TEST: integrations tests
165153
class TestInstallTypesFromPylockIntegration(unittest.TestCase):
166154
def make_options(self) -> SimpleNamespace:
167-
return SimpleNamespace(
168-
python_executable="python",
169-
cache_dir="unused",
170-
)
155+
return SimpleNamespace(python_executable="python", cache_dir="unused")
171156

172157
@patch("mypy.main.subprocess.run")
173158
def test_install_types_builds_correct_pip_command(self, mock_run) -> None:
174-
content = textwrap.dedent(
175-
"""
159+
content = textwrap.dedent("""
176160
[[package]]
177161
name = "requests"
178162
version = "2.32.3"
179163
180164
[[package]]
181165
name = "python-dateutil"
182166
version = "2.9.0"
183-
"""
184-
)
167+
""")
185168

186-
with tempfile.NamedTemporaryFile(
187-
"w", suffix=".toml", delete=False, encoding="utf-8"
188-
) as f:
169+
with tempfile.NamedTemporaryFile("w", suffix=".toml", delete=False, encoding="utf-8") as f:
189170
f.write(content)
190171
path = f.name
191172

192173
try:
193174
options = self.make_options()
194-
formatter = DummyFormatter() #FIX
175+
formatter = DummyFormatter() # FIX
195176

196177
result = install_types(
197178
# formatter=None,
198-
formatter=formatter, # FIX
179+
formatter=formatter, # FIX
199180
options=options,
200181
non_interactive=True,
201182
pylock_path=path,
@@ -229,27 +210,23 @@ def test_install_types_builds_correct_pip_command(self, mock_run) -> None:
229210

230211
@patch("mypy.main.subprocess.run")
231212
def test_no_stubs_found_skips_install(self, mock_run) -> None:
232-
content = textwrap.dedent(
233-
"""
213+
content = textwrap.dedent("""
234214
[[package]]
235215
name = "unknown-lib"
236216
version = "1.0.0"
237-
"""
238-
)
217+
""")
239218

240-
with tempfile.NamedTemporaryFile(
241-
"w", suffix=".toml", delete=False, encoding="utf-8"
242-
) as f:
219+
with tempfile.NamedTemporaryFile("w", suffix=".toml", delete=False, encoding="utf-8") as f:
243220
f.write(content)
244221
path = f.name
245222

246223
try:
247224
options = self.make_options()
248-
formatter = DummyFormatter() #FIX
225+
formatter = DummyFormatter() # FIX
249226

250227
result = install_types(
251228
# formatter=None,
252-
formatter=formatter, #FIX
229+
formatter=formatter, # FIX
253230
options=options,
254231
non_interactive=True,
255232
pylock_path=path,
@@ -263,13 +240,11 @@ def test_no_stubs_found_skips_install(self, mock_run) -> None:
263240

264241
@patch("mypy.main.subprocess.run")
265242
def test_constraint_file_cleaned_up_after_success(self, mock_run) -> None:
266-
content = textwrap.dedent(
267-
"""
243+
content = textwrap.dedent("""
268244
[[package]]
269245
name = "requests"
270246
version = "2.32.3"
271-
"""
272-
)
247+
""")
273248
with tempfile.NamedTemporaryFile("w", suffix=".toml", delete=False, encoding="utf-8") as f:
274249
f.write(content)
275250
path = f.name
@@ -300,13 +275,11 @@ def capture_cmd(cmd: list[str], **kwargs: object) -> None:
300275

301276
@patch("mypy.main.subprocess.run")
302277
def test_constraint_file_cleaned_up_even_if_subprocess_fails(self, mock_run) -> None:
303-
content = textwrap.dedent(
304-
"""
278+
content = textwrap.dedent("""
305279
[[package]]
306280
name = "requests"
307281
version = "2.32.3"
308-
"""
309-
)
282+
""")
310283
with tempfile.NamedTemporaryFile("w", suffix=".toml", delete=False, encoding="utf-8") as f:
311284
f.write(content)
312285
path = f.name
@@ -335,4 +308,4 @@ def capture_and_raise(cmd: list[str], **kwargs: object) -> None:
335308
self.assertFalse(
336309
os.path.exists(captured[0]),
337310
"Temp constraint file should be deleted even when subprocess raises",
338-
)
311+
)

0 commit comments

Comments
 (0)