File tree 1 file changed +19
-0
lines changed
1 file changed +19
-0
lines changed Original file line number Diff line number Diff line change @@ -543,6 +543,25 @@ The :mod:`functools` module defines the following functions:
543
543
... print(arg.real, arg.imag)
544
544
...
545
545
546
+ For code that dispatches on a collections type (e.g., ``list ``), but wants
547
+ to typehint the items of the collection (e.g., ``list[int] ``), the
548
+ dispatch type should be passed explicitly to the decorator itself with the
549
+ typehint going into the function definition::
550
+
551
+ >>> @fun.register(list)
552
+ ... def _(arg: list[int], verbose=False):
553
+ ... if verbose:
554
+ ... print("Enumerate this:")
555
+ ... for i, elem in enumerate(arg):
556
+ ... print(i, elem)
557
+
558
+ .. note ::
559
+
560
+ At runtime the function will dispatch on an instance of a list regardless
561
+ of the type contained within the list i.e. ``[1,2,3] `` will be
562
+ dispatched the same as ``["foo", "bar", "baz"] ``. The annotation
563
+ provided in this example is for static type checkers only and has no
564
+ runtime impact.
546
565
547
566
To enable registering :term: `lambdas<lambda> ` and pre-existing functions,
548
567
the :func: `register ` attribute can also be used in a functional form::
You can’t perform that action at this time.
0 commit comments