Skip to content

Commit b612c8e

Browse files
author
Guido van Rossum
committed
Reorder the description of what's in typing.py from most to least fundamental. Fixes #111.
1 parent d02f33a commit b612c8e

File tree

1 file changed

+53
-52
lines changed

1 file changed

+53
-52
lines changed

pep-0484.txt

Lines changed: 53 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,68 +1086,64 @@ The ``typing`` Module
10861086

10871087
To open the usage of static type checking to Python 3.5 as well as older
10881088
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``.
10911090

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.
10931096

1094-
* List, used as ``List[element_type]``
1097+
Fundamental building blocks:
10951098

1096-
* Set, used as ``Set[element_type]``. See remark for ``AbstractSet``
1097-
below.
1099+
* Any, used as ``def get(key: str) -> Any: ...``
10981100

1099-
* FrozenSet, used as ``FrozenSet[element_type]``
1101+
* Union, used as ``Union[Type1, Type2, Type3]``
1102+
1103+
* Callable, used as ``Callable[[Arg1Type, Arg2Type], ReturnType]``
11001104

11011105
* Tuple, used by listing the element types, for example
11021106
``Tuple[int, int, str]``.
11031107
Arbitrary-length homogeneous tuples can be expressed
11041108
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.)
11061110

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)
11271113

1128-
It also introduces factories and helper members needed to express
1129-
generics and union types:
1114+
Generic variants of builtin collections:
11301115

1131-
* Any, used as ``def get(key: str) -> Any: ...``
1116+
* Dict, used as ``Dict[key_type, value_type]``
11321117

1133-
* Union, used as ``Union[Type1, Type2, Type3]``
1118+
* List, used as ``List[element_type]``
11341119

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.
11371122

1138-
* Callable, used as ``Callable[[Arg1Type, Arg2Type], ReturnType]``
1123+
* FrozenSet, used as ``FrozenSet[element_type]``
11391124

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``.
11411129

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):
11441131

11451132
* ByteString
11461133

1147-
* Callable (see above)
1134+
* Callable (see above, listed here for completeness)
11481135

11491136
* Container
11501137

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+
11511147
* Hashable (not generic, but present for completeness)
11521148

11531149
* ItemsView
@@ -1195,7 +1191,7 @@ A few one-off types are defined that test for single special methods
11951191

11961192
* SupportsBytes, to test for ``__bytes__``
11971193

1198-
The library includes literals for platform-specific type hinting:
1194+
Constants for platform-specific type hinting:
11991195

12001196
* PY2
12011197

@@ -1205,41 +1201,46 @@ The library includes literals for platform-specific type hinting:
12051201

12061202
* POSIX, equivalent to ``not WINDOWS``
12071203

1208-
The following conveniece functions and decorators are exported:
1204+
Convenience definitions:
12091205

1210-
* cast, described earlier
1206+
* AnyStr, defined as ``TypeVar('AnyStr', str, bytes)``
12111207

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
12131218
function (see below)
12141219

1215-
* no_type_check_decorator, a decorator to create your own decorators
1220+
* @no_type_check_decorator, a decorator to create your own decorators
12161221
with the same meaning as ``@no_type_check`` (see below)
12171222

1218-
* overload, described earlier
1223+
* @overload, described earlier
12191224

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
12211226
function or method. Given a function or method object, it returns
12221227
a dict with the same format as ``__annotations__``, but evaluating
12231228
forward references (which are given as string literals) as expressions
12241229
in the context of the original function or method definition.
12251230

1226-
The following types are available in the ``typing.io`` module:
1231+
Types available in the ``typing.io`` submodule:
12271232

12281233
* IO (generic over ``AnyStr``)
12291234

12301235
* BinaryIO (a simple subtype of ``IO[bytes]``)
12311236

12321237
* TextIO (a simple subtype of ``IO[str]``)
12331238

1234-
The following types are provided by the ``typing.re`` module:
1239+
Types available in the ``typing.re`` submodule:
12351240

12361241
* Match and Pattern, types of ``re.match()`` and ``re.compile()``
12371242
results (generic over ``AnyStr``)
12381243

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-
12431244

12441245
Rejected Alternatives
12451246
=====================

0 commit comments

Comments
 (0)