Skip to content

Commit 279a7a0

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

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
@@ -402,8 +402,8 @@ The :mod:`functools` module defines the following functions:
402402
dispatch>` :term:`generic function`.
403403

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

408408
>>> from functools import singledispatch
409409
>>> @singledispatch
@@ -413,9 +413,9 @@ The :mod:`functools` module defines the following functions:
413413
... print(arg)
414414

415415
To add overloaded implementations to the function, use the :func:`register`
416-
attribute of the generic function. It is a decorator. For functions
417-
annotated with types, the decorator will infer the type of the first
418-
argument automatically::
416+
attribute of the generic function, which can be used as a decorator. For
417+
functions annotated with types, the decorator will infer the type of the
418+
first argument automatically::
419419

420420
>>> @fun.register
421421
... def _(arg: int, verbose=False):
@@ -441,17 +441,17 @@ The :mod:`functools` module defines the following functions:
441441
...
442442

443443

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

447447
>>> def nothing(arg, verbose=False):
448448
... print("Nothing.")
449449
...
450450
>>> fun.register(type(None), nothing)
451451

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

456456
>>> @fun.register(float)
457457
... @fun.register(Decimal)
@@ -486,11 +486,12 @@ The :mod:`functools` module defines the following functions:
486486
Where there is no registered implementation for a specific type, its
487487
method resolution order is used to find a more generic implementation.
488488
The original function decorated with ``@singledispatch`` is registered
489-
for the base ``object`` type, which means it is used if no better
489+
for the base :class:`object` type, which means it is used if no better
490490
implementation is found.
491491

492-
If an implementation registered to :term:`abstract base class`, virtual
493-
subclasses will be dispatched to that implementation::
492+
If an implementation is registered to an :term:`abstract base class`,
493+
virtual subclasses of the base class will be dispatched to that
494+
implementation::
494495

495496
>>> from collections.abc import Mapping
496497
>>> @fun.register
@@ -503,7 +504,7 @@ The :mod:`functools` module defines the following functions:
503504
>>> fun({"a": "b"})
504505
a => b
505506

506-
To check which implementation will the generic function choose for
507+
To check which implementation the generic function will choose for
507508
a given type, use the ``dispatch()`` attribute::
508509

509510
>>> fun.dispatch(float)
@@ -526,7 +527,7 @@ The :mod:`functools` module defines the following functions:
526527
.. versionadded:: 3.4
527528

528529
.. versionchanged:: 3.7
529-
The :func:`register` attribute supports using type annotations.
530+
The :func:`register` attribute now supports using type annotations.
530531

531532

532533
.. class:: singledispatchmethod(func)
@@ -535,8 +536,9 @@ The :mod:`functools` module defines the following functions:
535536
dispatch>` :term:`generic function`.
536537

537538
To define a generic method, decorate it with the ``@singledispatchmethod``
538-
decorator. Note that the dispatch happens on the type of the first non-self
539-
or non-cls argument, create your function accordingly::
539+
decorator. When defining a function using ``@singledispatchmethod``, note
540+
that the dispatch happens on the type of the first non-*self* or non-*cls*
541+
argument::
540542

541543
class Negator:
542544
@singledispatchmethod
@@ -552,9 +554,10 @@ The :mod:`functools` module defines the following functions:
552554
return not arg
553555

554556
``@singledispatchmethod`` supports nesting with other decorators such as
555-
``@classmethod``. Note that to allow for ``dispatcher.register``,
556-
``singledispatchmethod`` must be the *outer most* decorator. Here is the
557-
``Negator`` class with the ``neg`` methods being class bound::
557+
:func:`@classmethod<classmethod>`. Note that to allow for
558+
``dispatcher.register``, ``singledispatchmethod`` must be the *outer most*
559+
decorator. Here is the ``Negator`` class with the ``neg`` methods bound to
560+
the class, rather than an instance of the class::
558561

559562
class Negator:
560563
@singledispatchmethod
@@ -572,8 +575,9 @@ The :mod:`functools` module defines the following functions:
572575
def _(cls, arg: bool):
573576
return not arg
574577

575-
The same pattern can be used for other similar decorators: ``staticmethod``,
576-
``abstractmethod``, and others.
578+
The same pattern can be used for other similar decorators:
579+
:func:`@staticmethod<staticmethod>`,
580+
:func:`@abstractmethod<abc.abstractmethod>`, and others.
577581

578582
.. versionadded:: 3.8
579583

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)