1515import sys
1616import types
1717from contextlib import contextmanager
18+ from importlib import import_module
1819
1920import pytest
2021import torch
@@ -30,8 +31,10 @@ def install_fake_module(monkeypatch, name, module):
3031 if idx == len (parts ):
3132 current_module = module
3233 else :
33- current_module = sys .modules .get (full_name , types .ModuleType (full_name ))
34- current_module .__path__ = []
34+ current_module = sys .modules .get (full_name )
35+ if current_module is None :
36+ current_module = types .ModuleType (full_name )
37+ current_module .__path__ = []
3538 monkeypatch .setitem (sys .modules , full_name , current_module )
3639
3740 for idx in range (1 , len (parts )):
@@ -41,6 +44,22 @@ def install_fake_module(monkeypatch, name, module):
4144 monkeypatch .setattr (sys .modules [parent_name ], child_name , sys .modules [child_full_name ], raising = False )
4245
4346
47+ def test_install_fake_module_preserves_real_package_paths ():
48+ nemo_automodel = pytest .importorskip ("nemo_automodel" )
49+ original_path = list (nemo_automodel .__path__ )
50+
51+ with pytest .MonkeyPatch .context () as monkeypatch :
52+ fake_te_patches = types .ModuleType ("nemo_automodel.shared.te_patches" )
53+ fake_te_patches .apply_te_patches = lambda : None
54+ install_fake_module (monkeypatch , "nemo_automodel.shared.te_patches" , fake_te_patches )
55+
56+ assert list (nemo_automodel .__path__ ) == original_path
57+ import_module ("nemo_automodel.components.distributed.config" )
58+
59+ assert list (nemo_automodel .__path__ ) == original_path
60+ import_module ("nemo_automodel.components.distributed.config" )
61+
62+
4463@contextmanager
4564def recording_context (events , name ):
4665 events .append (f"{ name } :enter" )
0 commit comments