Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0db1c42

Browse files
author
Guido van Rossum
committed
Update typing docs based on a patch by Daniel Andrade Groppe.
1 parent 2e7da04 commit 0db1c42

1 file changed

Lines changed: 53 additions & 14 deletions

File tree

Doc/library/typing.rst

Lines changed: 53 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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,30 +339,59 @@ 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

358397
A generic version of :class:`collections.abc.MutableSequence`.

0 commit comments

Comments
 (0)