Skip to content

Discover hooks via inspect.getattr_static - #701

Merged
RonnyPfannschmidt merged 11 commits into
pytest-dev:mainfrom
RonnyPfannschmidt:getattr-static-hook-discovery
Sep 12, 2026
Merged

RonnyPfannschmidt merged 11 commits into
pytest-dev:mainfrom
RonnyPfannschmidt:getattr-static-hook-discovery

Conversation

@RonnyPfannschmidt

@RonnyPfannschmidt RonnyPfannschmidt commented Jul 21, 2026

Copy link
Copy Markdown
Member

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, staticmethod and bound methods are considered hookable; properties, cached_property and other descriptors are skipped without being evaluated.

Binding semantics

Discovery reproduces what getattr() would have returned, rather than replacing it:

  • a function found on the class or module is returned unbound, as before;
  • a function found on the instance — in its __dict__ or in a __slots__ slot — is also returned unbound, because getattr() does not run the descriptor protocol on instance storage. A hookimpl assigned in __init__ keeps its declared signature and gets no implicit self;
  • classmethod/staticmethod are 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: @hookimpl applied above @classmethod/@staticmethod now works. The marker sits on the wrapper, which the old getattr() 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 on main and stands on its own — #536 can be closed once this lands.

src/pluggy/_manager.py is 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

Copilot AI review requested due to automatic review settings July 21, 2026 08:27

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 PluginManager to skip properties/other descriptors without executing them.
  • Discover @hookimpl markers when applied above @classmethod/@staticmethod wrappers.
  • 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 thread src/pluggy/_manager.py Outdated
pirate and others added 10 commits September 12, 2026 16:28
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>
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>
@RonnyPfannschmidt
RonnyPfannschmidt merged commit bc9bcac into pytest-dev:main Sep 12, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants