@@ -1086,68 +1086,64 @@ The ``typing`` Module
1086
1086
1087
1087
To open the usage of static type checking to Python 3.5 as well as older
1088
1088
versions, a uniform namespace is required. For this purpose, a new
1089
- module in the standard library is introduced called ``typing``. It
1090
- holds a set of classes representing builtin types with generics, namely:
1089
+ module in the standard library is introduced called ``typing``.
1091
1090
1092
- * Dict, used as ``Dict[key_type, value_type]``
1091
+ It defines the fundamental building blocks for constructing types
1092
+ (e.g. ``Any``), types representing generic variants of builtin
1093
+ collections (e.g. ``List``), types representing generic
1094
+ collection ABCs (e.g. ``Sequence``), and a small collection of
1095
+ convenience definitions.
1093
1096
1094
- * List, used as ``List[element_type]``
1097
+ Fundamental building blocks:
1095
1098
1096
- * Set, used as ``Set[element_type]``. See remark for ``AbstractSet``
1097
- below.
1099
+ * Any, used as ``def get(key: str) -> Any: ...``
1098
1100
1099
- * FrozenSet, used as ``FrozenSet[element_type]``
1101
+ * Union, used as ``Union[Type1, Type2, Type3]``
1102
+
1103
+ * Callable, used as ``Callable[[Arg1Type, Arg2Type], ReturnType]``
1100
1104
1101
1105
* Tuple, used by listing the element types, for example
1102
1106
``Tuple[int, int, str]``.
1103
1107
Arbitrary-length homogeneous tuples can be expressed
1104
1108
using one type and ellipsis, for example ``Tuple[int, ...]``.
1105
- (The ``...`` here are part of the syntax.)
1109
+ (The ``...`` here are part of the syntax, a literal ellipsis .)
1106
1110
1107
- * NamedTuple, used as
1108
- ``NamedTuple(type_name, [(field_name, field_type), ...])``
1109
- and equivalent to
1110
- ``collections.namedtuple(type_name, [field_name, ...])``.
1111
-
1112
- The generic versions of concrete collection types (``Dict``, ``List``,
1113
- ``Set``, ``FrozenSet``, and homogeneous arbitrary-length ``Tuple``)
1114
- are mainly useful for annotating return values. For arguments, prefer
1115
- the abstract collection types defined below, e.g. ``Mapping``,
1116
- ``Sequence`` or ``AbstractSet``.
1117
-
1118
- The ``typing`` module defines the ``Generator`` type for return values
1119
- of generator functions. It is a subtype of ``Iterable`` and it has
1120
- additional type variables for the type accepted by the ``send()``
1121
- method (which is contravariant -- a generator that accepts sending it
1122
- ``Employee`` instance is valid in a context where a generator is
1123
- required that accepts sending it ``Manager`` instances) and the return
1124
- type of the generator:
1125
-
1126
- * Generator, used as ``Generator[yield_type, send_type, return_type]``
1111
+ * TypeVar, used as ``X = TypeVar('X', Type1, Type2, Type3)`` or simply
1112
+ ``Y = TypeVar('Y')`` (see above for more details)
1127
1113
1128
- It also introduces factories and helper members needed to express
1129
- generics and union types:
1114
+ Generic variants of builtin collections:
1130
1115
1131
- * Any , used as ``def get(key: str) -> Any: ... ``
1116
+ * Dict , used as ``Dict[key_type, value_type] ``
1132
1117
1133
- * Union , used as ``Union[Type1, Type2, Type3 ]``
1118
+ * List , used as ``List[element_type ]``
1134
1119
1135
- * TypeVar , used as ``X = TypeVar('X', Type1, Type2, Type3)`` or simply
1136
- ``Y = TypeVar('Y')``
1120
+ * Set , used as ``Set[element_type]``. See remark for ``AbstractSet``
1121
+ below.
1137
1122
1138
- * Callable , used as ``Callable[[Arg1Type, Arg2Type], ReturnType ]``
1123
+ * FrozenSet , used as ``FrozenSet[element_type ]``
1139
1124
1140
- * AnyStr, defined as ``TypeVar('AnyStr', str, bytes)``
1125
+ Note: ``Dict``, ``List``, ``Set`` and ``FrozenSet`` are mainly useful
1126
+ for annotating return values. For arguments, prefer the abstract
1127
+ collection types defined below, e.g. ``Mapping``, ``Sequence`` or
1128
+ ``AbstractSet``.
1141
1129
1142
- All abstract base classes available in ``collections.abc`` are
1143
- importable from the ``typing`` module, with added generics support:
1130
+ Generic variants of container ABCs (and a few non-containers):
1144
1131
1145
1132
* ByteString
1146
1133
1147
- * Callable (see above)
1134
+ * Callable (see above, listed here for completeness )
1148
1135
1149
1136
* Container
1150
1137
1138
+ * Generator, used as ``Generator[yield_type, send_type,
1139
+ return_type]``. This represents the return value of generator
1140
+ functions. It is a subtype of ``Iterable`` and it has additional
1141
+ type variables for the type accepted by the ``send()`` method (which
1142
+ is contravariant -- a generator that accepts sending it ``Employee``
1143
+ instance is valid in a context where a generator is required that
1144
+ accepts sending it ``Manager`` instances) and the return type of the
1145
+ generator.
1146
+
1151
1147
* Hashable (not generic, but present for completeness)
1152
1148
1153
1149
* ItemsView
@@ -1195,7 +1191,7 @@ A few one-off types are defined that test for single special methods
1195
1191
1196
1192
* SupportsBytes, to test for ``__bytes__``
1197
1193
1198
- The library includes literals for platform-specific type hinting:
1194
+ Constants for platform-specific type hinting:
1199
1195
1200
1196
* PY2
1201
1197
@@ -1205,41 +1201,46 @@ The library includes literals for platform-specific type hinting:
1205
1201
1206
1202
* POSIX, equivalent to ``not WINDOWS``
1207
1203
1208
- The following conveniece functions and decorators are exported :
1204
+ Convenience definitions :
1209
1205
1210
- * cast, described earlier
1206
+ * AnyStr, defined as ``TypeVar('AnyStr', str, bytes)``
1211
1207
1212
- * no_type_check, a decorator to disable type checking per class or
1208
+ * NamedTuple, used as
1209
+ ``NamedTuple(type_name, [(field_name, field_type), ...])``
1210
+ and equivalent to
1211
+ ``collections.namedtuple(type_name, [field_name, ...])``.
1212
+ This is useful to declare the types of the fields of a a named tuple
1213
+ type.
1214
+
1215
+ * cast(), described earlier
1216
+
1217
+ * @no_type_check, a decorator to disable type checking per class or
1213
1218
function (see below)
1214
1219
1215
- * no_type_check_decorator, a decorator to create your own decorators
1220
+ * @ no_type_check_decorator, a decorator to create your own decorators
1216
1221
with the same meaning as ``@no_type_check`` (see below)
1217
1222
1218
- * overload, described earlier
1223
+ * @ overload, described earlier
1219
1224
1220
- * get_type_hints, a utility function to retrieve the type hints from a
1225
+ * get_type_hints() , a utility function to retrieve the type hints from a
1221
1226
function or method. Given a function or method object, it returns
1222
1227
a dict with the same format as ``__annotations__``, but evaluating
1223
1228
forward references (which are given as string literals) as expressions
1224
1229
in the context of the original function or method definition.
1225
1230
1226
- The following types are available in the ``typing.io`` module :
1231
+ Types available in the ``typing.io`` submodule :
1227
1232
1228
1233
* IO (generic over ``AnyStr``)
1229
1234
1230
1235
* BinaryIO (a simple subtype of ``IO[bytes]``)
1231
1236
1232
1237
* TextIO (a simple subtype of ``IO[str]``)
1233
1238
1234
- The following types are provided by the ``typing.re`` module :
1239
+ Types available in the ``typing.re`` submodule :
1235
1240
1236
1241
* Match and Pattern, types of ``re.match()`` and ``re.compile()``
1237
1242
results (generic over ``AnyStr``)
1238
1243
1239
- As a convenience measure, types from ``typing.io`` and ``typing.re`` are
1240
- also available in ``typing`` (quoting Guido, "There's a reason those
1241
- modules have two-letter names.").
1242
-
1243
1244
1244
1245
Rejected Alternatives
1245
1246
=====================
0 commit comments