@@ -2,7 +2,7 @@ More types
2
2
==========
3
3
4
4
This 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
6
6
how to give functions more precise types using overloads. All of these are only
7
7
situationally useful, so feel free to skip this section and come back when you
8
8
have a need for some of them.
@@ -11,7 +11,7 @@ Here's a quick summary of what's covered here:
11
11
12
12
* :py:data: `~typing.NoReturn ` lets you tell mypy that a function never returns normally.
13
13
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
15
15
separate type by mypy but is identical to the original type at runtime.
16
16
For example, you can have ``UserId `` as a variant of ``int `` that is
17
17
just an ``int `` at runtime.
@@ -75,7 +75,7 @@ certain values from base class instances. Example:
75
75
...
76
76
77
77
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
79
79
almost zero runtime overhead. Mypy will treat the statement
80
80
``Derived = NewType('Derived', Base) `` as being roughly equivalent to the following
81
81
definition:
@@ -113,12 +113,12 @@ implicitly casting from ``UserId`` where ``int`` is expected. Examples:
113
113
114
114
num: int = UserId(5 ) + 1
115
115
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
117
117
containing the name of the new type and must equal the name of the variable to which the new
118
118
type is assigned. The second argument must be a properly subclassable class, i.e.,
119
119
not a type construct like :py:data: `~typing.Union `, etc.
120
120
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
122
122
supporting only one constructor accepting an instance of the base class (see above).
123
123
Example:
124
124
@@ -139,12 +139,12 @@ Example:
139
139
tcp_packet = TcpPacketId(127 , 0 ) # Fails in type checker and at runtime
140
140
141
141
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 `.
143
143
144
144
.. note ::
145
145
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
148
148
detect cases where you accidentally mixed together the old base type and the
149
149
new derived type.
150
150
@@ -160,7 +160,7 @@ You cannot use :py:func:`isinstance` or :py:func:`issubclass` on the object retu
160
160
161
161
name_by_id(3 ) # ints and UserId are synonymous
162
162
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:
164
164
165
165
.. code-block :: python
166
166
0 commit comments