test(pt_expt): restore sys.modules snapshot in plugin entry-point test#5728
test(pt_expt): restore sys.modules snapshot in plugin entry-point test#5728wanghan-iapcm wants to merge 2 commits into
Conversation
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.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughA test in ChangesModule Cache Cleanup Fix
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@source/tests/pt_expt/test_plugin.py`:
- Around line 38-54: The import cleanup in the pt_expt plugin test leaves a
stale deepmd.pt_expt attribute on the deepmd package when that parent binding
was not part of the saved snapshot. Update the cleanup logic around
importlib.import_module("deepmd.pt_expt") and the finally block so that
deepmd_pkg.pt_expt is removed when "deepmd.pt_expt" was not originally present
in saved, while still restoring the attribute from saved when it was present.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: e632fa93-28a2-4f7a-b5d5-b8b8249f2ca6
📒 Files selected for processing (1)
source/tests/pt_expt/test_plugin.py
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #5728 +/- ##
==========================================
- Coverage 81.26% 81.15% -0.12%
==========================================
Files 988 988
Lines 110885 110892 +7
Branches 4232 4234 +2
==========================================
- Hits 90115 89996 -119
- Misses 19245 19372 +127
+ Partials 1525 1524 -1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
The finally block only rebound deepmd_pkg.pt_expt when deepmd.pt_expt was in the saved snapshot. When the test runs before anything imports deepmd.pt_expt (shard-order dependent), the fresh import_module still binds deepmd_pkg.pt_expt, and popping the module from sys.modules left that attribute dangling on the parent package — the same leak class this test set out to prevent. Delete the attribute in the not-in-saved case so the parent is restored exactly as found.
njzjz-bot
left a comment
There was a problem hiding this comment.
Reviewed this PR after the follow-up commit. The cleanup now snapshots/restores the whole deepmd.pt_expt module tree and also fixes the parent-package binding leak when deepmd.pt_expt was not originally imported. That addresses the shard-order-dependent failure mode without broadening the test scope.
CI is green, and I do not see remaining blockers.
Authored by OpenClaw 2026.6.8 (844f405) (model: custom-chat-jinzhezeng-group/gpt-5.5)
Summary
source/tests/pt_expt/test_plugin.py(from #5559) popsdeepmd.pt_exptfromsys.moduleswithout 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 whoseutils/inferattributes are never rebound, andmock.patch("deepmd.pt_expt.utils...")intest_deep_eval_serialize_api.pythen fails withAttributeError: 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_evalbeforetest_pluginruns, 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 failsTest Python (4, 3.10)on #5714 andTest Python (8, 3.10)on #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 thedeepmdparent-package attribute binding) in thefinally.Verified: fixed test passes together with the serialize-API tests; an inline emulation of the worst-case ordering (child cached → pop/reimport/pop →
mock.patchtarget resolution) resolves cleanly.Same one-file fix is cherry-picked on #5714 (as 6422007) and #5717; whichever lands first, the others resolve trivially.
Summary by CodeRabbit