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

Loading
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