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
e848cd76
Commit
e848cd76
authored
Sep 06, 2016
by
Guido van Rossum
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #27905: Docs for typing.Type[C], by Michael Lee.
parent
33d2a492
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
39 additions
and
0 deletions
+39
-0
Doc/library/typing.rst
Doc/library/typing.rst
+39
-0
No files found.
Doc/library/typing.rst
View file @
e848cd76
...
...
@@ -502,6 +502,45 @@ The module defines the following classes, functions and decorators:
except KeyError:
return default
.. class:: Type
A variable annotated with ``C`` may accept a value of type ``C``. In
contrast, a variable annotated with ``Type[C]`` may accept values that are
classes themselves -- specifically, it will accept the *class object* of
``C``. For example::
a = 3 # Has type 'int'
b = int # Has type 'Type[int]'
c = type(a) # Also has type 'Type[int]'
Note that ``Type[C]`` is covariant::
class User: ...
class BasicUser(User): ...
class ProUser(User): ...
class TeamUser(User): ...
# Accepts User, BasicUser, ProUser, TeamUser, ...
def make_new_user(user_class: Type[User]) -> User:
# ...
return user_class()
The fact that ``Type[C]`` is covariant implies that all subclasses of
``C`` should implement the same constructor signature and class method
signatures as ``C``. The type checker should flag violations of this,
but should also allow constructor calls in subclasses that match the
constructor calls in the indicated base class. How the type checker is
required to handle this particular case may change in future revisions of
PEP 484.
The only legal parameters for ``Type`` are classes, unions of classes, and
``Any``. For example::
def new_non_team_user(user_class: Type[Union[BaseUser, ProUser]]): ...
``Type[Any]`` is equivalent to ``Type`` which in turn is equivalent
to ``type``, which is the root of Python's metaclass hierarchy.
.. class:: Iterable(Generic[T_co])
A generic version of the :class:`collections.abc.Iterable`.
...
...
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