Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
0db1c42c
Commit
0db1c42c
authored
Sep 09, 2015
by
Guido van Rossum
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update typing docs based on a patch by Daniel Andrade Groppe.
parent
2e7da04b
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
53 additions
and
14 deletions
+53
-14
Doc/library/typing.rst
Doc/library/typing.rst
+53
-14
No files found.
Doc/library/typing.rst
View file @
0db1c42c
...
...
@@ -68,7 +68,7 @@ subscription to denote expected types for container elements.
overrides: Mapping[str, str]) -> None: ...
Generics can be parametrized by using a new factory available in typing
called
TypeVar
.
called
:class:`TypeVar`
.
.. code-block:: python
...
...
@@ -145,7 +145,7 @@ This is thus invalid::
class Pair(Generic[T, T]): # INVALID
...
You can use multiple inheritance with `Generic`::
You can use multiple inheritance with
:class:
`Generic`::
from typing import TypeVar, Generic, Sized
...
...
@@ -154,6 +154,17 @@ You can use multiple inheritance with `Generic`::
class LinkedList(Sized, Generic[T]):
...
When inheriting from generic classes, some type variables could fixed::
from typing import TypeVar, Mapping
T = TypeVar('T')
class MyDict(Mapping[str, T]):
...
In this case ``MyDict`` has a single parameter, ``T``.
Subclassing a generic class without specifying type parameters assumes
:class:`Any` for each position. In the following example, ``MyIterable`` is
not generic but implicitly inherits from ``Iterable[Any]``::
...
...
@@ -162,7 +173,11 @@ not generic but implicitly inherits from ``Iterable[Any]``::
class MyIterable(Iterable): # Same as Iterable[Any]
Generic metaclasses are not supported.
The metaclass used by :class:`Generic` is a subclass of :class:`abc.ABCMeta`.
A generic class can be an ABC by including abstract methods or properties,
and generic classes can also have ABCs as base classes without a metaclass
conflict. Generic metaclasses are not supported.
The :class:`Any` type
---------------------
...
...
@@ -178,15 +193,6 @@ when a value has type :class:`Any`, the type checker will allow all operations
on it, and a value of type :class:`Any` can be assigned to a variable (or used
as a return value) of a more constrained type.
Default argument values
-----------------------
Use a literal ellipsis ``...`` to declare an argument as having a default value::
from typing import AnyStr
def foo(x: AnyStr, y: AnyStr = ...) -> AnyStr: ...
Classes, functions, and decorators
----------------------------------
...
...
@@ -236,7 +242,11 @@ The module defines the following classes, functions and decorators:
Type variables may be marked covariant or contravariant by passing
``covariant=True`` or ``contravariant=True``. See :pep:`484` for more
details. By default type variables are invariant.
details. By default type variables are invariant. Alternatively,
a type variable may specify an upper bound using ``bound=<type>``.
This means that an actual type substituted (explicitly or implictly)
for the type variable must be a subclass of the boundary type,
see :pep:`484`.
.. class:: Union
...
...
@@ -329,30 +339,59 @@ The module defines the following classes, functions and decorators:
.. class:: Iterable(Generic[T_co])
A generic version of the :class:`collections.abc.Iterable`.
.. class:: Iterator(Iterable[T_co])
A generic version of the :class:`collections.abc.Iterator`.
.. class:: SupportsInt
An ABC with one abstract method `__int__`.
.. class:: SupportsFloat
An ABC with one abstract method `__float__`.
.. class:: SupportsAbs
An ABC with one abstract method `__abs__` that is covariant
in its return type.
.. class:: SupportsRound
An ABC with one abstract method `__round__`
that is covariant in its return type.
.. class:: Reversible
An ABC with one abstract method `__reversed__` returning
an `Iterator[T_co]`.
.. class:: Container(Generic[T_co])
A generic version of :class:`collections.abc.Container`.
.. class:: AbstractSet(Sized, Iterable[T_co], Container[T_co])
A generic version of :class:`collections.abc.Set`.
.. class:: MutableSet(AbstractSet[T])
.. class:: Mapping(Sized, Iterable[KT_co], Container[KT_co], Generic[KT_co, VT_co])
A generic version of :class:`collections.abc.MutableSet`.
.. class:: Mapping(Sized, Iterable[KT], Container[KT], Generic[VT_co])
A generic version of :class:`collections.abc.Mapping`.
.. class:: MutableMapping(Mapping[KT, VT])
A generic version of :class:`collections.abc.MutableMapping`.
.. class:: Sequence(Sized, Iterable[T_co], Container[T_co])
A generic version of :class:`collections.abc.Sequence`.
.. class:: MutableSequence(Sequence[T])
A generic version of :class:`collections.abc.MutableSequence`.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment