Skip to content

test(pt_expt): restore sys.modules snapshot in plugin entry-point test#5728

Queued
wanghan-iapcm wants to merge 2 commits into
deepmodeling:masterfrom
wanghan-iapcm:fix-test-plugin-sys-modules
Queued

test(pt_expt): restore sys.modules snapshot in plugin entry-point test#5728
wanghan-iapcm wants to merge 2 commits into
deepmodeling:masterfrom
wanghan-iapcm:fix-test-plugin-sys-modules

Conversation

@wanghan-iapcm

@wanghan-iapcm wanghan-iapcm commented Jul 4, 2026

Copy link
Copy Markdown
Collaborator

Summary

source/tests/pt_expt/test_plugin.py (from #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 #5714 and Test 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 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 #5714 (as 6422007) and #5717; whichever lands first, the others resolve trivially.

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.

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.
@dosubot dosubot Bot added the bug label Jul 4, 2026
@github-actions github-actions Bot added the Python label Jul 4, 2026
@coderabbitai

coderabbitai Bot commented Jul 4, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: b6443cc1-5642-42d4-8821-d99c3af88750

📥 Commits

Reviewing files that changed from the base of the PR and between 64c96dc and 69450a5.

📒 Files selected for processing (1)
  • source/tests/pt_expt/test_plugin.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • source/tests/pt_expt/test_plugin.py

📝 Walkthrough

Walkthrough

A test in source/tests/pt_expt/test_plugin.py was updated to snapshot and restore sys.modules entries for deepmd.pt_expt and its submodules around a re-import, and to keep the parent deepmd package attribute in sync afterward.

Changes

Module Cache Cleanup Fix

Layer / File(s) Summary
Sys.modules snapshot/restore around re-import
source/tests/pt_expt/test_plugin.py
Captures existing deepmd.pt_expt* module entries, reimports deepmd.pt_expt, removes entries created by that import, restores the prior snapshot, and updates deepmd.pt_expt on the parent package.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

  • deepmodeling/deepmd-kit#5559: Introduced the deepmd.pt_expt import-time entry-point loading behavior that this test cleanup logic supports.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the test fix and describes the main change accurately.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

📥 Commits

Reviewing files that changed from the base of the PR and between d279173 and 64c96dc.

📒 Files selected for processing (1)
  • source/tests/pt_expt/test_plugin.py

Comment thread source/tests/pt_expt/test_plugin.py Outdated
@codecov

codecov Bot commented Jul 4, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.15%. Comparing base (d279173) to head (69450a5).
⚠️ Report is 2 commits behind head on master.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@wanghan-iapcm wanghan-iapcm requested a review from njzjz July 4, 2026 07:52
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 njzjz-bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

@njzjz njzjz added this pull request to the merge queue Jul 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants