44import tempfile
55import textwrap
66import 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
1010from 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
1818class 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.
160147class 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
165153class 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