@@ -18,7 +18,7 @@ This PEP introduces a concise and friendly syntax for callable types,
18
18
supporting the same functionality as ``typing.Callable `` but with an
19
19
arrow syntax inspired by the syntax for typed function
20
20
signatures. This allows types like ``Callable[[int, str], bool] `` to
21
- be written ``(int, str) -> bool ``.
21
+ be written as ``(int, str) -> bool ``.
22
22
23
23
The proposed syntax supports all the functionality provided by
24
24
``typing.Callable `` and ``typing.Concatenate ``, and is intended to
@@ -78,7 +78,7 @@ There are a few usability challenges with ``Callable`` we can see here:
78
78
- The bracket structure is not visually similar to how function signatures
79
79
are written.
80
80
- It requires an explicit import, unlike many of the other most common
81
- types like ``list ``.
81
+ types like ``list `` and `` dict `` .
82
82
83
83
Possibly as a result, `programmers often fail to write complete
84
84
Callable types
@@ -102,8 +102,8 @@ the benefits of static typing. For example, they might write this::
102
102
flat_map(add, [1, 2, 3]) # oops, no type check error!
103
103
104
104
There's some partial type information here - we at least know that ``func ``
105
- needs to be callable. But we've dropped too much type information to catch
106
- the mistake .
105
+ needs to be callable. But we've dropped too much type information for
106
+ type checkers to find the bug .
107
107
108
108
With our proposal, the example looks like this::
109
109
@@ -142,7 +142,7 @@ types, is more complicated to read and write, and still requires an
142
142
import and bracket-based syntax.
143
143
144
144
In this proposal, we chose to support all the existing semantics of
145
- ``typing.Callable ``, without adding support for new features. We took
145
+ ``typing.Callable ``, without adding support for new features. We made
146
146
this decision after examining how frequently each feature might be
147
147
used in existing typed and untyped open-source code. We determined
148
148
that the vast majority of use cases are covered.
@@ -296,8 +296,8 @@ It also optionally allows adding names to the arguments, for example::
296
296
297
297
(x: Int, y: String) -> Bool
298
298
299
- As in TypeScript, the argument names if provided are just there for documentation
300
- and are not part of the type itself.
299
+ As in TypeScript, the argument names ( if provided) are just there for
300
+ documentation and are not part of the type itself.
301
301
302
302
Scala
303
303
~~~~~
@@ -315,11 +315,11 @@ Function types can optionally include names, for example::
315
315
316
316
Unlike in TypeScript and Kotlin, these names are part of the type if
317
317
provided - any function implementing the type must use the same names.
318
- This is similar to the extended syntax proposal we described in our
318
+ This is similar to the extended syntax proposal we describe in our
319
319
`Rejected Alternatives `_ section.
320
320
321
- Function Definition vs Callable Type Annotations
322
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
321
+ Function Definitions vs Callable Type Annotations
322
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
323
323
324
324
In all of the languages listed above, type annotations for function
325
325
definitions use a ``: `` rather than a ``-> ``. For example, in TypeScript
@@ -542,7 +542,7 @@ Trailing Commas
542
542
~~~~~~~~~~~~~~~
543
543
544
544
- Following the precedent of function signatures, putting a comma in
545
- an empty arguments list is illegal, ``(,) -> bool `` is a syntax
545
+ an empty arguments list is illegal: ``(,) -> bool `` is a syntax
546
546
error.
547
547
- Again following precedent, trailing commas are otherwise always
548
548
permitted::
@@ -710,7 +710,7 @@ We decided against proposing it for the following reasons:
710
710
demonstrate that fewer than 3% of use cases would benefit from any
711
711
of the added features.
712
712
- The group that debated these proposals was split down the middle
713
- about whether these changes are even desirable:
713
+ about whether these changes are desirable:
714
714
715
715
- On the one hand, they make callable types more expressive. On the
716
716
other hand, they could easily confuse users who have not read the
@@ -723,8 +723,8 @@ We decided against proposing it for the following reasons:
723
723
- We intend to implement the current proposal in a way that is
724
724
forward-compatible with the more complicated extended syntax. If the
725
725
community decides after more experience and discussion that we want
726
- the additional features, they should be straightforward to propose
727
- in the future.
726
+ the additional features, it should be straightforward to propose
727
+ them in the future.
728
728
- Even a full extended syntax cannot replace the use of callback
729
729
protocols for overloads. For example, no closed form of callable type
730
730
could express a function that maps bools to bools and ints to floats,
@@ -752,7 +752,8 @@ We decided against proposing it for the following reasons:
752
752
We confirmed that the current proposal is forward-compatible with
753
753
extended syntax by
754
754
`implementing <https://github.com/stroxler/cpython/tree/callable-type-syntax--extended >`_
755
- a quick-and-dirty grammar and AST on top of this grammar and AST for.
755
+ a grammar and AST for this extended syntax on top of our reference
756
+ implementation of this PEP's grammar.
756
757
757
758
758
759
Syntax Closer to Function Signatures
@@ -778,11 +779,10 @@ The benefits of this proposal would have included:
778
779
779
780
Key downsides that led us to reject the idea include the following:
780
781
781
- - A large majority of use cases only use positional-only arguments,
782
- and this syntax would be more verbose for that use case, both
783
- because of requiring argument names and an explicit ``/ ``, for
784
- example ``(int, /) -> bool `` where our proposal allows ``(int) ->
785
- bool ``
782
+ - A large majority of use cases only use positional-only arguments. This
783
+ syntax would be more verbose for that use case, both because of requiring
784
+ argument names and an explicit ``/ ``, for example ``(int, /) -> bool `` where
785
+ our proposal allows ``(int) -> bool ``
786
786
- The requirement for explicit ``/ `` for positional-only arguments has
787
787
a high risk of causing frequent bugs - which often would not be
788
788
detected by unit tests - where library authors would accidentally
@@ -982,8 +982,7 @@ This actually is a significant readability improvement for
982
982
multi-argument functions, but the problem is that it makes callables
983
983
with one arguments, which are the most common arity, hard to
984
984
write: because ``(x) `` evaluates to ``x ``, they would have to be
985
- written like ``callable[(int,), bool] ``. This is awkward enough that
986
- we dislike this idea.
985
+ written like ``callable[(int,), bool] ``. We find this awkward.
987
986
988
987
Moreover, none of these ideas help as much with reducing verbosity
989
988
as the current proposal, nor do they introduce as strong a visual cue
@@ -1078,8 +1077,8 @@ types. However, we have since begun to do so, e.g. with PEP 604.
1078
1077
at the PyCon Typing Summit 2021.
1079
1078
1080
1079
**Steven ** `brought up this proposal on typing-sig
1081
- <https://mail.python.org/archives/list/[email protected] /thread/3JNXLYH5VFPBNIVKT6FFBVVFCZO4GFR2> `
. We
1082
- had several meetings to discuss alternatives, and `this presentation
1080
+ <https://mail.python.org/archives/list/[email protected] /thread/3JNXLYH5VFPBNIVKT6FFBVVFCZO4GFR2> `
_.
1081
+ We had several meetings to discuss alternatives, and `this presentation
1083
1082
<https://www.dropbox.com/s/sshgtr4p30cs0vc/Python%20Callable%20Syntax%20Proposals.pdf?dl=0> `_
1084
1083
led us to the current proposal.
1085
1084
0 commit comments