Discover hooks via inspect.getattr_static#701
Open
RonnyPfannschmidt wants to merge 13 commits into
Open
Conversation
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>
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.
Comment on lines
+68
to
+71
| if inspect.isfunction(static): | ||
| if inspect.isclass(obj) or inspect.ismodule(obj): | ||
| return static | ||
| return static.__get__(obj, type(obj)) |
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.
Summary
AttributeErrorspecial-casing with narrowinspect.getattr_staticdiscovery.classmethod,staticmethod, and bound methods are treated as hookable; properties/cached_property/other descriptors are skipped without executing them.__get__instead of discoverygetattr.@hookimplapplied above@classmethod/@staticmethod(previously missed when registering instances).Depends on #536 — this branch is stacked on that PR. Merge #536 first, then rebase this onto
main(or merge as-is after #536 lands).Test plan
uv run pytest(149 passed)uv run pre-commit run -a@hookimplabove classmethod/staticmethod testsMade with Cursor