Repository navigation
fix: dynamic_context for @staticmethod and @classmethod tests (issue 1923) - #2297
Merged
Merged
Conversation
`qualname_from_frame()` found a test method through its `self` argument or through the module globals, so `@staticmethod` and `@classmethod` tests got no context under `dynamic_context = test_function`. Use the code object's `co_qualname` (Python 3.11+) when neither lookup works, but only for real function frames, so a class body with a test-like name (test_bug_829) is still ignored. Closes coveragepy#1923. Co-Authored-By: Claude Fable 5.1 <[email protected]>
Resolve the CHANGES.rst conflict by keeping both Unreleased entries. Co-Authored-By: Claude Fable 5.1 <[email protected]>
Member
|
Why do we need CO_OPTIMIZED? |
Contributor
Author
|
Thanks for the merge and the PYVERSIONS comment. On CO_OPTIMIZED: without it a module-level |
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.
Closes #1923.
qualname_from_frame()finds a test method through itsselfargument or, failing that, through the module globals. A@staticmethodor@classmethodtest satisfies neither, so it returnedNoneand the test ran under no context. With the issue's example onmain(08fbc32),CoverageData.measured_contexts()is['', 'test_foo.TestFoo.test_foo_instance']: the static and class method tests are missing.The fix falls back to the code object's
co_qualname(Python 3.11+), only for real function frames (CO_OPTIMIZED) so a class body with a test-like name still returnsNone(test_bug_829), and not for nested functions, which keep their old behavior. The same run then reports all three contexts. On 3.10 nothing changes and the new tests are skipped.Your
nedbat/bug1923-dynctx-staticmethodbranch returned the bareco_qualname; that is what failstest_bug_829, and it also drops the module prefix. This version keeps every existing test passing.Tests: two
QualnameTestcases and one end-to-endDynamicContextTestcase. All three fail onmainand pass with the fix. Fork CI on this SHA: Tests https://github.com/glaziermag/coveragepy/actions/runs/35817084448, Quality https://github.com/glaziermag/coveragepy/actions/runs/35817082251.Written with Claude Code (Fable 5.1); I ran the reproduction and tests above myself.