Skip to content

Commit a8ae7a5

Browse files
bpo-45762: Improve docs for @singledispatch/@singledispatchmethod (GH-29426) (GH-29430)
(cherry picked from commit 71e8a3e) Co-authored-by: Alex Waygood <[email protected]>
1 parent 276a3a6 commit a8ae7a5

File tree

2 files changed

+28
-22
lines changed

2 files changed

+28
-22
lines changed

Doc/library/functools.rst

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -392,8 +392,8 @@ The :mod:`functools` module defines the following functions:
392392
dispatch>` :term:`generic function`.
393393

394394
To define a generic function, decorate it with the ``@singledispatch``
395-
decorator. Note that the dispatch happens on the type of the first argument,
396-
create your function accordingly::
395+
decorator. When defining a function using ``@singledispatch``, note that the
396+
dispatch happens on the type of the first argument::
397397

398398
>>> from functools import singledispatch
399399
>>> @singledispatch
@@ -403,9 +403,9 @@ The :mod:`functools` module defines the following functions:
403403
... print(arg)
404404

405405
To add overloaded implementations to the function, use the :func:`register`
406-
attribute of the generic function. It is a decorator. For functions
407-
annotated with types, the decorator will infer the type of the first
408-
argument automatically::
406+
attribute of the generic function, which can be used as a decorator. For
407+
functions annotated with types, the decorator will infer the type of the
408+
first argument automatically::
409409

410410
>>> @fun.register
411411
... def _(arg: int, verbose=False):
@@ -431,17 +431,17 @@ The :mod:`functools` module defines the following functions:
431431
...
432432

433433

434-
To enable registering lambdas and pre-existing functions, the
435-
:func:`register` attribute can be used in a functional form::
434+
To enable registering :term:`lambdas<lambda>` and pre-existing functions,
435+
the :func:`register` attribute can also be used in a functional form::
436436

437437
>>> def nothing(arg, verbose=False):
438438
... print("Nothing.")
439439
...
440440
>>> fun.register(type(None), nothing)
441441

442-
The :func:`register` attribute returns the undecorated function which
443-
enables decorator stacking, pickling, as well as creating unit tests for
444-
each variant independently::
442+
The :func:`register` attribute returns the undecorated function. This
443+
enables decorator stacking, :mod:`pickling<pickle>`, and the creation
444+
of unit tests for each variant independently::
445445

446446
>>> @fun.register(float)
447447
... @fun.register(Decimal)
@@ -476,11 +476,12 @@ The :mod:`functools` module defines the following functions:
476476
Where there is no registered implementation for a specific type, its
477477
method resolution order is used to find a more generic implementation.
478478
The original function decorated with ``@singledispatch`` is registered
479-
for the base ``object`` type, which means it is used if no better
479+
for the base :class:`object` type, which means it is used if no better
480480
implementation is found.
481481

482-
If an implementation registered to :term:`abstract base class`, virtual
483-
subclasses will be dispatched to that implementation::
482+
If an implementation is registered to an :term:`abstract base class`,
483+
virtual subclasses of the base class will be dispatched to that
484+
implementation::
484485

485486
>>> from collections.abc import Mapping
486487
>>> @fun.register
@@ -493,7 +494,7 @@ The :mod:`functools` module defines the following functions:
493494
>>> fun({"a": "b"})
494495
a => b
495496

496-
To check which implementation will the generic function choose for
497+
To check which implementation the generic function will choose for
497498
a given type, use the ``dispatch()`` attribute::
498499

499500
>>> fun.dispatch(float)
@@ -516,7 +517,7 @@ The :mod:`functools` module defines the following functions:
516517
.. versionadded:: 3.4
517518

518519
.. versionchanged:: 3.7
519-
The :func:`register` attribute supports using type annotations.
520+
The :func:`register` attribute now supports using type annotations.
520521

521522

522523
.. class:: singledispatchmethod(func)
@@ -525,8 +526,9 @@ The :mod:`functools` module defines the following functions:
525526
dispatch>` :term:`generic function`.
526527

527528
To define a generic method, decorate it with the ``@singledispatchmethod``
528-
decorator. Note that the dispatch happens on the type of the first non-self
529-
or non-cls argument, create your function accordingly::
529+
decorator. When defining a function using ``@singledispatchmethod``, note
530+
that the dispatch happens on the type of the first non-*self* or non-*cls*
531+
argument::
530532

531533
class Negator:
532534
@singledispatchmethod
@@ -542,9 +544,10 @@ The :mod:`functools` module defines the following functions:
542544
return not arg
543545

544546
``@singledispatchmethod`` supports nesting with other decorators such as
545-
``@classmethod``. Note that to allow for ``dispatcher.register``,
546-
``singledispatchmethod`` must be the *outer most* decorator. Here is the
547-
``Negator`` class with the ``neg`` methods being class bound::
547+
:func:`@classmethod<classmethod>`. Note that to allow for
548+
``dispatcher.register``, ``singledispatchmethod`` must be the *outer most*
549+
decorator. Here is the ``Negator`` class with the ``neg`` methods bound to
550+
the class, rather than an instance of the class::
548551

549552
class Negator:
550553
@singledispatchmethod
@@ -562,8 +565,9 @@ The :mod:`functools` module defines the following functions:
562565
def _(cls, arg: bool):
563566
return not arg
564567

565-
The same pattern can be used for other similar decorators: ``staticmethod``,
566-
``abstractmethod``, and others.
568+
The same pattern can be used for other similar decorators:
569+
:func:`@staticmethod<staticmethod>`,
570+
:func:`@abstractmethod<abc.abstractmethod>`, and others.
567571

568572
.. versionadded:: 3.8
569573

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Improve documentation for :func:`functools.singledispatch` and
2+
:class:`functools.singledispatchmethod`.

0 commit comments

Comments
 (0)