Skip to content

Commit f9fa591

Browse files
Update lambda.rst: Typos, Jelle suggestions, and comment elaborations.
1 parent 3e237d3 commit f9fa591

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

docs/guides/lambda.rst

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,27 @@ error, but is uncaught in most (perhaps all) type checkers:
1818

1919
.. code-block:: python
2020
21-
f1 = lambda a, b: a * b
21+
f1 = lambda a, b: a + b
2222
f1(1, "a")
2323
2424
(The alternative way of writing this, ``(lambda a, b: a + b)(1, "a")``, is typically
2525
caught by type checkers, because it is simple and immediate enough that they are able
2626
to deduce that a type error will occur.)
2727

28+
..
29+
(This is an RST comment.)
30+
A slightly more realistic example of an uncaught lambda type error is
31+
32+
.. code-block :: python
33+
def apply(f, *x):
34+
f(*x)
35+
apply((lambda a, b: a + b), 1, "a")
36+
37+
since it doesn't immediately defeat the purpose of a lambda by binding it.
38+
It also fails to get caught by mypy and pyright in their default modes, as
39+
required for the example. However, it's a little bit harder to understand,
40+
so we went with the other one.
41+
2842
There are some workarounds to this problem, which all involve assigning the lambda to
2943
something, in one way or another, and annotating that. This is a bit unfortunate,
3044
because the idiomatic use of a lambda involves not doing that. In fact, at that point
@@ -37,14 +51,23 @@ the type of that variable with a Callable.
3751

3852
``f: Callable[[object], object] = lambda x: x``
3953

40-
Type comments on function definitions do not actually work on lambda, nor do
41-
normal type comments help (although you can use a type comment on an assignment
42-
to a variable with a lambda, of course; however this will have to be the Callable
43-
syntax and not the function-arrow special one).
54+
..
55+
(This is an RST comment. The following paragraph has been excised from the guide,
56+
as most beginners will not know what a type comment is anyway — especially a function
57+
type comment. However, the paragraph is left in this comment for greater context for
58+
you, the future editor:)
59+
60+
Type comments on function definitions do not actually work on lambda, nor do
61+
normal type comments help (although you can use a type comment on an assignment
62+
to a variable with a lambda, of course; however this will have to be the Callable
63+
syntax and not the function-arrow special one).
4464

4565
Most type checkers include an option to emit a warning if they aren't able to deduce
4666
the type of an expression; this should be helpful if you want to avoid silent uncaught
47-
type errors resulting from lambda expressions being deduced as ``Any``.
67+
type errors resulting from lambda expressions being deduced as ``Any``. For instance,
68+
Mypy includes ``disallow_any_expr``/``--disallow-any-expr`` and Pyright includes
69+
``reportUnknownLambdaType``. Both of those options are set to true in the respective
70+
strict modes of those type checkers.
4871

4972
In conclusion:
5073

0 commit comments

Comments
 (0)