Skip to content

Commit 2a19d95

Browse files
committed
Restore doc updates to typing.rst by Ivan Levkivskyi and Daniel Andrade Groppe.
1 parent 88d073d commit 2a19d95

File tree

1 file changed

+110
-18
lines changed

1 file changed

+110
-18
lines changed

Doc/library/typing.rst

Lines changed: 110 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Callable
3535
--------
3636

3737
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]``.
3939

4040
For example::
4141

@@ -68,7 +68,7 @@ subscription to denote expected types for container elements.
6868
overrides: Mapping[str, str]) -> None: ...
6969
7070
Generics can be parametrized by using a new factory available in typing
71-
called TypeVar.
71+
called :class:`TypeVar`.
7272

7373
.. code-block:: python
7474
@@ -145,7 +145,7 @@ This is thus invalid::
145145
class Pair(Generic[T, T]): # INVALID
146146
...
147147

148-
You can use multiple inheritance with `Generic`::
148+
You can use multiple inheritance with :class:`Generic`::
149149

150150
from typing import TypeVar, Generic, Sized
151151

@@ -154,6 +154,17 @@ You can use multiple inheritance with `Generic`::
154154
class LinkedList(Sized, Generic[T]):
155155
...
156156

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+
157168
Subclassing a generic class without specifying type parameters assumes
158169
:class:`Any` for each position. In the following example, ``MyIterable`` is
159170
not generic but implicitly inherits from ``Iterable[Any]``::
@@ -162,7 +173,11 @@ not generic but implicitly inherits from ``Iterable[Any]``::
162173

163174
class MyIterable(Iterable): # Same as Iterable[Any]
164175

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+
166181

167182
The :class:`Any` type
168183
---------------------
@@ -178,15 +193,6 @@ when a value has type :class:`Any`, the type checker will allow all operations
178193
on it, and a value of type :class:`Any` can be assigned to a variable (or used
179194
as a return value) of a more constrained type.
180195

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

191197
Classes, functions, and decorators
192198
----------------------------------
@@ -236,7 +242,11 @@ The module defines the following classes, functions and decorators:
236242

237243
Type variables may be marked covariant or contravariant by passing
238244
``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`.
240250

241251
.. class:: Union
242252

@@ -329,57 +339,139 @@ The module defines the following classes, functions and decorators:
329339

330340
.. class:: Iterable(Generic[T_co])
331341

342+
A generic version of the :class:`collections.abc.Iterable`.
343+
332344
.. class:: Iterator(Iterable[T_co])
333345

346+
A generic version of the :class:`collections.abc.Iterator`.
347+
334348
.. class:: SupportsInt
335349

350+
An ABC with one abstract method `__int__`.
351+
336352
.. class:: SupportsFloat
337353

354+
An ABC with one abstract method `__float__`.
355+
338356
.. class:: SupportsAbs
339357

358+
An ABC with one abstract method `__abs__` that is covariant
359+
in its return type.
360+
340361
.. class:: SupportsRound
341362

363+
An ABC with one abstract method `__round__`
364+
that is covariant in its return type.
365+
342366
.. class:: Reversible
343367

368+
An ABC with one abstract method `__reversed__` returning
369+
an `Iterator[T_co]`.
370+
344371
.. class:: Container(Generic[T_co])
345372

373+
A generic version of :class:`collections.abc.Container`.
374+
346375
.. class:: AbstractSet(Sized, Iterable[T_co], Container[T_co])
347376

377+
A generic version of :class:`collections.abc.Set`.
378+
348379
.. class:: MutableSet(AbstractSet[T])
349380

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

352387
.. class:: MutableMapping(Mapping[KT, VT])
353388

389+
A generic version of :class:`collections.abc.MutableMapping`.
390+
354391
.. class:: Sequence(Sized, Iterable[T_co], Container[T_co])
355392

393+
A generic version of :class:`collections.abc.Sequence`.
394+
356395
.. class:: MutableSequence(Sequence[T])
357396

397+
A generic version of :class:`collections.abc.MutableSequence`.
398+
358399
.. class:: ByteString(Sequence[int])
359400

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+
360409
.. class:: List(list, MutableSequence[T])
361410

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

364430
.. class:: MappingView(Sized, Iterable[T_co])
365431

432+
A generic version of :class:`collections.abc.MappingView`.
433+
366434
.. class:: KeysView(MappingView[KT_co], AbstractSet[KT_co])
367435

436+
A generic version of :class:`collections.abc.KeysView`.
437+
368438
.. class:: ItemsView(MappingView, Generic[KT_co, VT_co])
369439

440+
A generic version of :class:`collections.abc.ItemsView`.
441+
370442
.. class:: ValuesView(MappingView[VT_co])
371443

444+
A generic version of :class:`collections.abc.ValuesView`.
445+
372446
.. class:: Dict(dict, MutableMapping[KT, VT])
373447

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+
374454
.. class:: Generator(Iterator[T_co], Generic[T_co, T_contra, V_co])
375455

376456
.. class:: io
377457

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

380465
.. class:: re
381466

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]``.
383475

384476
.. function:: NamedTuple(typename, fields)
385477

0 commit comments

Comments
 (0)