Re-export all names from typing (#196)
* Re-export all names from typing
Fixes #50.
I decided to exclude top-level names that have a definite removal
timeline (io, re, ByteString); there's no point in re-exporting them
now.
Many other names are deprecated without a definite removal plan
(PEP 585). If they do get removed eventually, typing-extensions can
simply re-export the builtin/collections.abc names.
In the documentation, I add a separate section listing all the names
that are pure aliases for typing. This makes it easier to distinguish
the cases where there is something more interesting going on in
typing-extensions.
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5526ef9..2df212b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,8 @@
# Unreleased
+- `typing_extensions` now re-exports all names in the standard library's
+ `typing` module, except the deprecated `ByteString`. Patch by Jelle
+ Zijlstra.
- Due to changes in the implementation of `typing_extensions.Protocol`,
`typing.runtime_checkable` can now be used on `typing_extensions.Protocol`
(previously, users had to use `typing_extensions.runtime_checkable` if they
diff --git a/doc/index.rst b/doc/index.rst
index 6049737..8d1f9a8 100644
--- a/doc/index.rst
+++ b/doc/index.rst
@@ -22,6 +22,14 @@
Bugfixes and new typing features that don't require a PEP may be added to
``typing_extensions`` once they are merged into CPython's main branch.
+``typing_extensions`` also re-exports all names from the :py:mod:`typing` module,
+including those that have always been present in the module. This allows users to
+import names from ``typing_extensions`` without having to remember exactly when
+each object was added to :py:mod:`typing`. There are a few exceptions:
+:py:class:`typing.ByteString`, which is deprecated and due to be removed in Python
+3.14, is not re-exported. Similarly, the ``typing.io`` and ``typing.re`` submodules,
+which are removed in Python 3.13, are excluded.
+
Versioning and backwards compatibility
--------------------------------------
@@ -161,10 +169,6 @@
Added to support inheritance from ``Any``.
-.. data:: ClassVar
-
- See :py:data:`typing.ClassVar` and :pep:`526`. In ``typing`` since 3.5.3.
-
.. data:: Concatenate
See :py:data:`typing.Concatenate` and :pep:`612`. In ``typing`` since 3.10.
@@ -229,10 +233,6 @@
The improvements from Python 3.10 and 3.11 were backported.
-.. data:: NoReturn
-
- See :py:data:`typing.NoReturn`. In ``typing`` since 3.5.4 and 3.6.2.
-
.. data:: NotRequired
See :py:data:`typing.NotRequired` and :pep:`655`. In ``typing`` since 3.11.
@@ -294,10 +294,6 @@
.. versionadded:: 4.0.0
-.. class:: Type
-
- See :py:class:`typing.Type`. In ``typing`` since 3.5.2.
-
.. data:: TypeAlias
See :py:data:`typing.TypeAlias` and :pep:`613`. In ``typing`` since 3.10.
@@ -408,22 +404,6 @@
Generic concrete collections
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-.. class:: ChainMap
-
- See :py:class:`typing.ChainMap`. In ``typing`` since 3.5.4 and 3.6.1.
-
-.. class:: Counter
-
- See :py:class:`typing.Counter`. In ``typing`` since 3.5.4 and 3.6.1.
-
-.. class:: DefaultDict
-
- See :py:class:`typing.DefaultDict`. In ``typing`` since 3.5.2.
-
-.. class:: Deque
-
- See :py:class:`typing.Deque`. In ``typing`` since 3.5.4 and 3.6.1.
-
.. class:: OrderedDict
See :py:class:`typing.OrderedDict`. In ``typing`` since 3.7.2.
@@ -431,26 +411,6 @@
Abstract Base Classes
~~~~~~~~~~~~~~~~~~~~~
-.. class:: AsyncContextManager
-
- See :py:class:`typing.AsyncContextManager`. In ``typing`` since 3.5.4 and 3.6.2.
-
-.. class:: AsyncGenerator
-
- See :py:class:`typing.AsyncGenerator`. In ``typing`` since 3.6.1.
-
-.. class:: AsyncIterable
-
- See :py:class:`typing.AsyncIterable`. In ``typing`` since 3.5.2.
-
-.. class:: AsyncIterator
-
- See :py:class:`typing.AsyncIterator`. In ``typing`` since 3.5.2.
-
-.. class:: Awaitable
-
- See :py:class:`typing.Awaitable`. In ``typing`` since 3.5.2.
-
.. class:: Buffer
See :py:class:`collections.abc.Buffer`. Added to the standard library
@@ -458,14 +418,6 @@
.. versionadded:: 4.6.0
-.. class:: ContextManager
-
- See :py:class:`typing.ContextManager`. In ``typing`` since 3.5.4.
-
-.. class:: Coroutine
-
- See :py:class:`typing.Coroutine`. In ``typing`` since 3.5.3.
-
Protocols
~~~~~~~~~
@@ -698,13 +650,294 @@
.. versionadded:: 4.1.0
-Other
-~~~~~
+Pure aliases
+~~~~~~~~~~~~
+
+These are simply re-exported from the :mod:`typing` module on all supported
+versions of Python. They are listed here for completeness.
+
+.. class:: AbstractSet
+
+ See :py:class:`typing.AbstractSet`.
+
+ .. versionadded:: 4.7.0
+
+.. data:: AnyStr
+
+ See :py:data:`typing.AnyStr`.
+
+ .. versionadded:: 4.7.0
+
+.. class:: AsyncContextManager
+
+ See :py:class:`typing.AsyncContextManager`. In ``typing`` since 3.5.4 and 3.6.2.
+
+.. class:: AsyncGenerator
+
+ See :py:class:`typing.AsyncGenerator`. In ``typing`` since 3.6.1.
+
+.. class:: AsyncIterable
+
+ See :py:class:`typing.AsyncIterable`. In ``typing`` since 3.5.2.
+
+.. class:: AsyncIterator
+
+ See :py:class:`typing.AsyncIterator`. In ``typing`` since 3.5.2.
+
+.. class:: Awaitable
+
+ See :py:class:`typing.Awaitable`. In ``typing`` since 3.5.2.
+
+.. class:: BinaryIO
+
+ See :py:class:`typing.BinaryIO`.
+
+ .. versionadded:: 4.7.0
+
+.. data:: Callable
+
+ See :py:data:`typing.Callable`.
+
+ .. versionadded:: 4.7.0
+
+.. class:: ChainMap
+
+ See :py:class:`typing.ChainMap`. In ``typing`` since 3.5.4 and 3.6.1.
+
+.. data:: ClassVar
+
+ See :py:data:`typing.ClassVar` and :pep:`526`. In ``typing`` since 3.5.3.
+
+.. class:: Collection
+
+ See :py:class:`typing.Collection`.
+
+ .. versionadded:: 4.7.0
+
+.. class:: Container
+
+ See :py:class:`typing.Container`.
+
+ .. versionadded:: 4.7.0
+
+.. class:: ContextManager
+
+ See :py:class:`typing.ContextManager`. In ``typing`` since 3.5.4.
+
+.. class:: Coroutine
+
+ See :py:class:`typing.Coroutine`. In ``typing`` since 3.5.3.
+
+.. class:: Counter
+
+ See :py:class:`typing.Counter`. In ``typing`` since 3.5.4 and 3.6.1.
+
+.. class:: DefaultDict
+
+ See :py:class:`typing.DefaultDict`. In ``typing`` since 3.5.2.
+
+.. class:: Deque
+
+ See :py:class:`typing.Deque`. In ``typing`` since 3.5.4 and 3.6.1.
+
+.. class:: Dict
+
+ See :py:class:`typing.Dict`.
+
+ .. versionadded:: 4.7.0
+
+.. class:: ForwardRef
+
+ See :py:class:`typing.ForwardRef`.
+
+ .. versionadded:: 4.7.0
+
+.. class:: FrozenSet
+
+ See :py:class:`typing.FrozenSet`.
+
+ .. versionadded:: 4.7.0
+
+.. class:: Generator
+
+ See :py:class:`typing.Generator`.
+
+ .. versionadded:: 4.7.0
+
+.. class:: Generic
+
+ See :py:class:`typing.Generic`.
+
+ .. versionadded:: 4.7.0
+
+.. class:: Hashable
+
+ See :py:class:`typing.Hashable`.
+
+ .. versionadded:: 4.7.0
+
+.. class:: IO
+
+ See :py:class:`typing.IO`.
+
+ .. versionadded:: 4.7.0
+
+.. class:: ItemsView
+
+ See :py:class:`typing.ItemsView`.
+
+ .. versionadded:: 4.7.0
+
+.. class:: Iterable
+
+ See :py:class:`typing.Iterable`.
+
+ .. versionadded:: 4.7.0
+
+.. class:: Iterator
+
+ See :py:class:`typing.Iterator`.
+
+ .. versionadded:: 4.7.0
+
+.. class:: KeysView
+
+ See :py:class:`typing.KeysView`.
+
+ .. versionadded:: 4.7.0
+
+.. class:: List
+
+ See :py:class:`typing.List`.
+
+ .. versionadded:: 4.7.0
+
+.. class:: Mapping
+
+ See :py:class:`typing.Mapping`.
+
+ .. versionadded:: 4.7.0
+
+.. class:: MappingView
+
+ See :py:class:`typing.MappingView`.
+
+ .. versionadded:: 4.7.0
+
+.. class:: Match
+
+ See :py:class:`typing.Match`.
+
+ .. versionadded:: 4.7.0
+
+.. class:: MutableMapping
+
+ See :py:class:`typing.MutableMapping`.
+
+ .. versionadded:: 4.7.0
+
+.. class:: MutableSequence
+
+ See :py:class:`typing.MutableSequence`.
+
+ .. versionadded:: 4.7.0
+
+.. class:: MutableSet
+
+ See :py:class:`typing.MutableSet`.
+
+ .. versionadded:: 4.7.0
+
+.. data:: NoReturn
+
+ See :py:data:`typing.NoReturn`. In ``typing`` since 3.5.4 and 3.6.2.
+
+.. data:: Optional
+
+ See :py:data:`typing.Optional`.
+
+ .. versionadded:: 4.7.0
+
+.. class:: Pattern
+
+ See :py:class:`typing.Pattern`.
+
+ .. versionadded:: 4.7.0
+
+.. class:: Reversible
+
+ See :py:class:`typing.Reversible`.
+
+ .. versionadded:: 4.7.0
+
+.. class:: Sequence
+
+ See :py:class:`typing.Sequence`.
+
+ .. versionadded:: 4.7.0
+
+.. class:: Set
+
+ See :py:class:`typing.Set`.
+
+ .. versionadded:: 4.7.0
+
+.. class:: Sized
+
+ See :py:class:`typing.Sized`.
+
+ .. versionadded:: 4.7.0
.. class:: Text
See :py:class:`typing.Text`. In ``typing`` since 3.5.2.
+.. class:: TextIO
+
+ See :py:class:`typing.TextIO`.
+
+ .. versionadded:: 4.7.0
+
+.. data:: Tuple
+
+ See :py:data:`typing.Tuple`.
+
+ .. versionadded:: 4.7.0
+
+.. class:: Type
+
+ See :py:class:`typing.Type`. In ``typing`` since 3.5.2.
+
.. data:: TYPE_CHECKING
See :py:data:`typing.TYPE_CHECKING`. In ``typing`` since 3.5.2.
+
+.. data:: Union
+
+ See :py:data:`typing.Union`.
+
+ .. versionadded:: 4.7.0
+
+.. class:: ValuesView
+
+ See :py:class:`typing.ValuesView`.
+
+ .. versionadded:: 4.7.0
+
+.. function:: cast
+
+ See :py:func:`typing.cast`.
+
+ .. versionadded:: 4.7.0
+
+.. decorator:: no_type_check
+
+ See :py:func:`typing.no_type_check`.
+
+ .. versionadded:: 4.7.0
+
+.. decorator:: no_type_check_decorator
+
+ See :py:func:`typing.no_type_check_decorator`.
+
+ .. versionadded:: 4.7.0
diff --git a/src/test_typing_extensions.py b/src/test_typing_extensions.py
index 71b70b7..102f0c1 100644
--- a/src/test_typing_extensions.py
+++ b/src/test_typing_extensions.py
@@ -4683,6 +4683,20 @@
class AllTests(BaseTestCase):
+ def test_drop_in_for_typing(self):
+ # Check that the typing_extensions.__all__ is a superset of
+ # typing.__all__.
+ t_all = set(typing.__all__)
+ te_all = set(typing_extensions.__all__)
+ exceptions = {"ByteString"}
+ self.assertGreaterEqual(te_all, t_all - exceptions)
+ # Deprecated, to be removed in 3.14
+ self.assertFalse(hasattr(typing_extensions, "ByteString"))
+ # These were never included in `typing.__all__`,
+ # and have been removed in Python 3.13
+ self.assertNotIn('re', te_all)
+ self.assertNotIn('io', te_all)
+
def test_typing_extensions_includes_standard(self):
a = typing_extensions.__all__
self.assertIn('ClassVar', a)
diff --git a/src/typing_extensions.py b/src/typing_extensions.py
index 21fa282..449ea5e 100644
--- a/src/typing_extensions.py
+++ b/src/typing_extensions.py
@@ -85,6 +85,45 @@
'NoReturn',
'Required',
'NotRequired',
+
+ # Pure aliases, have always been in typing
+ 'AbstractSet',
+ 'AnyStr',
+ 'BinaryIO',
+ 'Callable',
+ 'Collection',
+ 'Container',
+ 'Dict',
+ 'ForwardRef',
+ 'FrozenSet',
+ 'Generator',
+ 'Generic',
+ 'Hashable',
+ 'IO',
+ 'ItemsView',
+ 'Iterable',
+ 'Iterator',
+ 'KeysView',
+ 'List',
+ 'Mapping',
+ 'MappingView',
+ 'Match',
+ 'MutableMapping',
+ 'MutableSequence',
+ 'MutableSet',
+ 'Optional',
+ 'Pattern',
+ 'Reversible',
+ 'Sequence',
+ 'Set',
+ 'Sized',
+ 'TextIO',
+ 'Tuple',
+ 'Union',
+ 'ValuesView',
+ 'cast',
+ 'no_type_check',
+ 'no_type_check_decorator',
]
# for backward compatibility
@@ -2837,3 +2876,46 @@
if not _is_unionable(left):
return NotImplemented
return typing.Union[left, self]
+
+
+# Aliases for items that have always been in typing.
+# Explicitly assign these (rather than using `from typing import *` at the top),
+# so that we get a CI error if one of these is deleted from typing.py
+# in a future version of Python
+AbstractSet = typing.AbstractSet
+AnyStr = typing.AnyStr
+BinaryIO = typing.BinaryIO
+Callable = typing.Callable
+Collection = typing.Collection
+Container = typing.Container
+Dict = typing.Dict
+ForwardRef = typing.ForwardRef
+FrozenSet = typing.FrozenSet
+Generator = typing.Generator
+Generic = typing.Generic
+Hashable = typing.Hashable
+IO = typing.IO
+ItemsView = typing.ItemsView
+Iterable = typing.Iterable
+Iterator = typing.Iterator
+KeysView = typing.KeysView
+List = typing.List
+Mapping = typing.Mapping
+MappingView = typing.MappingView
+Match = typing.Match
+MutableMapping = typing.MutableMapping
+MutableSequence = typing.MutableSequence
+MutableSet = typing.MutableSet
+Optional = typing.Optional
+Pattern = typing.Pattern
+Reversible = typing.Reversible
+Sequence = typing.Sequence
+Set = typing.Set
+Sized = typing.Sized
+TextIO = typing.TextIO
+Tuple = typing.Tuple
+Union = typing.Union
+ValuesView = typing.ValuesView
+cast = typing.cast
+no_type_check = typing.no_type_check
+no_type_check_decorator = typing.no_type_check_decorator