@@ -2,7 +2,7 @@ More types
22==========
33
44This section introduces a few additional kinds of types, including :py:data: `~typing.NoReturn `,
5- :py:func: ` NewType < typing.NewType> `, and types for async code. It also discusses
5+ :py:class: ` ~ typing.NewType `, and types for async code. It also discusses
66how to give functions more precise types using overloads. All of these are only
77situationally useful, so feel free to skip this section and come back when you
88have a need for some of them.
@@ -11,7 +11,7 @@ Here's a quick summary of what's covered here:
1111
1212* :py:data: `~typing.NoReturn ` lets you tell mypy that a function never returns normally.
1313
14- * :py:func: ` NewType < typing.NewType> ` lets you define a variant of a type that is treated as a
14+ * :py:class: ` ~ typing.NewType ` lets you define a variant of a type that is treated as a
1515 separate type by mypy but is identical to the original type at runtime.
1616 For example, you can have ``UserId `` as a variant of ``int `` that is
1717 just an ``int `` at runtime.
@@ -75,7 +75,7 @@ certain values from base class instances. Example:
7575 ...
7676
7777 However, this approach introduces some runtime overhead. To avoid this, the typing
78- module provides a helper object :py:func: ` NewType < typing.NewType> ` that creates simple unique types with
78+ module provides a helper object :py:class: ` ~ typing.NewType ` that creates simple unique types with
7979almost zero runtime overhead. Mypy will treat the statement
8080``Derived = NewType('Derived', Base) `` as being roughly equivalent to the following
8181definition:
@@ -113,12 +113,12 @@ implicitly casting from ``UserId`` where ``int`` is expected. Examples:
113113
114114 num: int = UserId(5 ) + 1
115115
116- :py:func: ` NewType < typing.NewType> ` accepts exactly two arguments. The first argument must be a string literal
116+ :py:class: ` ~ typing.NewType ` accepts exactly two arguments. The first argument must be a string literal
117117containing the name of the new type and must equal the name of the variable to which the new
118118type is assigned. The second argument must be a properly subclassable class, i.e.,
119119not a type construct like :py:data: `~typing.Union `, etc.
120120
121- The callable returned by :py:func: ` NewType < typing.NewType> ` accepts only one argument; this is equivalent to
121+ The callable returned by :py:class: ` ~ typing.NewType ` accepts only one argument; this is equivalent to
122122supporting only one constructor accepting an instance of the base class (see above).
123123Example:
124124
@@ -139,12 +139,12 @@ Example:
139139 tcp_packet = TcpPacketId(127 , 0 ) # Fails in type checker and at runtime
140140
141141 You cannot use :py:func: `isinstance ` or :py:func: `issubclass ` on the object returned by
142- :py:func : `~typing.NewType `, nor can you subclass an object returned by :py:func : `~typing.NewType `.
142+ :py:class : `~typing.NewType `, nor can you subclass an object returned by :py:class : `~typing.NewType `.
143143
144144.. note ::
145145
146- Unlike type aliases, :py:func: ` NewType < typing.NewType> ` will create an entirely new and
147- unique type when used. The intended purpose of :py:func: ` NewType < typing.NewType> ` is to help you
146+ Unlike type aliases, :py:class: ` ~ typing.NewType ` will create an entirely new and
147+ unique type when used. The intended purpose of :py:class: ` ~ typing.NewType ` is to help you
148148 detect cases where you accidentally mixed together the old base type and the
149149 new derived type.
150150
@@ -160,7 +160,7 @@ You cannot use :py:func:`isinstance` or :py:func:`issubclass` on the object retu
160160
161161 name_by_id(3 ) # ints and UserId are synonymous
162162
163- But a similar example using :py:func: ` NewType < typing.NewType> ` will not typecheck:
163+ But a similar example using :py:class: ` ~ typing.NewType ` will not typecheck:
164164
165165 .. code-block :: python
166166
0 commit comments