38
38
39
39
40
40
class _HookSpecOpts (TypedDict ):
41
+ """Options for a hook specification."""
42
+
43
+ #: Whether the hook is :ref:`first result only <firstresult>`.
41
44
firstresult : bool
45
+ #: Whether the hook is :ref:`historic <historic>`.
42
46
historic : bool
47
+ #: Whether the hook :ref:`warns when implemented <warn_on_impl>`.
43
48
warn_on_impl : Warning | None
44
49
45
50
46
51
class _HookImplOpts (TypedDict ):
52
+ """Options for a hook implementation."""
53
+
54
+ #: Whether the hook implementation is a :ref:`wrapper <hookwrapper>`.
47
55
wrapper : bool
56
+ #: Whether the hook implementation is an :ref:`old-style wrapper
57
+ #: <old_style_hookwrappers>`.
48
58
hookwrapper : bool
59
+ #: Whether validation against a hook specification is :ref:`optional
60
+ #: <optionalhook>`.
49
61
optionalhook : bool
62
+ #: Whether to try to order this hook implementation :ref:`first
63
+ #: <callorder>`.
50
64
tryfirst : bool
65
+ #: Whether to try to order this hook implementation :ref:`last
66
+ #: <callorder>`.
51
67
trylast : bool
68
+ #: The name of the hook specification to match, see :ref:`specname`.
52
69
specname : str | None
53
70
54
71
@@ -57,7 +74,7 @@ class HookspecMarker:
57
74
58
75
Instantiate it with a project_name to get a decorator.
59
76
Calling :meth:`PluginManager.add_hookspecs` later will discover all marked
60
- functions if the :class:`PluginManager` uses the same project_name .
77
+ functions if the :class:`PluginManager` uses the same project name .
61
78
"""
62
79
63
80
__slots__ = ("project_name" ,)
@@ -98,12 +115,18 @@ def __call__( # noqa: F811
98
115
If passed no function, returns a decorator which can be applied to a
99
116
function later using the attributes supplied.
100
117
101
- If ``firstresult`` is ``True``, the 1:N hook call (N being the number of
102
- registered hook implementation functions) will stop at I<=N when the
103
- I'th function returns a non-``None`` result.
118
+ :param firstresult:
119
+ If ``True``, the 1:N hook call (N being the number of registered
120
+ hook implementation functions) will stop at I<=N when the I'th
121
+ function returns a non-``None`` result. See :ref:`firstresult`.
104
122
105
- If ``historic`` is ``True``, every call to the hook will be memorized
106
- and replayed on plugins registered after the call was made.
123
+ :param historic:
124
+ If ``True``, every call to the hook will be memorized and replayed
125
+ on plugins registered after the call was made. See :ref:`historic`.
126
+
127
+ :param warn_on_impl:
128
+ If given, every implementation of this hook will trigger the given
129
+ warning. See :ref:`warn_on_impl`.
107
130
"""
108
131
109
132
def setattr_hookspec_opts (func : _F ) -> _F :
@@ -128,7 +151,7 @@ class HookimplMarker:
128
151
129
152
Instantiate it with a ``project_name`` to get a decorator.
130
153
Calling :meth:`PluginManager.register` later will discover all marked
131
- functions if the :class:`PluginManager` uses the same project_name .
154
+ functions if the :class:`PluginManager` uses the same project name .
132
155
"""
133
156
134
157
__slots__ = ("project_name" ,)
@@ -178,36 +201,46 @@ def __call__( # noqa: F811
178
201
If passed no function, returns a decorator which can be applied to a
179
202
function later using the attributes supplied.
180
203
181
- If ``optionalhook`` is ``True``, a missing matching hook specification
182
- will not result in an error (by default it is an error if no matching
183
- spec is found).
184
-
185
- If ``tryfirst`` is ``True``, this hook implementation will run as early
186
- as possible in the chain of N hook implementations for a specification.
187
-
188
- If ``trylast`` is ``True``, this hook implementation will run as late as
189
- possible in the chain of N hook implementations.
190
-
191
- If ``wrapper`` is ``True``("new-style hook wrapper"), the hook
192
- implementation needs to execute exactly one ``yield``. The code before
193
- the ``yield`` is run early before any non-hook-wrapper function is run.
194
- The code after the ``yield`` is run after all non-hook-wrapper functions
195
- have run. The ``yield`` receives the result value of the inner calls, or
196
- raises the exception of inner calls (including earlier hook wrapper
197
- calls). The return value of the function becomes the return value of the
198
- hook, and a raised exception becomes the exception of the hook.
199
-
200
- If ``hookwrapper`` is ``True`` ("old-style hook wrapper"), the hook
201
- implementation needs to execute exactly one ``yield``. The code before
202
- the ``yield`` is run early before any non-hook-wrapper function is run.
203
- The code after the ``yield`` is run after all non-hook-wrapper function
204
- have run The ``yield`` receives a :class:`_Result` object representing
205
- the exception or result outcome of the inner calls (including earlier
206
- hook wrapper calls). This option is mutually exclusive with ``wrapper``.
207
-
208
- If ``specname`` is provided, it will be used instead of the function
209
- name when matching this hook implementation to a hook specification
210
- during registration.
204
+ :param optionalhook:
205
+ If ``True``, a missing matching hook specification will not result
206
+ in an error (by default it is an error if no matching spec is
207
+ found). See :ref:`optionalhook`.
208
+
209
+ :param tryfirst:
210
+ If ``True``, this hook implementation will run as early as possible
211
+ in the chain of N hook implementations for a specification. See
212
+ :ref:`callorder`.
213
+
214
+ :param trylast:
215
+ If ``True``, this hook implementation will run as late as possible
216
+ in the chain of N hook implementations for a specification. See
217
+ :ref:`callorder`.
218
+
219
+ :param wrapper:
220
+ If ``True`` ("new-style hook wrapper"), the hook implementation
221
+ needs to execute exactly one ``yield``. The code before the
222
+ ``yield`` is run early before any non-hook-wrapper function is run.
223
+ The code after the ``yield`` is run after all non-hook-wrapper
224
+ functions have run. The ``yield`` receives the result value of the
225
+ inner calls, or raises the exception of inner calls (including
226
+ earlier hook wrapper calls). The return value of the function
227
+ becomes the return value of the hook, and a raised exception becomes
228
+ the exception of the hook. See :ref:`hookwrapper`.
229
+
230
+ :param hookwrapper:
231
+ If ``True`` ("old-style hook wrapper"), the hook implementation
232
+ needs to execute exactly one ``yield``. The code before the
233
+ ``yield`` is run early before any non-hook-wrapper function is run.
234
+ The code after the ``yield`` is run after all non-hook-wrapper
235
+ function have run The ``yield`` receives a :class:`_Result` object
236
+ representing the exception or result outcome of the inner calls
237
+ (including earlier hook wrapper calls). This option is mutually
238
+ exclusive with ``wrapper``. See :ref:`old_style_hookwrapper`.
239
+
240
+ :param specname:
241
+ If provided, the given name will be used instead of the function
242
+ name when matching this hook implementation to a hook specification
243
+ during registration. See :ref:`specname`.
211
244
212
245
.. versionadded:: 1.2.0
213
246
The ``wrapper`` parameter.
@@ -314,6 +347,9 @@ class _HookRelay:
314
347
315
348
__slots__ = ("__dict__" ,)
316
349
350
+ def __init__ (self ) -> None :
351
+ """:meta private:"""
352
+
317
353
if TYPE_CHECKING :
318
354
319
355
def __getattr__ (self , name : str ) -> _HookCaller :
@@ -324,6 +360,8 @@ def __getattr__(self, name: str) -> _HookCaller:
324
360
325
361
326
362
class _HookCaller :
363
+ """A caller of all registered implementations of a hook specification."""
364
+
327
365
__slots__ = (
328
366
"name" ,
329
367
"spec" ,
@@ -339,6 +377,7 @@ def __init__(
339
377
specmodule_or_class : _Namespace | None = None ,
340
378
spec_opts : _HookSpecOpts | None = None ,
341
379
) -> None :
380
+ """:meta private:"""
342
381
self .name : Final = name
343
382
self ._hookexec : Final = hook_execute
344
383
self ._hookimpls : Final [list [HookImpl ]] = []
@@ -348,9 +387,11 @@ def __init__(
348
387
assert spec_opts is not None
349
388
self .set_specification (specmodule_or_class , spec_opts )
350
389
390
+ # TODO: Document, or make private.
351
391
def has_spec (self ) -> bool :
352
392
return self .spec is not None
353
393
394
+ # TODO: Document, or make private.
354
395
def set_specification (
355
396
self ,
356
397
specmodule_or_class : _Namespace ,
@@ -366,6 +407,7 @@ def set_specification(
366
407
self ._call_history = []
367
408
368
409
def is_historic (self ) -> bool :
410
+ """Whether this caller is :ref:`historic <historic>`."""
369
411
return self ._call_history is not None
370
412
371
413
def _remove_plugin (self , plugin : _Plugin ) -> None :
@@ -376,6 +418,7 @@ def _remove_plugin(self, plugin: _Plugin) -> None:
376
418
raise ValueError (f"plugin { plugin !r} not found" )
377
419
378
420
def get_hookimpls (self ) -> list [HookImpl ]:
421
+ """Get all registered hook implementations for this hook."""
379
422
return self ._hookimpls .copy ()
380
423
381
424
def _add_hookimpl (self , hookimpl : HookImpl ) -> None :
@@ -424,6 +467,14 @@ def _verify_all_args_are_provided(self, kwargs: Mapping[str, object]) -> None:
424
467
break
425
468
426
469
def __call__ (self , ** kwargs : object ) -> Any :
470
+ """Call the hook.
471
+
472
+ Only accepts keyword arguments, which should match the hook
473
+ specification.
474
+
475
+ Returns the result(s) of calling all registered plugins, see
476
+ :ref:`calling`.
477
+ """
427
478
assert (
428
479
not self .is_historic ()
429
480
), "Cannot directly call a historic hook - use call_historic instead."
@@ -437,10 +488,12 @@ def call_historic(
437
488
kwargs : Mapping [str , object ] | None = None ,
438
489
) -> None :
439
490
"""Call the hook with given ``kwargs`` for all registered plugins and
440
- for all plugins which will be registered afterwards.
491
+ for all plugins which will be registered afterwards, see
492
+ :ref:`historic`.
441
493
442
- If ``result_callback`` is provided, it will be called for each
443
- non-``None`` result obtained from a hook implementation.
494
+ :param result_callback:
495
+ If provided, will be called for each non-``None`` result obtained
496
+ from a hook implementation.
444
497
"""
445
498
assert self ._call_history is not None
446
499
kwargs = kwargs or {}
@@ -459,7 +512,8 @@ def call_extra(
459
512
self , methods : Sequence [Callable [..., object ]], kwargs : Mapping [str , object ]
460
513
) -> Any :
461
514
"""Call the hook with some additional temporarily participating
462
- methods using the specified ``kwargs`` as call parameters."""
515
+ methods using the specified ``kwargs`` as call parameters, see
516
+ :ref:`call_extra`."""
463
517
assert (
464
518
not self .is_historic ()
465
519
), "Cannot directly call a historic hook - use call_historic instead."
0 commit comments