@@ -35,7 +35,7 @@ Callable
35
35
--------
36
36
37
37
Frameworks expecting callback functions of specific signatures might be
38
- type hinted using `Callable[[Arg1Type, Arg2Type], ReturnType] `.
38
+ type hinted using `` Callable[[Arg1Type, Arg2Type], ReturnType] ` `.
39
39
40
40
For example::
41
41
@@ -68,7 +68,7 @@ subscription to denote expected types for container elements.
68
68
overrides : Mapping[str , str ]) -> None : ...
69
69
70
70
Generics can be parametrized by using a new factory available in typing
71
- called TypeVar.
71
+ called :class: ` TypeVar ` .
72
72
73
73
.. code-block :: python
74
74
@@ -145,7 +145,7 @@ This is thus invalid::
145
145
class Pair(Generic[T, T]): # INVALID
146
146
...
147
147
148
- You can use multiple inheritance with `Generic `::
148
+ You can use multiple inheritance with :class: `Generic `::
149
149
150
150
from typing import TypeVar, Generic, Sized
151
151
@@ -154,6 +154,17 @@ You can use multiple inheritance with `Generic`::
154
154
class LinkedList(Sized, Generic[T]):
155
155
...
156
156
157
+ When inheriting from generic classes, some type variables could fixed::
158
+
159
+ from typing import TypeVar, Mapping
160
+
161
+ T = TypeVar('T')
162
+
163
+ class MyDict(Mapping[str, T]):
164
+ ...
165
+
166
+ In this case ``MyDict `` has a single parameter, ``T ``.
167
+
157
168
Subclassing a generic class without specifying type parameters assumes
158
169
:class: `Any ` for each position. In the following example, ``MyIterable `` is
159
170
not generic but implicitly inherits from ``Iterable[Any] ``::
@@ -162,7 +173,11 @@ not generic but implicitly inherits from ``Iterable[Any]``::
162
173
163
174
class MyIterable(Iterable): # Same as Iterable[Any]
164
175
165
- Generic metaclasses are not supported.
176
+ The metaclass used by :class: `Generic ` is a subclass of :class: `abc.ABCMeta `.
177
+ A generic class can be an ABC by including abstract methods or properties,
178
+ and generic classes can also have ABCs as base classes without a metaclass
179
+ conflict. Generic metaclasses are not supported.
180
+
166
181
167
182
The :class: `Any ` type
168
183
---------------------
@@ -178,15 +193,6 @@ when a value has type :class:`Any`, the type checker will allow all operations
178
193
on it, and a value of type :class: `Any ` can be assigned to a variable (or used
179
194
as a return value) of a more constrained type.
180
195
181
- Default argument values
182
- -----------------------
183
-
184
- Use a literal ellipsis ``... `` to declare an argument as having a default value::
185
-
186
- from typing import AnyStr
187
-
188
- def foo(x: AnyStr, y: AnyStr = ...) -> AnyStr: ...
189
-
190
196
191
197
Classes, functions, and decorators
192
198
----------------------------------
@@ -236,7 +242,11 @@ The module defines the following classes, functions and decorators:
236
242
237
243
Type variables may be marked covariant or contravariant by passing
238
244
``covariant=True `` or ``contravariant=True ``. See :pep: `484 ` for more
239
- details. By default type variables are invariant.
245
+ details. By default type variables are invariant. Alternatively,
246
+ a type variable may specify an upper bound using ``bound=<type> ``.
247
+ This means that an actual type substituted (explicitly or implictly)
248
+ for the type variable must be a subclass of the boundary type,
249
+ see :pep: `484 `.
240
250
241
251
.. class :: Union
242
252
@@ -329,57 +339,139 @@ The module defines the following classes, functions and decorators:
329
339
330
340
.. class :: Iterable(Generic[T_co])
331
341
342
+ A generic version of the :class: `collections.abc.Iterable `.
343
+
332
344
.. class :: Iterator(Iterable[T_co])
333
345
346
+ A generic version of the :class: `collections.abc.Iterator `.
347
+
334
348
.. class :: SupportsInt
335
349
350
+ An ABC with one abstract method `__int__ `.
351
+
336
352
.. class :: SupportsFloat
337
353
354
+ An ABC with one abstract method `__float__ `.
355
+
338
356
.. class :: SupportsAbs
339
357
358
+ An ABC with one abstract method `__abs__ ` that is covariant
359
+ in its return type.
360
+
340
361
.. class :: SupportsRound
341
362
363
+ An ABC with one abstract method `__round__ `
364
+ that is covariant in its return type.
365
+
342
366
.. class :: Reversible
343
367
368
+ An ABC with one abstract method `__reversed__ ` returning
369
+ an `Iterator[T_co] `.
370
+
344
371
.. class :: Container(Generic[T_co])
345
372
373
+ A generic version of :class: `collections.abc.Container `.
374
+
346
375
.. class :: AbstractSet(Sized, Iterable[T_co], Container[T_co])
347
376
377
+ A generic version of :class: `collections.abc.Set `.
378
+
348
379
.. class :: MutableSet(AbstractSet[T])
349
380
350
- .. class :: Mapping(Sized, Iterable[KT_co], Container[KT_co], Generic[KT_co, VT_co])
381
+ A generic version of :class: `collections.abc.MutableSet `.
382
+
383
+ .. class :: Mapping(Sized, Iterable[KT], Container[KT], Generic[VT_co])
384
+
385
+ A generic version of :class: `collections.abc.Mapping `.
351
386
352
387
.. class :: MutableMapping(Mapping[KT, VT])
353
388
389
+ A generic version of :class: `collections.abc.MutableMapping `.
390
+
354
391
.. class :: Sequence(Sized, Iterable[T_co], Container[T_co])
355
392
393
+ A generic version of :class: `collections.abc.Sequence `.
394
+
356
395
.. class :: MutableSequence(Sequence[T])
357
396
397
+ A generic version of :class: `collections.abc.MutableSequence `.
398
+
358
399
.. class :: ByteString(Sequence[int])
359
400
401
+ A generic version of :class: `collections.abc.ByteString `.
402
+
403
+ This type represents the types :class: `bytes `, :class: `bytearray `,
404
+ and :class: `memoryview `.
405
+
406
+ As a shorthand for this type, :class: `bytes ` can be used to
407
+ annotate arguments of any of the types mentioned above.
408
+
360
409
.. class :: List(list, MutableSequence[T])
361
410
362
- .. class :: Set(set, MutableSet[T])
411
+ Generic version of :class: `list `.
412
+ Useful for annotating return types. To annotate arguments it is preferred
413
+ to use abstract collection types such as :class: `Mapping `, :class: `Sequence `,
414
+ or :class: `AbstractSet `.
415
+
416
+ This type may be used as follows::
417
+
418
+ T = TypeVar('T', int, float)
419
+
420
+ def vec2(x: T, y: T) -> List[T]:
421
+ return [x, y]
422
+
423
+ def slice__to_4(vector: Sequence[T]) -> List[T]:
424
+ return vector[0:4]
425
+
426
+ .. class :: AbstractSet(set, MutableSet[T])
427
+
428
+ A generic version of :class: `collections.abc.Set `.
363
429
364
430
.. class :: MappingView(Sized, Iterable[T_co])
365
431
432
+ A generic version of :class: `collections.abc.MappingView `.
433
+
366
434
.. class :: KeysView(MappingView[KT_co], AbstractSet[KT_co])
367
435
436
+ A generic version of :class: `collections.abc.KeysView `.
437
+
368
438
.. class :: ItemsView(MappingView, Generic[KT_co, VT_co])
369
439
440
+ A generic version of :class: `collections.abc.ItemsView `.
441
+
370
442
.. class :: ValuesView(MappingView[VT_co])
371
443
444
+ A generic version of :class: `collections.abc.ValuesView `.
445
+
372
446
.. class :: Dict(dict, MutableMapping[KT, VT])
373
447
448
+ A generic version of :class: `dict `.
449
+ The usage of this type is as follows::
450
+
451
+ def get_position_in_index(word_list: Dict[str, int], word: str) -> int:
452
+ return word_list[word]
453
+
374
454
.. class :: Generator(Iterator[T_co], Generic[T_co, T_contra, V_co])
375
455
376
456
.. class :: io
377
457
378
- Wrapper namespace for IO generic classes.
458
+ Wrapper namespace for I/O stream types.
459
+
460
+ This defines the generic type ``IO[AnyStr] `` and aliases ``TextIO ``
461
+ and ``BinaryIO `` for respectively ``IO[str] `` and ``IO[bytes] ``.
462
+ These representing the types of I/O streams such as returned by
463
+ :func: `open `.
379
464
380
465
.. class :: re
381
466
382
- Wrapper namespace for re type classes.
467
+ Wrapper namespace for regular expression matching types.
468
+
469
+ This defines the type aliases ``Pattern `` and ``Match `` which
470
+ correspond to the return types from :func: `re.compile ` and
471
+ :func: `re.match `. These types (and the corresponding functions)
472
+ are generic in ``AnyStr `` and can be made specific by writing
473
+ ``Pattern[str] ``, ``Pattern[bytes] ``, ``Match[str] ``, or
474
+ ``Match[bytes] ``.
383
475
384
476
.. function :: NamedTuple(typename, fields)
385
477
0 commit comments