Skip to content

Commit 10ba5c1

Browse files
authored
Docs: Use PEP 585 syntax in "The type of class objects" (#12516)
1 parent ce858a6 commit 10ba5c1

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

docs/source/kinds_of_types.rst

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -615,10 +615,11 @@ The type of class objects
615615
<484#the-type-of-class-objects>`.)
616616

617617
Sometimes you want to talk about class objects that inherit from a
618-
given class. This can be spelled as :py:class:`Type[C] <typing.Type>` where ``C`` is a
618+
given class. This can be spelled as ``type[C]`` (or, on Python 3.8 and lower,
619+
:py:class:`typing.Type[C] <typing.Type>`) where ``C`` is a
619620
class. In other words, when ``C`` is the name of a class, using ``C``
620621
to annotate an argument declares that the argument is an instance of
621-
``C`` (or of a subclass of ``C``), but using :py:class:`Type[C] <typing.Type>` as an
622+
``C`` (or of a subclass of ``C``), but using ``type[C]`` as an
622623
argument annotation declares that the argument is a class object
623624
deriving from ``C`` (or ``C`` itself).
624625

@@ -649,7 +650,7 @@ you pass it the right class object:
649650
# (Here we could write the user object to a database)
650651
return user
651652
652-
How would we annotate this function? Without :py:class:`~typing.Type` the best we
653+
How would we annotate this function? Without the ability to parameterize ``type``, the best we
653654
could do would be:
654655

655656
.. code-block:: python
@@ -665,14 +666,14 @@ doesn't see that the ``buyer`` variable has type ``ProUser``:
665666
buyer = new_user(ProUser)
666667
buyer.pay() # Rejected, not a method on User
667668
668-
However, using :py:class:`~typing.Type` and a type variable with an upper bound (see
669+
However, using the ``type[C]`` syntax and a type variable with an upper bound (see
669670
:ref:`type-variable-upper-bound`) we can do better:
670671

671672
.. code-block:: python
672673
673674
U = TypeVar('U', bound=User)
674675
675-
def new_user(user_class: Type[U]) -> U:
676+
def new_user(user_class: type[U]) -> U:
676677
# Same implementation as before
677678
678679
Now mypy will infer the correct type of the result when we call
@@ -685,12 +686,12 @@ Now mypy will infer the correct type of the result when we call
685686
686687
.. note::
687688

688-
The value corresponding to :py:class:`Type[C] <typing.Type>` must be an actual class
689+
The value corresponding to ``type[C]`` must be an actual class
689690
object that's a subtype of ``C``. Its constructor must be
690691
compatible with the constructor of ``C``. If ``C`` is a type
691692
variable, its upper bound must be a class object.
692693

693-
For more details about ``Type[]`` see :pep:`PEP 484: The type of
694+
For more details about ``type[]`` and :py:class:`typing.Type[] <typing.Type>`, see :pep:`PEP 484: The type of
694695
class objects <484#the-type-of-class-objects>`.
695696

696697
.. _text-and-anystr:

0 commit comments

Comments
 (0)