summaryrefslogtreecommitdiffstats
path: root/Doc/library
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2021-07-24 09:31:33 (GMT)
committerGitHub <noreply@github.com>2021-07-24 09:31:33 (GMT)
commitd15949a845a6db66675bca7105ad508ba9e79639 (patch)
tree81ce3e940a182d7d9b3d7e87408b75d17ac897d4 /Doc/library
parent5ec275758dbc307e9838e7038bfc3b5390950ea7 (diff)
downloadcpython-d15949a845a6db66675bca7105ad508ba9e79639.zip
cpython-d15949a845a6db66675bca7105ad508ba9e79639.tar.gz
cpython-d15949a845a6db66675bca7105ad508ba9e79639.tar.bz2
bpo-44353: Document that typing.NewType is now a class (GH-27319) (GH-27321)
(cherry picked from commit 7aac3f623610cf3dffbf548a5be5bfd4fa6790a0) Co-authored-by: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com>
Diffstat (limited to 'Doc/library')
-rw-r--r--Doc/library/typing.rst29
1 files changed, 19 insertions, 10 deletions
diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst
index b1d67e4..8cccdc0 100644
--- a/Doc/library/typing.rst
+++ b/Doc/library/typing.rst
@@ -77,7 +77,7 @@ Note that ``None`` as a type hint is a special case and is replaced by
NewType
=======
-Use the :func:`NewType` helper function to create distinct types::
+Use the :class:`NewType` helper class to create distinct types::
from typing import NewType
@@ -106,15 +106,14 @@ accidentally creating a ``UserId`` in an invalid way::
Note that these checks are enforced only by the static type checker. At runtime,
the statement ``Derived = NewType('Derived', Base)`` will make ``Derived`` a
-function that immediately returns whatever parameter you pass it. That means
+class that immediately returns whatever parameter you pass it. That means
the expression ``Derived(some_value)`` does not create a new class or introduce
-any overhead beyond that of a regular function call.
+much overhead beyond that of a regular function call.
More precisely, the expression ``some_value is Derived(some_value)`` is always
true at runtime.
-This also means that it is not possible to create a subtype of ``Derived``
-since it is an identity function at runtime, not an actual type::
+It is invalid to create a subtype of ``Derived``::
from typing import NewType
@@ -123,7 +122,7 @@ since it is an identity function at runtime, not an actual type::
# Fails at runtime and does not typecheck
class AdminUserId(UserId): pass
-However, it is possible to create a :func:`NewType` based on a 'derived' ``NewType``::
+However, it is possible to create a :class:`NewType` based on a 'derived' ``NewType``::
from typing import NewType
@@ -151,6 +150,12 @@ See :pep:`484` for more details.
.. versionadded:: 3.5.2
+.. versionchanged:: 3.10.0
+ ``NewType`` is now a class rather than a function. There is some additional
+ runtime cost when calling ``NewType`` over a regular function. However, this
+ cost will be reduced in 3.11.0.
+
+
Callable
========
@@ -1306,17 +1311,21 @@ These are not used in annotations. They are building blocks for declaring types.
Removed the ``_field_types`` attribute in favor of the more
standard ``__annotations__`` attribute which has the same information.
-.. function:: NewType(name, tp)
+.. class:: NewType(name, tp)
- A helper function to indicate a distinct type to a typechecker,
- see :ref:`distinct`. At runtime it returns a function that returns
- its argument. Usage::
+ A helper class to indicate a distinct type to a typechecker,
+ see :ref:`distinct`. At runtime it returns an object that returns
+ its argument when called.
+ Usage::
UserId = NewType('UserId', int)
first_user = UserId(1)
.. versionadded:: 3.5.2
+ .. versionchanged:: 3.10.0
+ ``NewType`` is now a class rather than a function.
+
.. class:: TypedDict(dict)
Special construct to add type hints to a dictionary.