Skip to content

Commit ad73de0

Browse files
author
Han Wang
committed
test(pt_expt): restore sys.modules snapshot in plugin entry-point test
test_plugin popped deepmd.pt_expt from sys.modules without restoring it, leaving the package's cached submodules bound to a dead parent. Any later import of a cached submodule (e.g. deepmd.pt_expt.infer.deep_eval) re-created a BARE parent whose utils/infer attributes were never rebound, and mock.patch('deepmd.pt_expt.utils...') in test_deep_eval_serialize_api failed with AttributeError under py3.10's mock target resolution. Shard-order dependent: this PR's new test files reshuffled CI shard 4 and exposed the latent master-side hygiene bug. Snapshot the whole deepmd.pt_expt module tree before the re-import and restore it (including the parent-package attribute) afterwards.
1 parent 19f0cf1 commit ad73de0

1 file changed

Lines changed: 25 additions & 1 deletion

File tree

source/tests/pt_expt/test_plugin.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,36 @@ def fake_entry_points(*, group=None):
2222
return [_FakeEntryPoint(calls)]
2323

2424
monkeypatch.setattr(importlib.metadata, "entry_points", fake_entry_points)
25+
26+
# Snapshot the deepmd.pt_expt module tree BEFORE re-importing. Just popping
27+
# "deepmd.pt_expt" and leaving its submodules cached poisons sys.modules
28+
# for the rest of the pytest process: a later import of a cached submodule
29+
# (e.g. deepmd.pt_expt.infer.deep_eval) re-creates a BARE parent package
30+
# whose submodule attributes (utils/infer/...) are never rebound, and
31+
# mock.patch("deepmd.pt_expt.utils...") then fails with AttributeError on
32+
# py3.10 (shard-order dependent CI failure).
33+
saved = {
34+
k: v
35+
for k, v in sys.modules.items()
36+
if k == "deepmd.pt_expt" or k.startswith("deepmd.pt_expt.")
37+
}
38+
deepmd_pkg = sys.modules.get("deepmd")
2539
sys.modules.pop("deepmd.pt_expt", None)
2640

2741
try:
2842
importlib.import_module("deepmd.pt_expt")
2943
finally:
30-
sys.modules.pop("deepmd.pt_expt", None)
44+
# drop everything the fresh import created, then restore the snapshot
45+
# (including the parent-package attribute binding).
46+
for k in [
47+
m
48+
for m in list(sys.modules)
49+
if m == "deepmd.pt_expt" or m.startswith("deepmd.pt_expt.")
50+
]:
51+
sys.modules.pop(k, None)
52+
sys.modules.update(saved)
53+
if deepmd_pkg is not None and "deepmd.pt_expt" in saved:
54+
deepmd_pkg.pt_expt = saved["deepmd.pt_expt"]
3155

3256
assert groups == ["deepmd.pt_expt"]
3357
assert calls == ["load"]

0 commit comments

Comments
 (0)