@@ -346,27 +346,32 @@ def _add_hookimpl(self, hookimpl: "HookImpl") -> None:
346
346
def __repr__ (self ) -> str :
347
347
return f"<_HookCaller { self .name !r} >"
348
348
349
- def __call__ (self , * args : object , ** kwargs : object ) -> Any :
350
- if args :
351
- raise TypeError ("hook calling supports only keyword arguments" )
352
- assert not self .is_historic ()
353
-
349
+ def _verify_all_args_are_provided (self , kwargs : Mapping [str , object ]) -> None :
354
350
# This is written to avoid expensive operations when not needed.
355
351
if self .spec :
356
352
for argname in self .spec .argnames :
357
353
if argname not in kwargs :
358
- notincall = tuple (set (self .spec .argnames ) - kwargs .keys ())
354
+ notincall = ", " .join (
355
+ repr (argname )
356
+ for argname in self .spec .argnames
357
+ # Avoid self.spec.argnames - kwargs.keys() - doesn't preserve order.
358
+ if argname not in kwargs .keys ()
359
+ )
359
360
warnings .warn (
360
361
"Argument(s) {} which are declared in the hookspec "
361
- "can not be found in this hook call" .format (notincall ),
362
+ "cannot be found in this hook call" .format (notincall ),
362
363
stacklevel = 2 ,
363
364
)
364
365
break
365
366
366
- firstresult = self .spec .opts .get ("firstresult" , False )
367
- else :
368
- firstresult = False
369
-
367
+ def __call__ (self , * args : object , ** kwargs : object ) -> Any :
368
+ if args :
369
+ raise TypeError ("hook calling supports only keyword arguments" )
370
+ assert (
371
+ not self .is_historic ()
372
+ ), "Cannot directly call a historic hook - use call_historic instead."
373
+ self ._verify_all_args_are_provided (kwargs )
374
+ firstresult = self .spec .opts .get ("firstresult" , False ) if self .spec else False
370
375
return self ._hookexec (self .name , self .get_hookimpls (), kwargs , firstresult )
371
376
372
377
def call_historic (
@@ -382,6 +387,7 @@ def call_historic(
382
387
"""
383
388
assert self ._call_history is not None
384
389
kwargs = kwargs or {}
390
+ self ._verify_all_args_are_provided (kwargs )
385
391
self ._call_history .append ((kwargs , result_callback ))
386
392
# Historizing hooks don't return results.
387
393
# Remember firstresult isn't compatible with historic.
0 commit comments