Avoid unnecessary descriptor reads with raising=False - #15091
Open
houtinghang wants to merge 5 commits into
Open
houtinghang wants to merge 5 commits into
houtinghang wants to merge 5 commits into
Conversation
Read class and non-data descriptors statically before patching so their __get__ methods are not invoked merely to save the old attribute. Preserve dynamic lookup and instance data descriptor values for undo. Refs pytest-dev#10646 Co-authored-by: OpenAI Codex <noreply@openai.com>
Validate the descriptors and dynamic fallback directly before asserting that monkeypatch does not execute them. This covers the test code itself without excluding meaningful branches from coverage. Co-authored-by: OpenAI Codex <noreply@openai.com>
Preserve the warning while avoiding an unrelated Hypothesis failure on NumPy 2.4.6 and PyPy 8.0.0. Co-authored-by: OpenAI Codex <noreply@openai.com>
Reproduce with NumPy loaded before Hypothesis: the warning is attributed to importlib, so matching numpy.random misses it. Retain the exact warning message and category. Co-authored-by: OpenAI Codex <noreply@openai.com>
MateehUllah
approved these changes
Sep 24, 2026
MateehUllah
left a comment
There was a problem hiding this comment.
Reviewed the descriptor lookup changes and regression coverage. Using static lookup for class and non-data descriptors when raising=False avoids invoking descriptor code merely to preserve undo state, while instance data descriptors still use normal lookup because their current value must be restored through the setter. The fallback for dynamically provided attributes preserves getattr behavior, and the MRO-based data-descriptor check correctly handles inherited descriptors. The tests cover class descriptors, instance non-data descriptors, properties, slots, inherited attributes, dynamic attributes, both operations, and undo behavior. I did not find a blocking correctness issue.
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.
With
raising=False, patching a class descriptor currently calls its__get__once insetattrand twice indelattr. A descriptor that raises can prevent the patch altogether. This change uses static lookup for class and non-data descriptors, while keeping normal lookup for instance data descriptors whose values are needed byundo().Refs #10646.
The change keeps
raising=Truebehavior and falls back to dynamic lookup for attributes supplied by__getattr__. It does not promise side-effect-free access to instance properties or slots. Regression tests cover descriptor side effects and exceptions, restoration, inherited class attributes, dynamic attributes, properties, and slots.Validation on Windows / Python 3.13.9:
Full-suite command (UTF-8 mode and the virtualenv Scripts directory on PATH):
The first local runs exposed subprocess encoding errors and a long-path bytecode-cache failure. UTF-8 mode removed the encoding errors; a short temporary path resolved the cache test. The unmodified base also failed that cache test with the long path. The XPASS is the existing
test_make_numbered_dir_multiprocess_safecase (#11603).AI assistance: OpenAI Codex helped prepare the implementation, tests, and this description under my direction; it is credited in the commit. The upstream CI matrix passed on commit 9cdb28e.
CI follow-up:
test_idval_hypothesiswhile importingnumpy.random; the same failure reproduces with standaloneimport numpy.randomunder-W error, without pytest installed (PyPy 8.0.0 / Python 3.11.16, NumPy 2.4.6).Follow-up correction: the first PyPy focused run did not preload NumPy and therefore missed the failing Hypothesis path. A fresh-process reproduction which imports NumPy before running
test_idval_hypothesisfails with the original filter. Cython attributes this warning to<frozen importlib._bootstrap>, so the NumPy module restriction was removed while retaining the specific message and RuntimeWarning category. The same reproduction now passes with the warning displayed (1 passed, 1 warning).Final CI validation on
9cdb28e0e405ad900355159ceffddf50050c1a08: all 36 PR checks passed, including the finalcheckjob, the full OS/Python matrix, and Codecov patch coverage (100%). The PyPy full suite reports 4601 passed, 102 skipped, 12 xfailed, and 1 xpassed. Run: https://github.com/pytest-dev/pytest/actions/runs/35881114712