Implement get_code in AssertionRewritingHook so runpy can re-run modules - #14945
Closed
ump45nose wants to merge 3 commits into
Closed
Implement get_code in AssertionRewritingHook so runpy can re-run modules#14945ump45nose wants to merge 3 commits into
ump45nose wants to merge 3 commits into
Conversation
runpy.run_module() calls the loader's get_code() method to obtain the
module's code. AssertionRewritingHook did not implement get_code(), so a
test module re-running itself via runpy crashed with:
AttributeError: 'AssertionRewritingHook' object has no attribute 'get_code'
Refactor the rewriting logic from exec_module() into a private
_get_rewritten_code() helper and implement get_code() to return the
rewritten code object for a module.
Fixes pytest-dev#9007.
Member
|
how does this interact with nested pytest sessions |
added 2 commits
August 27, 2026 21:57
- 优先复用已重写模块的真实文件路径,支持包内测试模块再次执行 - 覆盖嵌套 pytest 会话中外层重写钩子的生命周期 - 补齐未知模块返回路径并消除补丁覆盖率缺口
- 构造可解析但尚未执行的测试模块\n- 覆盖 get_code 的有效 spec origin 分支\n- 补齐 Codecov 报告的缺失行与短路分支
Member
|
unattended ai |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Calling
runpy.run_module()on a test module that pytest is assert-rewritingcrashes with:
runpyobtains a module's code through the loader'sget_code()method, butpytest's
AssertionRewritingHookonly implemented theexec_module()path usedby normal imports, so re-running a test module via
runpyfailed.Fix
Refactor the rewriting logic out of
exec_module()into a private_get_rewritten_code()helper, and implementget_code()onAssertionRewritingHookto return the rewritten code object for a module.Test
Added a regression test in
testing/test_assertrewrite.pythat re-runs a testmodule with
runpy.run_module()and asserts it passes.Fixes #9007