Skip to content

Commit 1b5fd51

Browse files
committed
Rename _HookCaller -> HookCaller, export as pluggy.HookCaller
For typing purposes, refs #428. The old name `pluggy._hooks._HookCaller` is kept for backward compatibility, no reason to break the "offenders" who have imported it before.
1 parent d3e72b0 commit 1b5fd51

File tree

8 files changed

+51
-46
lines changed

8 files changed

+51
-46
lines changed

docs/api_reference.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ API Reference
1818
:show-inheritance:
1919
:members:
2020

21-
.. autoclass:: pluggy._hooks._HookCaller()
21+
.. autoclass:: pluggy.HookCaller()
2222
:members:
2323
:special-members: __call__
2424

@@ -31,6 +31,6 @@ API Reference
3131

3232
.. data:: <hook name>
3333

34-
:type: _HookCaller
34+
:type: HookCaller
3535

3636
The caller for the hook with the given name.

docs/index.rst

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ Also see the :ref:`pytest:firstresult` section in the ``pytest`` docs.
615615
Historic hooks
616616
^^^^^^^^^^^^^^
617617
You can mark a *hookspec* as being *historic* meaning that the hook
618-
can be called with :py:meth:`~pluggy._hooks._HookCaller.call_historic()` **before**
618+
can be called with :py:meth:`~pluggy.HookCaller.call_historic()` **before**
619619
having been registered:
620620

621621
.. code-block:: python
@@ -739,13 +739,13 @@ The core functionality of ``pluggy`` enables an extension provider
739739
to override function calls made at certain points throughout a program.
740740

741741
A particular *hook* is invoked by calling an instance of
742-
a :py:class:`pluggy._hooks._HookCaller` which in turn *loops* through the
742+
a :py:class:`pluggy.HookCaller` which in turn *loops* through the
743743
``1:N`` registered *hookimpls* and calls them in sequence.
744744

745745
Every :py:class:`~pluggy.PluginManager` has a ``hook`` attribute
746746
which is an instance of this :py:class:`pluggy._hooks._HookRelay`.
747747
The :py:class:`~pluggy._hooks._HookRelay` itself contains references
748-
(by hook name) to each registered *hookimpl*'s :py:class:`~pluggy._hooks._HookCaller` instance.
748+
(by hook name) to each registered *hookimpl*'s :py:class:`~pluggy.HookCaller` instance.
749749

750750
More practically you call a *hook* like so:
751751

@@ -761,7 +761,7 @@ More practically you call a *hook* like so:
761761
pm.add_hookspecs(mypluginspec)
762762
pm.register(myplugin)
763763
764-
# we invoke the _HookCaller and thus all underlying hookimpls
764+
# we invoke the HookCaller and thus all underlying hookimpls
765765
result_list = pm.hook.myhook(config=config, args=sys.argv)
766766
767767
Note that you **must** call hooks using keyword :std:term:`python:argument` syntax!
@@ -880,7 +880,7 @@ only useful if you expect that some *hookimpls* may be registered **after** the
880880
hook is initially invoked.
881881

882882
Historic hooks must be :ref:`specially marked <historic>` and called
883-
using the :py:meth:`~pluggy._hooks._HookCaller.call_historic()` method:
883+
using the :py:meth:`~pluggy.HookCaller.call_historic()` method:
884884

