@@ -77,7 +77,7 @@ Note that ``None`` as a type hint is a special case and is replaced by
7777NewType
7878=======
7979
80- Use the :func : `NewType ` helper function to create distinct types::
80+ Use the :class : `NewType ` helper class to create distinct types::
8181
8282 from typing import NewType
8383
@@ -106,15 +106,14 @@ accidentally creating a ``UserId`` in an invalid way::
106106
107107Note that these checks are enforced only by the static type checker. At runtime,
108108the statement ``Derived = NewType('Derived', Base) `` will make ``Derived `` a
109- function that immediately returns whatever parameter you pass it. That means
109+ class that immediately returns whatever parameter you pass it. That means
110110the expression ``Derived(some_value) `` does not create a new class or introduce
111- any overhead beyond that of a regular function call.
111+ much overhead beyond that of a regular function call.
112112
113113More precisely, the expression ``some_value is Derived(some_value) `` is always
114114true at runtime.
115115
116- This also means that it is not possible to create a subtype of ``Derived ``
117- since it is an identity function at runtime, not an actual type::
116+ It is invalid to create a subtype of ``Derived ``::
118117
119118 from typing import NewType
120119
@@ -123,7 +122,7 @@ since it is an identity function at runtime, not an actual type::
123122 # Fails at runtime and does not typecheck
124123 class AdminUserId(UserId): pass
125124
126- However, it is possible to create a :func : `NewType ` based on a 'derived' ``NewType ``::
125+ However, it is possible to create a :class : `NewType ` based on a 'derived' ``NewType ``::
127126
128127 from typing import NewType
129128
@@ -151,6 +150,12 @@ See :pep:`484` for more details.
151150
152151.. versionadded :: 3.5.2
153152
153+ .. versionchanged :: 3.10.0
154+ ``NewType `` is now a class rather than a function. There is some additional
155+ runtime cost when calling ``NewType `` over a regular function. However, this
156+ cost will be reduced in 3.11.0.
157+
158+
154159Callable
155160========
156161
@@ -1306,17 +1311,21 @@ These are not used in annotations. They are building blocks for declaring types.
13061311 Removed the ``_field_types `` attribute in favor of the more
13071312 standard ``__annotations__ `` attribute which has the same information.
13081313
1309- .. function :: NewType(name, tp)
1314+ .. class :: NewType(name, tp)
13101315
1311- A helper function to indicate a distinct type to a typechecker,
1312- see :ref: `distinct `. At runtime it returns a function that returns
1313- its argument. Usage::
1316+ A helper class to indicate a distinct type to a typechecker,
1317+ see :ref: `distinct `. At runtime it returns an object that returns
1318+ its argument when called.
1319+ Usage::
13141320
13151321 UserId = NewType('UserId', int)
13161322 first_user = UserId(1)
13171323
13181324 .. versionadded :: 3.5.2
13191325
1326+ .. versionchanged :: 3.10.0
1327+ ``NewType `` is now a class rather than a function.
1328+
13201329.. class :: TypedDict(dict)
13211330
13221331 Special construct to add type hints to a dictionary.
0 commit comments