Skip to content

Commit bd5f965

Browse files
authored
bpo-32717: Document PEP 560 (GH-6726)
1 parent ec1622d commit bd5f965

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed

Doc/library/types.rst

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ Dynamic Type Creation
5959

6060
The default value for the ``namespace`` element of the returned
6161
tuple has changed. Now an insertion-order-preserving mapping is
62-
used when the metaclass does not have a ``__prepare__`` method,
62+
used when the metaclass does not have a ``__prepare__`` method.
6363

6464
.. seealso::
6565

@@ -69,6 +69,23 @@ Dynamic Type Creation
6969
:pep:`3115` - Metaclasses in Python 3000
7070
Introduced the ``__prepare__`` namespace hook
7171

72+
.. function:: resolve_bases(bases)
73+
74+
Resolve MRO entries dynamically as specified by :pep:`560`.
75+
76+
This function looks for items in *bases* that are not instances of
77+
:class:`type`, and returns a tuple where each such object that has
78+
an ``__mro_entries__`` method is replaced with an unpacked result of
79+
calling this method. If a *bases* item is an instance of :class:`type`,
80+
or it doesn't have an ``__mro_entries__`` method, then it is included in
81+
the return tuple unchanged.
82+
83+
.. versionadded:: 3.7
84+
85+
.. seealso::
86+
87+
:pep:`560` - Core support for typing module and generic types
88+
7289

7390
Standard Interpreter Types
7491
--------------------------

Doc/reference/datamodel.rst

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1857,11 +1857,27 @@ passed through to all metaclass operations described below.
18571857

18581858
When a class definition is executed, the following steps occur:
18591859

1860+
* MRO entries are resolved
18601861
* the appropriate metaclass is determined
18611862
* the class namespace is prepared
18621863
* the class body is executed
18631864
* the class object is created
18641865

1866+
1867+
Resolving MRO entries
1868+
^^^^^^^^^^^^^^^^^^^^^
1869+
1870+
If a base that appears in class definition is not an instance of :class:`type`,
1871+
then an ``__mro_entries__`` method is searched on it. If found, it is called
1872+
with the original bases tuple. This method must return a tuple of classes that
1873+
will be used instead of this base. The tuple may be empty, in such case
1874+
the original base is ignored.
1875+
1876+
.. seealso::
1877+
1878+
:pep:`560` - Core support for typing module and generic types
1879+
1880+
18651881
Determining the appropriate metaclass
18661882
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
18671883
.. index::
@@ -2061,6 +2077,27 @@ case the instance is itself a class.
20612077
module) to the language.
20622078

20632079

2080+
Emulating generic types
2081+
-----------------------
2082+
2083+
One can implement the generic class syntax as specified by :pep:`484`
2084+
(for example ``List[int]``) by defining a special method
2085+
2086+
.. classmethod:: object.__class_getitem__(cls, key)
2087+
2088+
Return an object representing the specialization of a generic class
2089+
by type arguments found in *key*.
2090+
2091+
This method is looked up on the class object itself, and when defined in
2092+
the class body, this method is implicitly a class method. Note, this
2093+
mechanism is primarily reserved for use with static type hints, other usage
2094+
is discouraged.
2095+
2096+
.. seealso::
2097+
2098+
:pep:`560` - Core support for typing module and generic types
2099+
2100+
20642101
.. _callable-types:
20652102

20662103
Emulating callable objects

Doc/whatsnew/3.7.rst

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,25 @@ with the correct context in async/await code.
356356
PEP written and implemented by Yury Selivanov
357357

358358

359+
PEP 560: Core support for typing module and generic types
360+
---------------------------------------------------------
361+
362+
Initially :pep:`484` was designed in such way that it would not introduce *any*
363+
changes to the core CPython interpreter. Now type hints and the :mod:`typing`
364+
module are extensively used by the community, so this restriction is removed.
365+
The PEP introduces two special methods :meth:`__class_getitem__` and
366+
``__mro_entries__``, these methods are now used by most classes and special
367+
constructs in :mod:`typing`. As a result, the speed of various operations
368+
with types increased up to 7 times, the generic types can be used without
369+
metaclass conflicts, and several long standing bugs in :mod:`typing` module are
370+
fixed.
371+
372+
.. seealso::
373+
374+
:pep:`560` -- Core support for typing module and generic types
375+
PEP written and implemented by Ivan Levkivskyi
376+
377+
359378
New Development Mode: -X dev
360379
----------------------------
361380

0 commit comments

Comments
 (0)