Skip to content

Commit 2b6cd22

Browse files
wanghan-iapcmHan Wang
andauthored
test(pt_expt): restore sys.modules snapshot in plugin entry-point test (deepmodeling#5728)
## Summary `source/tests/pt_expt/test_plugin.py` (from deepmodeling#5559) pops `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-creates a BARE parent package whose `utils`/`infer` attributes are never rebound, and `mock.patch("deepmd.pt_expt.utils...")` in `test_deep_eval_serialize_api.py` then fails with `AttributeError: module 'deepmd.pt_expt' has no attribute 'utils'` under py3.10's mock target resolution (py3.13 tolerates it). The failure is shard-order dependent: it needs (1) something to import `deepmd.pt_expt.infer.deep_eval` before `test_plugin` runs, and (2) the serialize-API test to run after — so it appears/disappears as PRs add test files and reshuffle the duration-based shards. It currently fails `Test Python (4, 3.10)` on deepmodeling#5714 and `Test Python (8, 3.10)` on deepmodeling#5717 while master stays green by ordering luck. ## Fix Snapshot the whole `deepmd.pt_expt.*` module tree before the re-import and restore it (including the `deepmd` parent-package attribute binding) in the `finally`. Verified: fixed test passes together with the serialize-API tests; an inline emulation of the worst-case ordering (child cached → pop/reimport/pop → `mock.patch` target resolution) resolves cleanly. Same one-file fix is cherry-picked on deepmodeling#5714 (as 6422007) and deepmodeling#5717; whichever lands first, the others resolve trivially. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Tests** * Improved import handling in the plugin loading test to better isolate module state between test runs. * Reduced the chance of leftover cached imports affecting later tests in the same session. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: Han Wang <wang_han@iapcm.ac.cn>
1 parent 1fa7f71 commit 2b6cd22

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

source/tests/pt_expt/test_plugin.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,14 @@ def fake_entry_points(*, group=None):
5050
]:
5151
sys.modules.pop(k, None)
5252
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"]
53+
if deepmd_pkg is not None:
54+
if "deepmd.pt_expt" in saved:
55+
deepmd_pkg.pt_expt = saved["deepmd.pt_expt"]
56+
elif hasattr(deepmd_pkg, "pt_expt"):
57+
# deepmd.pt_expt was not imported before this test; the fresh
58+
# import bound it onto the parent package, so drop that stale
59+
# attribute to leave the parent exactly as we found it.
60+
delattr(deepmd_pkg, "pt_expt")
5561

5662
assert groups == ["deepmd.pt_expt"]
5763
assert calls == ["load"]

0 commit comments

Comments
 (0)