Skip to content

Fix monkeypatch.setattr/delattr binding descriptors with raising=False - #15017

Open
IMGillusion wants to merge 1 commit into
pytest-dev:mainfrom
IMGillusion:fix-10646-raising-false-descriptor
Open

Fix monkeypatch.setattr/delattr binding descriptors with raising=False#15017
IMGillusion wants to merge 1 commit into
pytest-dev:mainfrom
IMGillusion:fix-10646-raising-false-descriptor

Conversation

@IMGillusion

Copy link
Copy Markdown

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 #10646

Problem

monkeypatch.setattr/delattr with raising=False used getattr/hasattr to 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 with inspect.getattr_static, which reads the attribute without binding descriptors. raising=True keeps the previous behaviour (binding is intentional there).

Tests

Adds regression tests in testing/test_monkeypatch.py using a RaisingDescriptor whose __get__ raises, asserting the descriptor is bound with raising=True but not with raising=False, for both setattr and delattr (class and instance targets).

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
@psf-chronographer psf-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Sep 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MonkeyPatch.setattr/delattr cause descriptor binding even when the hasattr check is disabled with raising=False

1 participant