Discover hooks via inspect.getattr_static - #701
Merged
RonnyPfannschmidt merged 11 commits intoSep 12, 2026
Merged
RonnyPfannschmidt merged 11 commits into
RonnyPfannschmidt merged 11 commits into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refines Pluggy’s plugin hook discovery during PluginManager.register() and hookspec parsing by switching to inspect.getattr_static-based lookup to avoid triggering descriptors (e.g., @property, cached_property) while discovering @hookimpl/@hookspec markers. It also improves support for marker placement with @hookimpl stacked above @classmethod/@staticmethod, and binds callables via __get__ instead of using getattr() during discovery.
Changes:
- Add static (descriptor-safe) hook discovery helpers in
PluginManagerto skip properties/other descriptors without executing them. - Discover
@hookimplmarkers when applied above@classmethod/@staticmethodwrappers. - Add targeted regression tests ensuring registration does not evaluate properties/cached properties/raising descriptors and supports the new decorator ordering cases.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/pluggy/_manager.py |
Implements inspect.getattr_static-based marker discovery and callable binding to avoid executing descriptors during registration/spec parsing. |
testing/test_pluginmanager.py |
Adds tests covering descriptor-safety during registration and hookimpl discovery for @hookimpl above @classmethod/@staticmethod. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
updates: - [github.com/astral-sh/ruff-pre-commit: v0.6.7 → v0.6.8](astral-sh/ruff-pre-commit@v0.6.7...v0.6.8) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci
Co-authored-by: Ran Benita <ran@unusedvar.com>
Document the generic descriptor/proxy AttributeError skip and cover it with a minimal raising-descriptor plugin so the path is tested without pydantic. Co-authored-by: Cursor AI <ai@cursor.sh> Co-authored-by: Cursor Grok 4.5 <grok@x.ai>
Only treat functions, classmethods, staticmethods, and bound methods as hookable, so properties and other descriptors are never executed during registration. Also discovers @hookimpl applied above classmethod/staticmethod. Co-authored-by: Cursor AI <ai@cursor.sh> Co-authored-by: Cursor Grok 4.5 <grok@x.ai>
Co-authored-by: Cursor AI <ai@cursor.sh> Co-authored-by: Cursor Grok 4.5 <grok@x.ai>
ruff-check autofixes (unused List import, redundant return None) plus B018 silenced the same way as 095da32. Co-Authored-By: Claude Fable 5.1 <ai@anthropic.com> Co-Authored-By: Claude Code <ai@anthropic.com>
RonnyPfannschmidt
force-pushed
the
getattr-static-hook-discovery
branch
from
September 12, 2026 14:29
c494f95 to
21186ba
Compare
getattr() hands out values found in an instance __dict__ or a __slots__ slot as they are, without running the descriptor protocol. The getattr_static rewrite bound them anyway, so a hookimpl assigned in __init__ was called with the plugin as its first argument, and a hookimpl in a slot was dropped entirely because getattr_static returns the member descriptor rather than the stored function. Both regressions were silent: the first produced a wrong result, the second no hook call at all. _get_hookable and _hook_marker_holder duplicated the same type dispatch and could disagree; _get_hookable returned None on a path that was unreachable behind an assert. They are now one _static_hook_attr() returning both the marker holder and the callable, so the two can no longer drift and every branch is reachable. Also exercise the hook bodies in test_get_hookcallers_no_duplicates, which were the only uncovered lines left in the test suite. Co-Authored-By: Claude Opus 5 <ai@anthropic.com> Co-Authored-By: Claude Code <ai@anthropic.com>
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.
Written with AI assistance (Cursor, then Claude Code). I reviewed it and I am posting it.
Plugin registration used
getattr()to find hookimpls, which executes every property and descriptor on the object just to look for markers. That is a side effect nobody asked for, and it falls over on objects whose attribute access raises.This switches discovery to
inspect.getattr_static(). Only plain functions,classmethod,staticmethodand bound methods are considered hookable; properties,cached_propertyand other descriptors are skipped without being evaluated.Binding semantics
Discovery reproduces what
getattr()would have returned, rather than replacing it:__dict__or in a__slots__slot — is also returned unbound, becausegetattr()does not run the descriptor protocol on instance storage. A hookimpl assigned in__init__keeps its declared signature and gets no implicitself;classmethod/staticmethodare bound via__get__after the scan instead of during it.Thanks to @copilot for catching the instance-
__dict__case in review; the__slots__variant, which silently dropped the hook altogether, turned up while fixing it. Both are covered by tests now.Marker placement
One thing falls out of the static lookup:
@hookimplapplied above@classmethod/@staticmethodnow works. The marker sits on the wrapper, which the oldgetattr()path never saw, so those hooks were silently missed when registering an instance.Notes
Supersedes #536, which addressed the property case by special-casing
AttributeError. This branch is rebased onmainand stands on its own — #536 can be closed once this lands.src/pluggy/_manager.pyis at 100% statement and branch coverage with these tests. pytest's own test suite passes unchanged against this branch (4554 passed).🤖 Generated with Claude Code