11"""Tests for PEP 810 lazy imports."""
22
33import sys
4+ import subprocess
5+ import textwrap
6+ import threading
47import types
58import unittest
6- import threading
7- import textwrap
8- import subprocess
9- from test .support import import_helper
109
1110
1211class LazyImportTests (unittest .TestCase ):
@@ -24,43 +23,43 @@ def tearDown(self):
2423 def test_basic_unused (self ):
2524 """Lazy imported module should not be loaded if never accessed."""
2625 import test .test_import .data .lazy_imports .basic_unused
27- self .assertFalse ("test.test_import.data.lazy_imports.basic2" in sys .modules )
26+ self .assertNotIn ("test.test_import.data.lazy_imports.basic2" , sys .modules )
2827
2928 def test_basic_unused_use_externally (self ):
3029 """Lazy import should load module when accessed from outside."""
3130 from test .test_import .data .lazy_imports import basic_unused
3231
33- self .assertFalse ("test.test_import.data.lazy_imports.basic2" in sys .modules )
32+ self .assertNotIn ("test.test_import.data.lazy_imports.basic2" , sys .modules )
3433 x = basic_unused .test .test_import .data .lazy_imports .basic2
35- self .assertTrue ("test.test_import.data.lazy_imports.basic2" in sys .modules )
34+ self .assertIn ("test.test_import.data.lazy_imports.basic2" , sys .modules )
3635
3736 def test_basic_from_unused_use_externally (self ):
3837 """Lazy 'from' import should load when accessed from outside."""
3938 from test .test_import .data .lazy_imports import basic_from_unused
4039
41- self .assertFalse ("test.test_import.data.lazy_imports.basic2" in sys .modules )
40+ self .assertNotIn ("test.test_import.data.lazy_imports.basic2" , sys .modules )
4241 x = basic_from_unused .basic2
43- self .assertTrue ("test.test_import.data.lazy_imports.basic2" in sys .modules )
42+ self .assertIn ("test.test_import.data.lazy_imports.basic2" , sys .modules )
4443
4544 def test_basic_unused_dir (self ):
4645 """dir() on module should not trigger lazy import reification."""
4746 import test .test_import .data .lazy_imports .basic_unused
4847
4948 x = dir (test .test_import .data .lazy_imports .basic_unused )
5049 self .assertIn ("test" , x )
51- self .assertFalse ("test.test_import.data.lazy_imports.basic2" in sys .modules )
50+ self .assertNotIn ("test.test_import.data.lazy_imports.basic2" , sys .modules )
5251
5352 def test_basic_dir (self ):
5453 """dir() at module scope should not trigger lazy import reification."""
5554 from test .test_import .data .lazy_imports import basic_dir
5655
5756 self .assertIn ("test" , basic_dir .x )
58- self .assertFalse ("test.test_import.data.lazy_imports.basic2" in sys .modules )
57+ self .assertNotIn ("test.test_import.data.lazy_imports.basic2" , sys .modules )
5958
6059 def test_basic_used (self ):
6160 """Lazy import should load when accessed within the module."""
6261 import test .test_import .data .lazy_imports .basic_used
63- self .assertTrue ("test.test_import.data.lazy_imports.basic2" in sys .modules )
62+ self .assertIn ("test.test_import.data.lazy_imports.basic2" , sys .modules )
6463
6564
6665class GlobalLazyImportModeTests (unittest .TestCase ):
@@ -77,32 +76,32 @@ def tearDown(self):
7776 def test_global_off (self ):
7877 """Mode 'none' should disable lazy imports entirely."""
7978 import test .test_import .data .lazy_imports .global_off
80- self .assertTrue ("test.test_import.data.lazy_imports.basic2" in sys .modules )
79+ self .assertIn ("test.test_import.data.lazy_imports.basic2" , sys .modules )
8180
8281 def test_global_on (self ):
8382 """Mode 'all' should make regular imports lazy."""
8483 import test .test_import .data .lazy_imports .global_on
85- self .assertFalse ("test.test_import.data.lazy_imports.basic2" in sys .modules )
84+ self .assertNotIn ("test.test_import.data.lazy_imports.basic2" , sys .modules )
8685
8786 def test_global_filter (self ):
8887 """Filter returning False should prevent lazy loading."""
8988 import test .test_import .data .lazy_imports .global_filter
90- self .assertTrue ("test.test_import.data.lazy_imports.basic2" in sys .modules )
89+ self .assertIn ("test.test_import.data.lazy_imports.basic2" , sys .modules )
9190
9291 def test_global_filter_true (self ):
9392 """Filter returning True should allow lazy loading."""
9493 import test .test_import .data .lazy_imports .global_filter_true
95- self .assertFalse ("test.test_import.data.lazy_imports.basic2" in sys .modules )
94+ self .assertNotIn ("test.test_import.data.lazy_imports.basic2" , sys .modules )
9695
9796 def test_global_filter_from (self ):
9897 """Filter should work with 'from' imports."""
9998 import test .test_import .data .lazy_imports .global_filter
100- self .assertTrue ("test.test_import.data.lazy_imports.basic2" in sys .modules )
99+ self .assertIn ("test.test_import.data.lazy_imports.basic2" , sys .modules )
101100
102101 def test_global_filter_from_true (self ):
103102 """Filter returning True should allow lazy 'from' imports."""
104103 import test .test_import .data .lazy_imports .global_filter_true
105- self .assertFalse ("test.test_import.data.lazy_imports.basic2" in sys .modules )
104+ self .assertNotIn ("test.test_import.data.lazy_imports.basic2" , sys .modules )
106105
107106
108107class CompatibilityModeTests (unittest .TestCase ):
@@ -119,27 +118,27 @@ def tearDown(self):
119118 def test_compatibility_mode (self ):
120119 """__lazy_modules__ should enable lazy imports for listed modules."""
121120 import test .test_import .data .lazy_imports .basic_compatibility_mode
122- self .assertFalse ("test.test_import.data.lazy_imports.basic2" in sys .modules )
121+ self .assertNotIn ("test.test_import.data.lazy_imports.basic2" , sys .modules )
123122
124123 def test_compatibility_mode_used (self ):
125124 """Using a lazy import from __lazy_modules__ should load the module."""
126125 import test .test_import .data .lazy_imports .basic_compatibility_mode_used
127- self .assertTrue ("test.test_import.data.lazy_imports.basic2" in sys .modules )
126+ self .assertIn ("test.test_import.data.lazy_imports.basic2" , sys .modules )
128127
129128 def test_compatibility_mode_func (self ):
130129 """Imports inside functions should be eager even in compatibility mode."""
131130 import test .test_import .data .lazy_imports .compatibility_mode_func
132- self .assertTrue ("test.test_import.data.lazy_imports.basic2" in sys .modules )
131+ self .assertIn ("test.test_import.data.lazy_imports.basic2" , sys .modules )
133132
134133 def test_compatibility_mode_try_except (self ):
135134 """Imports in try/except should be eager even in compatibility mode."""
136135 import test .test_import .data .lazy_imports .compatibility_mode_try_except
137- self .assertTrue ("test.test_import.data.lazy_imports.basic2" in sys .modules )
136+ self .assertIn ("test.test_import.data.lazy_imports.basic2" , sys .modules )
138137
139138 def test_compatibility_mode_relative (self ):
140139 """__lazy_modules__ should work with relative imports."""
141140 import test .test_import .data .lazy_imports .basic_compatibility_mode_relative
142- self .assertFalse ("test.test_import.data.lazy_imports.basic2" in sys .modules )
141+ self .assertNotIn ("test.test_import.data.lazy_imports.basic2" , sys .modules )
143142
144143
145144class ModuleIntrospectionTests (unittest .TestCase ):
@@ -156,17 +155,17 @@ def tearDown(self):
156155 def test_modules_dict (self ):
157156 """Accessing module.__dict__ should not trigger reification."""
158157 import test .test_import .data .lazy_imports .modules_dict
159- self .assertFalse ("test.test_import.data.lazy_imports.basic2" in sys .modules )
158+ self .assertNotIn ("test.test_import.data.lazy_imports.basic2" , sys .modules )
160159
161160 def test_modules_getattr (self ):
162161 """Module __getattr__ for lazy import name should trigger reification."""
163162 import test .test_import .data .lazy_imports .modules_getattr
164- self .assertTrue ("test.test_import.data.lazy_imports.basic2" in sys .modules )
163+ self .assertIn ("test.test_import.data.lazy_imports.basic2" , sys .modules )
165164
166165 def test_modules_getattr_other (self ):
167166 """Module __getattr__ for other names should not trigger reification."""
168167 import test .test_import .data .lazy_imports .modules_getattr_other
169- self .assertFalse ("test.test_import.data.lazy_imports.basic2" in sys .modules )
168+ self .assertNotIn ("test.test_import.data.lazy_imports.basic2" , sys .modules )
170169
171170
172171class LazyImportTypeTests (unittest .TestCase ):
@@ -183,11 +182,11 @@ def tearDown(self):
183182 def test_lazy_value_resolve (self ):
184183 """resolve() method should force the lazy import to load."""
185184 import test .test_import .data .lazy_imports .lazy_get_value
186- self .assertTrue ("test.test_import.data.lazy_imports.basic2" in sys .modules )
185+ self .assertIn ("test.test_import.data.lazy_imports.basic2" , sys .modules )
187186
188187 def test_lazy_import_type_exposed (self ):
189188 """LazyImportType should be exposed in types module."""
190- self .assertTrue ( hasattr ( types , 'LazyImportType' ) )
189+ self .assertHasAttr ( types , 'LazyImportType' )
191190 self .assertEqual (types .LazyImportType .__name__ , 'lazy_import' )
192191
193192 def test_lazy_import_type_invalid_fromlist_type (self ):
@@ -307,13 +306,13 @@ def test_try_except_eager(self):
307306 """Imports in try/except should be eager even with mode='all'."""
308307 sys .set_lazy_imports ("all" )
309308 import test .test_import .data .lazy_imports .try_except_eager
310- self .assertTrue ("test.test_import.data.lazy_imports.basic2" in sys .modules )
309+ self .assertIn ("test.test_import.data.lazy_imports.basic2" , sys .modules )
311310
312311 def test_try_except_eager_from (self ):
313312 """From imports in try/except should be eager even with mode='all'."""
314313 sys .set_lazy_imports ("all" )
315314 import test .test_import .data .lazy_imports .try_except_eager_from
316- self .assertTrue ("test.test_import.data.lazy_imports.basic2" in sys .modules )
315+ self .assertIn ("test.test_import.data.lazy_imports.basic2" , sys .modules )
317316
318317 def test_eager_import_func (self ):
319318 """Imports inside functions should return modules, not proxies."""
@@ -338,12 +337,12 @@ def tearDown(self):
338337 def test_lazy_with (self ):
339338 """lazy import with 'with' statement should work."""
340339 import test .test_import .data .lazy_imports .lazy_with
341- self .assertFalse ("test.test_import.data.lazy_imports.basic2" in sys .modules )
340+ self .assertNotIn ("test.test_import.data.lazy_imports.basic2" , sys .modules )
342341
343342 def test_lazy_with_from (self ):
344343 """lazy from import with 'with' statement should work."""
345344 import test .test_import .data .lazy_imports .lazy_with_from
346- self .assertFalse ("test.test_import.data.lazy_imports.basic2" in sys .modules )
345+ self .assertNotIn ("test.test_import.data.lazy_imports.basic2" , sys .modules )
347346
348347
349348class PackageTests (unittest .TestCase ):
@@ -361,16 +360,16 @@ def test_lazy_import_pkg(self):
361360 """lazy import of package submodule should load the package."""
362361 import test .test_import .data .lazy_imports .lazy_import_pkg
363362
364- self .assertTrue ("test.test_import.data.lazy_imports.pkg" in sys .modules )
365- self .assertTrue ("test.test_import.data.lazy_imports.pkg.bar" in sys .modules )
363+ self .assertIn ("test.test_import.data.lazy_imports.pkg" , sys .modules )
364+ self .assertIn ("test.test_import.data.lazy_imports.pkg.bar" , sys .modules )
366365
367366 def test_lazy_import_pkg_cross_import (self ):
368367 """Cross-imports within package should preserve lazy imports."""
369368 import test .test_import .data .lazy_imports .pkg .c
370369
371- self .assertTrue ("test.test_import.data.lazy_imports.pkg" in sys .modules )
372- self .assertTrue ("test.test_import.data.lazy_imports.pkg.c" in sys .modules )
373- self .assertFalse ("test.test_import.data.lazy_imports.pkg.b" in sys .modules )
370+ self .assertIn ("test.test_import.data.lazy_imports.pkg" , sys .modules )
371+ self .assertIn ("test.test_import.data.lazy_imports.pkg.c" , sys .modules )
372+ self .assertNotIn ("test.test_import.data.lazy_imports.pkg.b" , sys .modules )
374373
375374 g = test .test_import .data .lazy_imports .pkg .c .get_globals ()
376375 self .assertEqual (type (g ["x" ]), int )
@@ -391,18 +390,18 @@ def tearDown(self):
391390 def test_dunder_lazy_import (self ):
392391 """__lazy_import__ should create lazy import proxy."""
393392 import test .test_import .data .lazy_imports .dunder_lazy_import
394- self .assertFalse ("test.test_import.data.lazy_imports.basic2" in sys .modules )
393+ self .assertNotIn ("test.test_import.data.lazy_imports.basic2" , sys .modules )
395394
396395 def test_dunder_lazy_import_used (self ):
397396 """Using __lazy_import__ result should trigger module load."""
398397 import test .test_import .data .lazy_imports .dunder_lazy_import_used
399- self .assertTrue ("test.test_import.data.lazy_imports.basic2" in sys .modules )
398+ self .assertIn ("test.test_import.data.lazy_imports.basic2" , sys .modules )
400399
401400 def test_dunder_lazy_import_builtins (self ):
402401 """__lazy_import__ should use module's __builtins__ for __import__."""
403402 from test .test_import .data .lazy_imports import dunder_lazy_import_builtins
404403
405- self .assertFalse ("test.test_import.data.lazy_imports.basic2" in sys .modules )
404+ self .assertNotIn ("test.test_import.data.lazy_imports.basic2" , sys .modules )
406405 self .assertEqual (dunder_lazy_import_builtins .basic , 42 )
407406
408407
@@ -693,22 +692,22 @@ def test_module_dict_returns_lazy_proxy_without_reifying(self):
693692 import test .test_import .data .lazy_imports .globals_access
694693
695694 # Module not loaded yet via direct dict access
696- self .assertFalse ("test.test_import.data.lazy_imports.basic2" in sys .modules )
695+ self .assertNotIn ("test.test_import.data.lazy_imports.basic2" , sys .modules )
697696
698697 # Access via get_from_globals should return lazy proxy
699698 lazy_obj = test .test_import .data .lazy_imports .globals_access .get_from_globals ()
700699 self .assertEqual (type (lazy_obj ), types .LazyImportType )
701- self .assertFalse ("test.test_import.data.lazy_imports.basic2" in sys .modules )
700+ self .assertNotIn ("test.test_import.data.lazy_imports.basic2" , sys .modules )
702701
703702 def test_direct_access_triggers_reification (self ):
704703 """Direct name access (not through globals()) should trigger reification."""
705704 import test .test_import .data .lazy_imports .globals_access
706705
707- self .assertFalse ("test.test_import.data.lazy_imports.basic2" in sys .modules )
706+ self .assertNotIn ("test.test_import.data.lazy_imports.basic2" , sys .modules )
708707
709708 # Direct access should reify
710709 result = test .test_import .data .lazy_imports .globals_access .get_direct ()
711- self .assertTrue ("test.test_import.data.lazy_imports.basic2" in sys .modules )
710+ self .assertIn ("test.test_import.data.lazy_imports.basic2" , sys .modules )
712711
713712 def test_resolve_method_forces_reification (self ):
714713 """Calling resolve() on lazy proxy should force reification.
@@ -1237,23 +1236,23 @@ def test_relative_lazy_import(self):
12371236 from test .test_import .data .lazy_imports import relative_lazy
12381237
12391238 # basic2 should not be loaded yet
1240- self .assertFalse ("test.test_import.data.lazy_imports.basic2" in sys .modules )
1239+ self .assertNotIn ("test.test_import.data.lazy_imports.basic2" , sys .modules )
12411240
12421241 # Access triggers reification
12431242 result = relative_lazy .get_basic2 ()
1244- self .assertTrue ("test.test_import.data.lazy_imports.basic2" in sys .modules )
1243+ self .assertIn ("test.test_import.data.lazy_imports.basic2" , sys .modules )
12451244
12461245 def test_relative_lazy_from_import (self ):
12471246 """lazy from .module import name should work."""
12481247 from test .test_import .data .lazy_imports import relative_lazy_from
12491248
12501249 # basic2 should not be loaded yet
1251- self .assertFalse ("test.test_import.data.lazy_imports.basic2" in sys .modules )
1250+ self .assertNotIn ("test.test_import.data.lazy_imports.basic2" , sys .modules )
12521251
12531252 # Access triggers reification
12541253 result = relative_lazy_from .get_x ()
12551254 self .assertEqual (result , 42 )
1256- self .assertTrue ("test.test_import.data.lazy_imports.basic2" in sys .modules )
1255+ self .assertIn ("test.test_import.data.lazy_imports.basic2" , sys .modules )
12571256
12581257
12591258class LazyModulesCompatibilityFromImportTests (unittest .TestCase ):
@@ -1276,12 +1275,12 @@ def test_lazy_modules_makes_from_imports_lazy(self):
12761275 from test .test_import .data .lazy_imports import lazy_compat_from
12771276
12781277 # basic2 should not be loaded yet because it's in __lazy_modules__
1279- self .assertFalse ("test.test_import.data.lazy_imports.basic2" in sys .modules )
1278+ self .assertNotIn ("test.test_import.data.lazy_imports.basic2" , sys .modules )
12801279
12811280 # Access triggers reification
12821281 result = lazy_compat_from .get_x ()
12831282 self .assertEqual (result , 42 )
1284- self .assertTrue ("test.test_import.data.lazy_imports.basic2" in sys .modules )
1283+ self .assertIn ("test.test_import.data.lazy_imports.basic2" , sys .modules )
12851284
12861285
12871286class ImportStateAtReificationTests (unittest .TestCase ):
0 commit comments