Commit 2b6cd22
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
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
50 | 50 | | |
51 | 51 | | |
52 | 52 | | |
53 | | - | |
54 | | - | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
55 | 61 | | |
56 | 62 | | |
57 | 63 | | |
0 commit comments