fix(monkeypatch): don't leave inherited attrs in instance __dict__ after undo - #14962
Closed
mturac wants to merge 1 commit into
Closed
fix(monkeypatch): don't leave inherited attrs in instance __dict__ after undo#14962mturac wants to merge 1 commit into
mturac wants to merge 1 commit into
Conversation
…ter undo setattr recorded getattr(target, name) as the old value, which for inherited attributes returns the class-level value. undo() then called setattr(target, name, old_value), writing that value into the instance __dict__ and permanently shadowing the class attribute (including descriptors like properties). Now check vars(target) first — if the name isn't in the instance's own namespace, record NOTSET so undo() uses delattr instead. Fixes pytest-dev#10644
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.
fixes #10644
monkeypatch.setattron an inherited attribute records the value fromgetattr()— wich is the class-level value. on undo it doessetattr(target, name, old), writing that into the instance's own__dict__. after that the class attribute (including descriptors) is permanently shadowed.fix: check
vars(target)first. if the name isnt in the instance's own namespace, store the sentinel so undo usesdelattrinstead ofsetattr. same logic fordelattrpath.three regression tests:
ran full
testing/test_monkeypatch.py+testing/test_unittest.py: 109 passed, 10 skipped, 0 failed.