@@ -211,42 +211,9 @@ def test_lazy_import_type_exposed(self):
211211 self .assertHasAttr (types , 'LazyImportType' )
212212 self .assertEqual (types .LazyImportType .__name__ , 'lazy_import' )
213213
214- def test_lazy_import_type_invalid_name (self ):
215- """passing invalid name to lazy imports should raise a TypeError"""
216- with self .assertRaises (TypeError ) as cm :
217- types .LazyImportType ({}, None )
218-
219- def test_lazy_import_type_invalid_fromlist_type (self ):
220- """LazyImportType should reject invalid fromlist types."""
221- # fromlist must be None, a string, or a tuple - not an int
222- with self .assertRaises (TypeError ) as cm :
223- types .LazyImportType ({}, "module" , 0 )
224- self .assertIn ("fromlist must be None, a string, or a tuple" , str (cm .exception ))
225-
226- # Also test with other invalid types
227- with self .assertRaises (TypeError ):
228- types .LazyImportType ({}, "module" , []) # list not allowed
229-
230- with self .assertRaises (TypeError ):
231- types .LazyImportType ({}, "module" , {"x" : 1 }) # dict not allowed
232-
233- def test_lazy_import_type_valid_fromlist (self ):
234- """LazyImportType should accept valid fromlist types."""
235- # None is valid (implicit)
236- lazy1 = types .LazyImportType ({}, "module" )
237- self .assertIsNotNone (lazy1 )
238-
239- # Explicit None is valid
240- lazy2 = types .LazyImportType ({}, "module" , None )
241- self .assertIsNotNone (lazy2 )
242-
243- # String is valid
244- lazy3 = types .LazyImportType ({}, "module" , "attr" )
245- self .assertIsNotNone (lazy3 )
246-
247- # Tuple is valid
248- lazy4 = types .LazyImportType ({}, "module" , ("attr1" , "attr2" ))
249- self .assertIsNotNone (lazy4 )
214+ def test_lazy_import_type_cant_construct (self ):
215+ """LazyImportType should not be directly constructible."""
216+ self .assertRaises (TypeError , types .LazyImportType , {}, "module" )
250217
251218
252219class SyntaxRestrictionTests (unittest .TestCase ):
@@ -768,6 +735,38 @@ def test_resolve():
768735 self .assertEqual (result .returncode , 0 , f"stdout: { result .stdout } , stderr: { result .stderr } " )
769736 self .assertIn ("OK" , result .stdout )
770737
738+ def test_add_lazy_to_globals (self ):
739+ code = textwrap .dedent ("""
740+ import sys
741+ import types
742+
743+ lazy from test.test_import.data.lazy_imports import basic2
744+
745+ assert 'test.test_import.data.lazy_imports.basic2' not in sys.modules
746+
747+ class C: pass
748+ sneaky = C()
749+ sneaky.x = 1
750+
751+ def f():
752+ t = 0
753+ for _ in range(5):
754+ t += sneaky.x
755+ return t
756+
757+ f()
758+ globals()["sneaky"] = globals()["basic2"]
759+ assert f() == 210
760+ print("OK")
761+ """ )
762+ result = subprocess .run (
763+ [sys .executable , "-c" , code ],
764+ capture_output = True ,
765+ text = True
766+ )
767+ self .assertEqual (result .returncode , 0 , f"stdout: { result .stdout } , stderr: { result .stderr } " )
768+ self .assertIn ("OK" , result .stdout )
769+
771770
772771class MultipleNameFromImportTests (unittest .TestCase ):
773772 """Tests for lazy from ... import with multiple names.
0 commit comments