@@ -1554,6 +1554,66 @@ def test_lazy_import_before_eager_resolves_to_same_module(self):
15541554 self .assertEqual (result .returncode , 0 , f"stdout: { result .stdout } , stderr: { result .stderr } " )
15551555 self .assertIn ("OK" , result .stdout )
15561556
1557+ def test_reload_after_lazy_reification_keeps_single_module_object (self ):
1558+ """Reloading a reified lazy module should keep identity with globals/sys.modules."""
1559+ code = textwrap .dedent ("""
1560+ import importlib
1561+ import sys
1562+
1563+ lazy import json as lazy_json
1564+
1565+ # Trigger reification.
1566+ assert lazy_json.dumps({"x": 1}) == '{"x": 1}'
1567+ before_reload = lazy_json
1568+ assert before_reload is sys.modules["json"]
1569+
1570+ reloaded = importlib.reload(lazy_json)
1571+ assert reloaded is before_reload
1572+ assert lazy_json is sys.modules["json"]
1573+ print("OK")
1574+ """ )
1575+ result = subprocess .run (
1576+ [sys .executable , "-c" , code ],
1577+ capture_output = True ,
1578+ text = True
1579+ )
1580+ self .assertEqual (result .returncode , 0 , f"stdout: { result .stdout } , stderr: { result .stderr } " )
1581+ self .assertIn ("OK" , result .stdout )
1582+
1583+ def test_reimport_after_deleting_sys_modules_entry_creates_new_module (self ):
1584+ """Deleting sys.modules entry should force a fresh module on next import."""
1585+ code = textwrap .dedent ("""
1586+ import sys
1587+
1588+ lazy import json as lazy_json
1589+ assert "json" not in sys.modules
1590+
1591+ # Reify lazy binding.
1592+ assert lazy_json.dumps({"x": 1}) == '{"x": 1}'
1593+ first_obj = lazy_json
1594+ first_id = id(first_obj)
1595+ assert first_obj is sys.modules["json"]
1596+
1597+ # Remove import cache entry; existing binding should still work.
1598+ del sys.modules["json"]
1599+ assert "json" not in sys.modules
1600+ assert lazy_json.dumps({"x": 2}) == '{"x": 2}'
1601+
1602+ import json as second_obj
1603+ second_id = id(second_obj)
1604+ assert "json" in sys.modules
1605+ assert second_obj is sys.modules["json"]
1606+ assert first_id != second_id
1607+ print("OK")
1608+ """ )
1609+ result = subprocess .run (
1610+ [sys .executable , "-c" , code ],
1611+ capture_output = True ,
1612+ text = True
1613+ )
1614+ self .assertEqual (result .returncode , 0 , f"stdout: { result .stdout } , stderr: { result .stderr } " )
1615+ self .assertIn ("OK" , result .stdout )
1616+
15571617
15581618class RelativeImportTests (unittest .TestCase ):
15591619 """Tests for relative imports with lazy keyword."""
0 commit comments