Skip to content

Commit b6aeb52

Browse files
committed
gh-152297: Update lazy import test expectations
1 parent 8f00fc9 commit b6aeb52

2 files changed

Lines changed: 42 additions & 21 deletions

File tree

Lib/test/test_lazy_import/__init__.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -755,14 +755,15 @@ class ErrorHandlingTests(LazyImportTestCase):
755755
"""
756756

757757
def test_import_error_shows_chained_traceback(self):
758-
"""Accessing a nonexistent lazy submodule via parent attr raises AttributeError."""
758+
"""A failed dotted lazy import should preserve the import failure."""
759759
code = textwrap.dedent("""
760760
import sys
761761
lazy import test.test_lazy_import.data.nonexistent_module
762762
763763
try:
764764
x = test.test_lazy_import.data.nonexistent_module
765-
except AttributeError as e:
765+
except ModuleNotFoundError as e:
766+
assert e.__cause__ is not None, "Expected chained exception"
766767
print("OK")
767768
""")
768769
result = subprocess.run(
@@ -807,20 +808,22 @@ def test_reification_retries_on_failure(self):
807808
808809
lazy import test.test_lazy_import.data.broken_module
809810
811+
def lazy_proxy():
812+
return globals()['test']
813+
810814
# First access - should fail
811815
try:
812816
x = test.test_lazy_import.data.broken_module
813-
except AttributeError:
817+
except ValueError:
814818
pass
815819
816-
# The lazy object should still be a lazy proxy (not reified)
817-
g = globals()
818-
lazy_obj = g['test']
819-
# The root 'test' binding should still allow retry
820+
assert type(lazy_proxy()) is types.LazyImportType
821+
820822
# Second access - should also fail (retry the import)
821823
try:
822824
x = test.test_lazy_import.data.broken_module
823-
except AttributeError:
825+
except ValueError:
826+
assert type(lazy_proxy()) is types.LazyImportType
824827
print("OK - retry worked")
825828
""")
826829
result = subprocess.run(
@@ -840,7 +843,8 @@ def test_error_during_module_execution_propagates(self):
840843
try:
841844
_ = test.test_lazy_import.data.broken_module
842845
print("FAIL - should have raised")
843-
except AttributeError:
846+
except ValueError as e:
847+
assert str(e) == "This module always fails to import"
844848
print("OK")
845849
""")
846850
result = subprocess.run(

Lib/test/test_traceback.py

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5609,24 +5609,43 @@ def _make_syntax_error(text, offset, end_offset):
56095609
class TestLazyImportSuggestions(unittest.TestCase):
56105610
"""Test that lazy imports are not reified when computing AttributeError suggestions."""
56115611

5612+
@staticmethod
5613+
def lazy_holder_script(body):
5614+
setup = textwrap.dedent("""
5615+
import atexit
5616+
import os
5617+
import shutil
5618+
import sys
5619+
import tempfile
5620+
5621+
tmpdir = tempfile.mkdtemp()
5622+
atexit.register(shutil.rmtree, tmpdir, ignore_errors=True)
5623+
with open(os.path.join(tmpdir, "lazy_traceback_bar.py"),
5624+
"w", encoding="utf-8") as f:
5625+
f.write('print("BAR_MODULE_LOADED")\\n')
5626+
with open(os.path.join(tmpdir, "lazy_holder.py"),
5627+
"w", encoding="utf-8") as f:
5628+
f.write("lazy import lazy_traceback_bar\\n")
5629+
5630+
sys.path.insert(0, tmpdir)
5631+
import lazy_holder
5632+
""")
5633+
return setup + textwrap.dedent(body)
5634+
56125635
def test_attribute_error_does_not_reify_lazy_imports(self):
56135636
"""Printing an AttributeError should not trigger lazy import reification."""
5614-
# pkg.bar prints "BAR_MODULE_LOADED" when imported.
5615-
# If lazy import is reified during suggestion computation, we'll see it.
5616-
code = textwrap.dedent("""
5617-
lazy import test.test_lazy_import.data.pkg.bar
5618-
test.test_lazy_import.data.pkg.nonexistent
5637+
code = self.lazy_holder_script("""
5638+
lazy_holder.nonexistent
56195639
""")
56205640
rc, stdout, stderr = assert_python_failure('-c', code)
56215641
self.assertNotIn(b"BAR_MODULE_LOADED", stdout)
56225642

56235643
def test_traceback_formatting_does_not_reify_lazy_imports(self):
56245644
"""Formatting a traceback should not trigger lazy import reification."""
5625-
code = textwrap.dedent("""
5645+
code = self.lazy_holder_script("""
56265646
import traceback
5627-
lazy import test.test_lazy_import.data.pkg.bar
56285647
try:
5629-
test.test_lazy_import.data.pkg.nonexistent
5648+
lazy_holder.nonexistent
56305649
except AttributeError:
56315650
traceback.format_exc()
56325651
print("OK")
@@ -5637,10 +5656,8 @@ def test_traceback_formatting_does_not_reify_lazy_imports(self):
56375656

56385657
def test_suggestion_still_works_for_non_lazy_attributes(self):
56395658
"""Suggestions should still work for non-lazy module attributes."""
5640-
code = textwrap.dedent("""
5641-
lazy import test.test_lazy_import.data.pkg.bar
5642-
# Typo for __name__
5643-
test.test_lazy_import.data.pkg.__nme__
5659+
code = self.lazy_holder_script("""
5660+
lazy_holder.__nme__
56445661
""")
56455662
rc, stdout, stderr = assert_python_failure('-c', code)
56465663
self.assertIn(b"__name__", stderr)

0 commit comments

Comments
 (0)