mirror of
https://github.com/python/cpython.git
synced 2025-09-30 12:21:51 +00:00
bpo-31564: Update typing documentation (GH-3696) (GH-3715)
Mention that ``NewType`` can derive from another ``NewType``.
(cherry picked from commit 039b25d8fd
)
This commit is contained in:
parent
7e32cee1a6
commit
018e6b9f69
1 changed files with 9 additions and 3 deletions
|
@ -111,8 +111,7 @@ More precisely, the expression ``some_value is Derived(some_value)`` is always
|
||||||
true at runtime.
|
true at runtime.
|
||||||
|
|
||||||
This also means that it is not possible to create a subtype of ``Derived``
|
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. Similarly, it
|
since it is an identity function at runtime, not an actual type::
|
||||||
is not possible to create another :func:`NewType` based on a ``Derived`` type::
|
|
||||||
|
|
||||||
from typing import NewType
|
from typing import NewType
|
||||||
|
|
||||||
|
@ -121,9 +120,16 @@ is not possible to create another :func:`NewType` based on a ``Derived`` type::
|
||||||
# Fails at runtime and does not typecheck
|
# Fails at runtime and does not typecheck
|
||||||
class AdminUserId(UserId): pass
|
class AdminUserId(UserId): pass
|
||||||
|
|
||||||
# Also does not typecheck
|
However, it is possible to create a :func:`NewType` based on a 'derived' ``NewType``::
|
||||||
|
|
||||||
|
from typing import NewType
|
||||||
|
|
||||||
|
UserId = NewType('UserId', int)
|
||||||
|
|
||||||
ProUserId = NewType('ProUserId', UserId)
|
ProUserId = NewType('ProUserId', UserId)
|
||||||
|
|
||||||
|
and typechecking for ``ProUserId`` will work as expected.
|
||||||
|
|
||||||
See :pep:`484` for more details.
|
See :pep:`484` for more details.
|
||||||
|
|
||||||
.. note::
|
.. note::
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue