@@ -188,6 +188,130 @@ using bidirectional type inference:
188
188
If you want to give the argument or return value types explicitly, use
189
189
an ordinary, perhaps nested function definition.
190
190
191
+ Extended Callable types
192
+ ***********************
193
+
194
+ As an experimental mypy extension, you can specify ``Callable `` types
195
+ that support keyword arguments, optional arguments, and more. Where
196
+ you specify the arguments of a Callable, you can choose to supply just
197
+ the type of a nameless positional argument, or an "argument specifier"
198
+ representing a more complicated form of argument. This allows one to
199
+ more closely emulate the full range of possibilities given by the
200
+ ``def `` statement in Python.
201
+
202
+ As an example, here's a complicated function definition and the
203
+ corresponding ``Callable ``:
204
+
205
+ .. code-block :: python
206
+
207
+ from typing import Callable
208
+ from mypy_extensions import (Arg, DefaultArg, NamedArg,
209
+ DefaultNamedArg, VarArg, KwArg)
210
+
211
+ def func (__a : int , # This convention is for nameless arguments
212
+ b : int ,
213
+ c : int = 0 ,
214
+ * args : int ,
215
+ d : int ,
216
+ e : int = 0 ,
217
+ ** kwargs : int ) -> int :
218
+ ...
219
+
220
+ F = Callable[[int , # Or Arg(int)
221
+ Arg(int , ' b' ),
222
+ DefaultArg(int , ' c' ),
223
+ VarArg(int ),
224
+ NamedArg(int , ' d' ),
225
+ DefaultNamedArg(int , ' e' ),
226
+ KwArg(int )],
227
+ int ]
228
+
229
+ f: F = func
230
+
231
+ Argument specifiers are special function calls that can specify the
232
+ following aspects of an argument:
233
+
234
+ - its type (the only thing that the basic format supports)
235
+
236
+ - its name (if it has one)
237
+
238
+ - whether it may be omitted
239
+
240
+ - whether it may or must be passed using a keyword
241
+
242
+ - whether it is a ``*args `` argument (representing the remaining
243
+ positional arguments)
244
+
245
+ - whether it is a ``**kwargs `` argument (representing the remaining
246
+ keyword arguments)
247
+
248
+ The following functions are available in ``mypy_extensions `` for this
249
+ purpose:
250
+
251
+ .. code-block :: python
252
+
253
+ def Arg (type = Any, name = None ):
254
+ # A normal, mandatory, positional argument.
255
+ # If the name is specified it may be passed as a keyword.
256
+
257
+ def DefaultArg (type = Any, name = None ):
258
+ # An optional positional argument (i.e. with a default value).
259
+ # If the name is specified it may be passed as a keyword.
260
+
261
+ def NamedArg (type = Any, name = None ):
262
+ # A mandatory keyword-only argument.
263
+
264
+ def DefaultNamedArg (type = Any, name = None ):
265
+ # An optional keyword-only argument (i.e. with a default value).
266
+
267
+ def VarArg (type = Any):
268
+ # A *args-style variadic positional argument.
269
+ # A single VarArg() specifier represents all remaining
270
+ # positional arguments.
271
+
272
+ def KwArg (type = Any):
273
+ # A **kwargs-style variadic keyword argument.
274
+ # A single KwArg() specifier represents all remaining
275
+ # keyword arguments.
276
+
277
+ In all cases, the ``type `` argument defaults to ``Any ``, and if the
278
+ ``name `` argument is omitted the argument has no name (the name is
279
+ required for ``NamedArg `` and ``DefaultNamedArg ``). A basic
280
+ ``Callable `` such as
281
+
282
+ .. code-block :: python
283
+
284
+ MyFunc = Callable[[int , str , int ], float ]
285
+
286
+ is equivalent to the following:
287
+
288
+ .. code-block :: python
289
+
290
+ MyFunc = Callable[[Arg(int ), Arg(str ), Arg(int )], float ]
291
+
292
+ A ``Callable `` with unspecified argument types, such as
293
+
294
+ .. code-block :: python
295
+
296
+ MyOtherFunc = Callable[... , int ]
297
+
298
+ is (roughly) equivalent to
299
+
300
+ .. code-block :: python
301
+
302
+ MyOtherFunc = Callable[[VarArg(), KwArg()], int ]
303
+
304
+ .. note ::
305
+
306
+ This feature is experimental. Details of the implementation may
307
+ change and there may be unknown limitations. **IMPORTANT: **
308
+ Each of the functions above currently just returns its ``type ``
309
+ argument, so the information contained in the argument specifiers
310
+ is not available at runtime. This limitation is necessary for
311
+ backwards compatibility with the existing ``typing.py `` module as
312
+ present in the Python 3.5+ standard library and distributed via
313
+ PyPI.
314
+
191
315
.. _union-types :
192
316
193
317
Union types
0 commit comments