11from __future__ import annotations
22
33import os
4+ import sys
45import tempfile
56import textwrap
67import 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
1310from 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
2120class 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
158144class 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