Skip to content

Commit 935b626

Browse files
committed
Improve docstring
1 parent 605e256 commit 935b626

1 file changed

Lines changed: 26 additions & 23 deletions

File tree

unpythonic/dispatch.py

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55
66
https://docs.python.org/3/library/functools.html#functools.singledispatch
77
8-
For now this is just a simplistic test. If we want to get serious, we must
9-
add support for things like `*args`, `**kwargs`, `typing.Sequence[int]`,
10-
and in 3.8, `typing.Literal['a', 'b', 'c', 'd']`.
11-
128
**WARNING: EXPERIMENTAL FEATURE**
139
1410
This experimental feature is a proof-of-concept provided for technical preview
@@ -43,41 +39,48 @@ def generic(f):
4339
are registered with the decorator `@f.register`, where `f` is the function
4440
you decorated with `@generic`.
4541
46-
Each method must specify type hints **on all of its parameters**. Then, at
47-
call time, the types of **all** arguments, as well as the number of arguments,
48-
are automatically used for choosing which method to call. In other words, multiple
49-
parameters are used for dispatching.
42+
Each method must specify type hints **on all of its parameters** except
43+
`**kwargs` (if it has one). Then, at call time, the types of **all** arguments
44+
(except any bound to `**kwargs`) as well as the number of arguments, are
45+
automatically used for choosing which method to call. In other words,
46+
multiple parameters are used for dispatching.
47+
48+
**Varargs are supported**. To have the contents of `*args` participate in
49+
dispatching, annotate the parameter as `*args: typing.Tuple[...]`. For the
50+
`...` part, see the documentation of the `typing` module. Both homogeneous
51+
and heterogeneous tuples are supported.
5052
51-
The first match wins, in most-recently-registered order - that is, later
52-
definitions override earlier ones. So specify the implementation with the
53-
most generic types first, and then move on to the more specific ones. The
54-
mnemonic is, "the function is generally defined like this, except if the
55-
arguments match these particular types..."
53+
The first method that matches wins, in most-recently-registered order. That
54+
is, later definitions override earlier ones. So specify the implementation
55+
with the most generic types first, and then move on to the more specific
56+
ones. The mnemonic is, "the function is generally defined like this, except
57+
if the arguments match these particular types..."
5658
5759
The point of this feature is to eliminate `if`/`elif`/`elif`... blocks
5860
that switch by `isinstance` on arguments, and then raise `TypeError`
5961
in the final `else`, by implementing the machinery once centrally.
6062
61-
Another use case are functions like the builtin `range` where the role
62-
of an argument in a particular position depends on the number of arguments
63-
given to the call.
63+
Another use case of `@generic` are functions like the builtin `range`, where
64+
the *role* of an argument in a particular position depends on the *number of*
65+
arguments passed in the call.
6466
6567
**Differences to tools in the standard library**:
6668
67-
Unlike `functools.singledispatch`, the `@generic` function itself is
68-
unused.
69+
Unlike `functools.singledispatch`, the `@generic` function itself is unused.
6970
70-
Unlike `typing.overload`, the implementations are to be provided in the
71-
method bodies.
71+
Unlike `typing.overload`, the implementations are given in the method bodies.
7272
7373
**CAUTION**:
7474
7575
To declare a parameter of a method as dynamically typed, explicitly
7676
annotate it as `typing.Any`; don't just omit the type annotation.
77-
Explicit is better than implicit; this is a feature.
77+
Explicit is better than implicit; **this is a feature**.
78+
79+
Dispatching by the contents of the `**kwargs` dictionary is not (yet)
80+
supported.
7881
79-
Currently, advanced features of `typing` such as `Sequence[...]` are
80-
not supported. This may or may not change in the future.
82+
See the limitations in `unpythonic.typecheck` for which features of the
83+
`typing` module are supported and which are not.
8184
"""
8285
# Dispatcher - this will replace the original f.
8386
@wraps(f)

0 commit comments

Comments
 (0)