Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOCS: NewType can subclass NewType #3695

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions Doc/library/typing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@ 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. Similarly, it
is not possible to create another :func:`NewType` based on a ``Derived`` type::
since it is an identity function at runtime, not an actual type:

from typing import NewType

Expand All @@ -121,9 +120,16 @@ is not possible to create another :func:`NewType` based on a ``Derived`` type::
# Fails at runtime and does not typecheck
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)

and typechecking for ``ProUserId`` will work as expected.

See :pep:`484` for more details.

.. note::
Expand Down