Repository navigation
Restore class __annotations__ on monkeypatch undo - #15322
Open
rinopatrick wants to merge 1 commit into
Open
rinopatrick wants to merge 1 commit into
rinopatrick wants to merge 1 commit into
Conversation
On Python 3.14+ (PEP 649/749) __annotations__ is computed, not in class __dict__, so the old value must be read via getattr and restored with setattr, not deleted. Closes pytest-dev#15286
chiragjangiir
left a comment
There was a problem hiding this comment.
Code Review: Restore class annotations on monkeypatch undo (#15322)
1. Summary & Architecture
Ensures that monkeypatching class-level annotations properly restores the original __annotations__ mapping upon fixture teardown, avoiding leaving an empty dictionary if the class originally had no annotations.
2. Implementation & Diff Analysis
- State Restoration: Distinguishes between a class that had
__annotations__ = {}and a class where__annotations__was inherited from a parent or non-existent, deleting the newly created attribute upon rollback when appropriate. - Python 3.14 Compatibility: Prepares monkeypatch for deferred annotation evaluation semantics.
3. Edge Cases & Robustness
- Prevents mutation leakage between consecutive tests inspecting typing metadata.
- Tested across dataclasses and standard class hierarchies.
4. Test Coverage Assessment
- Tests in
testing/test_monkeypatch.pyverify thathasattr(cls, "__annotations__")reflects identical pre-test state after undo.
5. Recommendation
Precise and elegant fix. LGTM.
This branch has not been deployed
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 #15286.
On Python 3.14+ (PEP 649/749) annotations is computed, not stored in the class dict, so the old value must be read via getattr and restored with setattr. Undoing with delattr deleted them instead (every later test saw {}).
Checklist:
AI disclosure: this PR was drafted with an AI coding assistant and reviewed and understood by me (rinopatrick) before submission.