885885
.. code-block:: python
886886
@@ -901,8 +901,8 @@ using the :py:meth:`~pluggy._hooks._HookCaller.call_historic()` method:
901901
# historic callback is invoked here
902902
pm.register(mylateplugin)
903903
904-
Note that if you :py:meth:`~pluggy._hooks._HookCaller.call_historic()`
905-
the :py:class:`~pluggy._hooks._HookCaller` (and thus your calling code)
904+
Note that if you :py:meth:`~pluggy.HookCaller.call_historic()`
905+
the :py:class:`~pluggy.HookCaller` (and thus your calling code)
906906
can not receive results back from the underlying *hookimpl* functions.
907907
Instead you can provide a *callback* for processing results (like the
908908
``callback`` function above) which will be called as each new plugin
@@ -919,19 +919,19 @@ Calling with extras
919919
-------------------
920920
You can call a hook with temporarily participating *implementation* functions
921921
(that aren't in the registry) using the
922-
:py:meth:`pluggy._hooks._HookCaller.call_extra()` method.
922+
:py:meth:`pluggy.HookCaller.call_extra()` method.
923923

924924

925925
Calling with a subset of registered plugins
926926
-------------------------------------------
927927
You can make a call using a subset of plugins by asking the
928928
:py:class:`~pluggy.PluginManager` first for a
929-
:py:class:`~pluggy._hooks._HookCaller` with those plugins removed
929+
:py:class:`~pluggy.HookCaller` with those plugins removed
930930
using the :py:meth:`pluggy.PluginManager.subset_hook_caller()` method.
931931

932-
You then can use that :py:class:`_HookCaller <pluggy._hooks._HookCaller>`
933-
to make normal, :py:meth:`~pluggy._hooks._HookCaller.call_historic`, or
934-
:py:meth:`~pluggy._hooks._HookCaller.call_extra` calls as necessary.
932+
You then can use that :py:class:`~pluggy.HookCaller`
933+
to make normal, :py:meth:`~pluggy.HookCaller.call_historic`, or
934+
:py:meth:`~pluggy.HookCaller.call_extra` calls as necessary.
935935

936936

937937
.. _tracing:

src/pluggy/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
__all__ = [
99
"PluginManager",
1010
"PluginValidationError",
11+
"HookCaller",
1112
"HookCallError",
1213
"HookspecMarker",
1314
"HookimplMarker",
@@ -16,4 +17,4 @@
1617

1718
from ._manager import PluginManager, PluginValidationError
1819
from ._result import HookCallError, Result
19-
from ._hooks import HookspecMarker, HookimplMarker
20+
from ._hooks import HookspecMarker, HookimplMarker, HookCaller

src/pluggy/_callers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def _multicall(
3333
"""Execute a call into multiple python functions/methods and return the
3434
result(s).
3535
36-
``caller_kwargs`` comes from _HookCaller.__call__().
36+
``caller_kwargs`` comes from HookCaller.__call__().
3737
"""
3838
__tracebackhide__ = True
3939
results: list[object] = []

src/pluggy/_hooks.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -352,14 +352,14 @@ def __init__(self) -> None:
352352

353353
if TYPE_CHECKING:
354354

355-
def __getattr__(self, name: str) -> _HookCaller:
355+
def __getattr__(self, name: str) -> HookCaller:
356356
...
357357

358358

359359
_CallHistory = List[Tuple[Mapping[str, object], Optional[Callable[[Any], None]]]]
360360

361361

362-
class _HookCaller:
362+
class HookCaller:
363363
"""A caller of all registered implementations of a hook specification."""
364364

365365
__slots__ = (
@@ -446,7 +446,7 @@ def _add_hookimpl(self, hookimpl: HookImpl) -> None:
446446
self._hookimpls.insert(i + 1, hookimpl)
447447

448448
def __repr__(self) -> str:
449-
return f"<_HookCaller {self.name!r}>"
449+
return f"<HookCaller {self.name!r}>"
450450

451451
def _verify_all_args_are_provided(self, kwargs: Mapping[str, object]) -> None:
452452
# This is written to avoid expensive operations when not needed.
@@ -553,11 +553,15 @@ def _maybe_apply_history(self, method: HookImpl) -> None:
553553
result_callback(res[0])
554554

555555

556-
class _SubsetHookCaller(_HookCaller):
556+
# Historical name (pluggy<=1.2), kept for backward compatibility.
557+
_HookCaller = HookCaller
558+
559+
560+
class _SubsetHookCaller(HookCaller):
557561
"""A proxy to another HookCaller which manages calls to all registered
558562
plugins except the ones from remove_plugins."""
559563

560-
# This class is unusual: in inhertits from `_HookCaller` so all of
564+
# This class is unusual: in inhertits from `HookCaller` so all of
561565
# the *code* runs in the class, but it delegates all underlying *data*
562566
# to the original HookCaller.
563567
# `subset_hook_caller` used to be implemented by creating a full-fledged
@@ -572,7 +576,7 @@ class _SubsetHookCaller(_HookCaller):
572576
"_remove_plugins",
573577
)
574578

575-
def __init__(self, orig: _HookCaller, remove_plugins: AbstractSet[_Plugin]) -> None:
579+
def __init__(self, orig: HookCaller, remove_plugins: AbstractSet[_Plugin]) -> None:
576580
self._orig = orig
577581
self._remove_plugins = remove_plugins
578582
self.name = orig.name # type: ignore[misc]

src/pluggy/_manager.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414

1515
from . import _tracing
1616
from ._callers import _multicall
17-
from ._hooks import _HookCaller
1817
from ._hooks import _HookImplFunction
1918
from ._hooks import _HookImplOpts
2019
from ._hooks import _HookRelay
2120
from ._hooks import _HookSpecOpts
2221
from ._hooks import _Namespace
2322
from ._hooks import _Plugin
2423
from ._hooks import _SubsetHookCaller
24+
from ._hooks import HookCaller
2525
from ._hooks import HookImpl
2626
from ._hooks import HookSpec
2727
from ._hooks import normalize_hookimpl_opts
@@ -156,9 +156,9 @@ def register(self, plugin: _Plugin, name: str | None = None) -> str | None:
156156
method: _HookImplFunction[object] = getattr(plugin, name)
157157
hookimpl = HookImpl(plugin, plugin_name, method, hookimpl_opts)
158158
name = hookimpl_opts.get("specname") or name
159-
hook: _HookCaller | None = getattr(self.hook, name, None)
159+
hook: HookCaller | None = getattr(self.hook, name, None)
160160
if hook is None:
161-
hook = _HookCaller(name, self._hookexec)
161+
hook = HookCaller(name, self._hookexec)
162162
setattr(self.hook, name, hook)
163163
elif hook.has_spec():
164164
self._verify_hook(hook, hookimpl)
@@ -242,9 +242,9 @@ def add_hookspecs(self, module_or_class: _Namespace) -> None:
242242
for name in dir(module_or_class):
243243
spec_opts = self.parse_hookspec_opts(module_or_class, name)
244244
if spec_opts is not None:
245-
hc: _HookCaller | None = getattr(self.hook, name, None)
245+
hc: HookCaller | None = getattr(self.hook, name, None)
246246
if hc is None:
247-
hc = _HookCaller(name, self._hookexec, module_or_class, spec_opts)
247+
hc = HookCaller(name, self._hookexec, module_or_class, spec_opts)
248248
setattr(self.hook, name, hc)
249249
else:
250250
# Plugins registered this hook without knowing the spec.
@@ -311,7 +311,7 @@ def get_name(self, plugin: _Plugin) -> str | None:
311311
return name
312312
return None
313313

314-
def _verify_hook(self, hook: _HookCaller, hookimpl: HookImpl) -> None:
314+
def _verify_hook(self, hook: HookCaller, hookimpl: HookImpl) -> None:
315315
if hook.is_historic() and (hookimpl.hookwrapper or hookimpl.wrapper):
316316
raise PluginValidationError(
317317
hookimpl.plugin,
@@ -364,7 +364,7 @@ def check_pending(self) -> None:
364364
:exc:`PluginValidationError`."""
365365
for name in self.hook.__dict__:
366366
if name[0] != "_":
367-
hook: _HookCaller = getattr(self.hook, name)
367+
hook: HookCaller = getattr(self.hook, name)
368368
if not hook.has_spec():
369369
for hookimpl in hook.get_hookimpls():
370370
if not hookimpl.optionalhook:
@@ -411,7 +411,7 @@ def list_name_plugin(self) -> list[tuple[str, _Plugin]]:
411411
"""Return a list of (name, plugin) pairs for all registered plugins."""
412412
return list(self._name2plugin.items())
413413

414-
def get_hookcallers(self, plugin: _Plugin) -> list[_HookCaller] | None:
414+
def get_hookcallers(self, plugin: _Plugin) -> list[HookCaller] | None:
415415
"""Get all hook callers for the specified plugin.
416416
417417
:returns:
@@ -491,11 +491,11 @@ def after(
491491

492492
def subset_hook_caller(
493493
self, name: str, remove_plugins: Iterable[_Plugin]
494-
) -> _HookCaller:
495-
"""Return a proxy :py:class:`._hooks._HookCaller` instance for the named
494+
) -> HookCaller:
495+
"""Return a proxy :class:`~pluggy.HookCaller` instance for the named
496496
method which manages calls to all registered plugins except the ones
497497
from remove_plugins."""
498-
orig: _HookCaller = getattr(self.hook, name)
498+
orig: HookCaller = getattr(self.hook, name)
499499
plugins_to_remove = {plug for plug in remove_plugins if hasattr(plug, name)}
500500
if plugins_to_remove:
501501
return _SubsetHookCaller(orig, plugins_to_remove)

testing/test_hookcaller.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
from pluggy import HookspecMarker
1010
from pluggy import PluginManager
1111
from pluggy import PluginValidationError
12-
from pluggy._hooks import _HookCaller
12+
from pluggy._hooks import HookCaller
1313
from pluggy._hooks import HookImpl
1414

1515
hookspec = HookspecMarker("example")
1616
hookimpl = HookimplMarker("example")
1717

1818

1919
@pytest.fixture
20-
def hc(pm: PluginManager) -> _HookCaller:
20+
def hc(pm: PluginManager) -> HookCaller:
2121
class Hooks:
2222
@hookspec
2323
def he_method1(self, arg: object) -> None:
@@ -31,7 +31,7 @@ def he_method1(self, arg: object) -> None:
3131

3232

3333
class AddMeth:
34-
def __init__(self, hc: _HookCaller) -> None:
34+
def __init__(self, hc: HookCaller) -> None:
3535
self.hc = hc
3636

3737
def __call__(
@@ -57,15 +57,15 @@ def wrap(func: FuncT) -> FuncT:
5757

5858

5959
@pytest.fixture
60-
def addmeth(hc: _HookCaller) -> AddMeth:
60+
def addmeth(hc: HookCaller) -> AddMeth:
6161
return AddMeth(hc)
6262

6363

6464
def funcs(hookmethods: Sequence[HookImpl]) -> List[Callable[..., object]]:
6565
return [hookmethod.function for hookmethod in hookmethods]
6666

6767

68-
def test_adding_nonwrappers(hc: _HookCaller, addmeth: AddMeth) -> None:
68+
def test_adding_nonwrappers(hc: HookCaller, addmeth: AddMeth) -> None:
6969
@addmeth()
7070
def he_method1() -> None:
7171
pass
@@ -81,7 +81,7 @@ def he_method3() -> None:
8181
assert funcs(hc.get_hookimpls()) == [he_method1, he_method2, he_method3]
8282

8383

84-
def test_adding_nonwrappers_trylast(hc: _HookCaller, addmeth: AddMeth) -> None:
84+
def test_adding_nonwrappers_trylast(hc: HookCaller, addmeth: AddMeth) -> None:
8585
@addmeth()
8686
def he_method1_middle() -> None:
8787
pass
@@ -97,7 +97,7 @@ def he_method1_b() -> None:
9797
assert funcs(hc.get_hookimpls()) == [he_method1, he_method1_middle, he_method1_b]
9898

9999

100-
def test_adding_nonwrappers_trylast3(hc: _HookCaller, addmeth: AddMeth) -> None:
100+
def test_adding_nonwrappers_trylast3(hc: HookCaller, addmeth: AddMeth) -> None:
101101
@addmeth()
102102
def he_method1_a() -> None:
103103
pass
@@ -122,7 +122,7 @@ def he_method1_d() -> None:
122122
]
123123

124124

125-
def test_adding_nonwrappers_trylast2(hc: _HookCaller, addmeth: AddMeth) -> None:
125+
def test_adding_nonwrappers_trylast2(hc: HookCaller, addmeth: AddMeth) -> None:
126126
@addmeth()
127127
def he_method1_middle() -> None:
128128
pass
@@ -138,7 +138,7 @@ def he_method1() -> None:
138138
assert funcs(hc.get_hookimpls()) == [he_method1, he_method1_middle, he_method1_b]
139139

140140

141-
def test_adding_nonwrappers_tryfirst(hc: _HookCaller, addmeth: AddMeth) -> None:
141+
def test_adding_nonwrappers_tryfirst(hc: HookCaller, addmeth: AddMeth) -> None:
142142
@addmeth(tryfirst=True)
143143
def he_method1() -> None:
144144
pass
@@ -154,7 +154,7 @@ def he_method1_b() -> None:
154154
assert funcs(hc.get_hookimpls()) == [he_method1_middle, he_method1_b, he_method1]
155155

156156

157-
def test_adding_wrappers_ordering(hc: _HookCaller, addmeth: AddMeth) -> None:
157+
def test_adding_wrappers_ordering(hc: HookCaller, addmeth: AddMeth) -> None:
158158
@addmeth(hookwrapper=True)
159159
def he_method1():
160160
yield
@@ -184,7 +184,7 @@ def he_method3():
184184
]
185185

186186

187-
def test_adding_wrappers_ordering_tryfirst(hc: _HookCaller, addmeth: AddMeth) -> None:
187+
def test_adding_wrappers_ordering_tryfirst(hc: HookCaller, addmeth: AddMeth) -> None:
188188
@addmeth(hookwrapper=True, tryfirst=True)
189189
def he_method1():
190190
yield
@@ -200,7 +200,7 @@ def he_method3():
200200
assert funcs(hc.get_hookimpls()) == [he_method2, he_method1, he_method3]
201201

202202

203-
def test_adding_wrappers_complex(hc: _HookCaller, addmeth: AddMeth) -> None:
203+
def test_adding_wrappers_complex(hc: HookCaller, addmeth: AddMeth) -> None:
204204
assert funcs(hc.get_hookimpls()) == []
205205

206206
@addmeth(hookwrapper=True, trylast=True)

testing/test_pluginmanager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ def he_method1(self, arg):
276276

277277
@pytest.mark.parametrize("result_callback", [True, False])
278278
def test_with_result_memorized(pm: PluginManager, result_callback: bool) -> None:
279-
"""Verify that ``_HookCaller._maybe_apply_history()`
279+
"""Verify that ``HookCaller._maybe_apply_history()`
280280
correctly applies the ``result_callback`` function, when provided,
281281
to the result from calling each newly registered hook.
282282
"""

0 commit comments

Comments
 (0)