Skip to content

Commit 79af872

Browse files
Add get_hookconfig method to HookimplMarker for type-safe config extraction
- Add HookimplMarker.get_hookconfig() method that returns HookimplConfiguration directly - Update test files to use new method instead of accessing dynamic attributes - Fix mypy type errors in test_multicall.py and test_hookcaller.py - Update CLAUDE.md to use 'uv run mypy src/' command 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 812b3c3 commit 79af872

File tree

5 files changed

+15
-8
lines changed

5 files changed

+15
-8
lines changed

.claude/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
],
88
"deny": []
99
}
10-
}
10+
}

CLAUDE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ It provides hook specification and implementation mechanisms through a plugin ma
2020
### Code Quality
2121
- `ruff check` - Run linting with Ruff
2222
- `ruff format` - Format code with Ruff
23-
- `mypy src/` - Type checking with mypy
23+
- `uv run mypy src/` - Type checking with mypy
2424
- `pre-commit run --all-files` - Run all pre-commit hooks
2525

2626
### Documentation

src/pluggy/_hooks.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,17 @@ def setattr_hookimpl_opts(func: _F) -> _F:
365365
else:
366366
return setattr_hookimpl_opts(function)
367367

368+
def get_hookconfig(self, func: Callable[..., object]) -> HookimplConfiguration:
369+
"""Extract hook implementation configuration from a decorated function.
370+
371+
:param func: A function decorated with this HookimplMarker
372+
:return: HookimplConfiguration object containing the hook implementation options
373+
:raises AttributeError: If the function is not decorated with this marker
374+
"""
375+
attr_name = self.project_name + "_impl"
376+
opts: HookimplOpts = getattr(func, attr_name)
377+
return HookimplConfiguration.from_opts(opts)
378+
368379

369380
def normalize_hookimpl_opts(opts: HookimplOpts) -> None:
370381
opts.setdefault("tryfirst", False)

testing/test_hookcaller.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from pluggy import PluginValidationError
1212
from pluggy._hooks import HookCaller
1313
from pluggy._hooks import HookImpl
14-
from pluggy._hooks import HookimplConfiguration
1514

1615

1716
hookspec = HookspecMarker("example")
@@ -55,7 +54,7 @@ def wrap(func: FuncT) -> FuncT:
5554
None,
5655
"<temp>",
5756
func,
58-
HookimplConfiguration.from_opts(func.example_impl),
57+
hookimpl.get_hookconfig(func),
5958
),
6059
)
6160
return func

testing/test_multicall.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from pluggy import HookspecMarker
1111
from pluggy._callers import _multicall
1212
from pluggy._hooks import HookImpl
13-
from pluggy._hooks import HookimplConfiguration
1413

1514

1615
hookspec = HookspecMarker("example")
@@ -25,9 +24,7 @@ def MC(
2524
caller = _multicall
2625
hookfuncs = []
2726
for method in methods:
28-
f = HookImpl(
29-
None, "<temp>", method, HookimplConfiguration.from_opts(method.example_impl)
30-
)
27+
f = HookImpl(None, "<temp>", method, hookimpl.get_hookconfig(method))
3128
hookfuncs.append(f)
3229
return caller("foo", hookfuncs, kwargs, firstresult)
3330

0 commit comments

Comments
 (0)