bpo-32717: Document PEP 560 (GH-6726) · python/cpython@101d0d5 (original) (raw)

`@@ -1857,11 +1857,27 @@ passed through to all metaclass operations described below.

`

1857

1857

``

1858

1858

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

`

1859

1859

``

``

1860

`+

`

1860

1861

`* the appropriate metaclass is determined

`

1861

1862

`* the class namespace is prepared

`

1862

1863

`* the class body is executed

`

1863

1864

`* the class object is created

`

1864

1865

``

``

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

+

1865

1881

`Determining the appropriate metaclass

`

1866

1882

`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

`

1867

1883

`.. index::

`

`@@ -2061,6 +2077,27 @@ case the instance is itself a class.

`

2061

2077

` module) to the language.

`

2062

2078

``

2063

2079

``

``

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

+

2064

2101

`.. _callable-types:

`

2065

2102

``

2066

2103

`Emulating callable objects

`