Fix monkeypatch.setattr/delattr binding descriptors with raising=False - #15017
Open
IMGillusion wants to merge 1 commit into
Open
Fix monkeypatch.setattr/delattr binding descriptors with raising=False#15017IMGillusion wants to merge 1 commit into
IMGillusion wants to merge 1 commit into
Conversation
When raising=False, the attribute existence check now reads the attribute statically (inspect.getattr_static) instead of using getattr/hasattr, so descriptors are no longer bound during the check and their __get__ side effects do not fire. closes pytest-dev#10646
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.
When
raising=False, the attribute existence check now reads the attribute statically (inspect.getattr_static) instead of usinggetattr/hasattr, so descriptors are no longer bound during the check and their__get__side effects do not fire.closes #10646
Problem
monkeypatch.setattr/delattrwithraising=Falseusedgetattr/hasattrto probe whether the attribute exists. That binds any descriptor on the target (invoking its__get__), triggering__get__side effects purely as a side effect of the existence check.Fix
When
raising=False, probe the attribute statically withinspect.getattr_static, which reads the attribute without binding descriptors.raising=Truekeeps the previous behaviour (binding is intentional there).Tests
Adds regression tests in
testing/test_monkeypatch.pyusing aRaisingDescriptorwhose__get__raises, asserting the descriptor is bound withraising=Truebut not withraising=False, for bothsetattranddelattr(class and instance targets).