@@ -5609,24 +5609,43 @@ def _make_syntax_error(text, offset, end_offset):
56095609class 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