Fix monkeypatch undo of inherited attributes - #14944
Closed
arpansahu wants to merge 1 commit into
Closed
Conversation
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: arpan sahu <28574248+arpansahu@users.noreply.github.com>
Author
|
Disclosure: this contribution was prepared with AI assistance (GitHub Copilot CLI), and The defect, the fix and the regression test were verified locally: the new test fails on If your project would prefer not to take AI-assisted contributions, or you would rather this |
Member
|
Redo of preexisting pr in a much worse way |
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
When
MonkeyPatch.setattr()patches an inherited attribute on an instance, undo restores the inherited value into the instance__dict__. This leaves the object in a different state after cleanup; for descriptors, it also shadows future descriptor lookup. Closes #10644.Reproducer
Fix
For non-class targets, record
NOTSETwhen the patched name is not present in the instance dictionary.undo()then deletes the temporary instance attribute and exposes the inherited attribute again, matching the original object state.Testing
testing/test_monkeypatch.py::test_setattr_inherited_attribute_undo_restores_instance_dict.AssertionError: assert 'x' not in {'x': 1}.1 passed.python -m pytest testing/test_monkeypatch.py -q→36 passed, 1 skipped.python -m ruff check src/_pytest/monkeypatch.py testing/test_monkeypatch.py→All checks passed!.python -m ruff format --check src/_pytest/monkeypatch.py testing/test_monkeypatch.py→2 files already formatted.Full suite note:
python -m pytest testing/ -q --tb=short --maxfail=1reaches a pre-existing Windows failure intesting/acceptance_test.py::TestGeneralUsage::test_file_not_found_unconfigure_issue143(ExitCode.INTERNAL_ERRORvsExitCode.USAGE_ERROR) after1769 passed.Checklist:
closes #XYZWto the PR description and/or commits.Co-authored-bycommit trailers.changelogdirectory.AUTHORSin alphabetical order.