Backported and Experimental Type Hints for Python 3.7+
Project description
Typing Extensions
Overview
The typing_extensions
module serves two related purposes:
- Enable use of new type system features on older Python versions. For example,
typing.TypeGuard
is new in Python 3.10, buttyping_extensions
allows users on previous Python versions to use it too. - Enable experimentation with new type system PEPs before they are accepted and
added to the
typing
module.
New features may be added to typing_extensions
as soon as they are specified
in a PEP that has been added to the python/peps
repository. If the PEP is accepted, the feature will then be added to typing
for the next CPython release. No typing PEP has been rejected so far, so we
haven't yet figured out how to deal with that possibility.
Starting with version 4.0.0, typing_extensions
uses
Semantic Versioning. The
major version is incremented for all backwards-incompatible changes.
Therefore, it's safe to depend
on typing_extensions
like this: typing_extensions >=x.y, <(x+1)
,
where x.y
is the first version that includes all features you need.
typing_extensions
supports Python versions 3.7 and higher. In the future,
support for older Python versions will be dropped some time after that version
reaches end of life.
Included items
This module currently contains the following:
-
Experimental features
-
In
typing
since Python 3.11assert_never
assert_type
clear_overloads
@dataclass_transform()
(see PEP 681)get_overloads
LiteralString
(see PEP 675)Never
NotRequired
(see PEP 655)reveal_type
Required
(see PEP 655)Self
(see PEP 673)TypeVarTuple
(see PEP 646; thetyping_extensions
version supports thedefault=
argument from PEP 696)Unpack
(see PEP 646)
-
In
typing
since Python 3.10 -
In
typing
since Python 3.9Annotated
(see PEP 593)
-
In
typing
since Python 3.8 -
In
typing
since Python 3.7OrderedDict
-
In
typing
since Python 3.5 or 3.6 (see the typing documentation for details)AsyncContextManager
AsyncGenerator
AsyncIterable
AsyncIterator
Awaitable
ChainMap
ClassVar
(see PEP 526)ContextManager
Coroutine
Counter
DefaultDict
Deque
NewType
NoReturn
overload
Text
Type
TYPE_CHECKING
get_type_hints
-
The following have always been present in
typing
, but thetyping_extensions
versions provide additional features:
Other Notes and Limitations
Certain objects were changed after they were added to typing
, and
typing_extensions
provides a backport even on newer Python versions:
TypedDict
does not store runtime information about which (if any) keys are non-required in Python 3.8, and does not honor thetotal
keyword with old-styleTypedDict()
in Python 3.9.0 and 3.9.1.TypedDict
also does not support multiple inheritance withtyping.Generic
on Python <3.11.get_origin
andget_args
lack support forAnnotated
in Python 3.8 and lack support forParamSpecArgs
andParamSpecKwargs
in 3.9.@final
was changed in Python 3.11 to set the.__final__
attribute.@overload
was changed in Python 3.11 to make function overloads introspectable at runtime. In order to access overloads withtyping_extensions.get_overloads()
, you must use@typing_extensions.overload
.NamedTuple
was changed in Python 3.11 to allow for multiple inheritance withtyping.Generic
.- Since Python 3.11, it has been possible to inherit from
Any
at runtime.typing_extensions.Any
also provides this capability. TypeVar
gains two additional parameters,default=
andinfer_variance=
, in the draft PEPs 695 and 696, which are being considered for inclusion in Python 3.12.
There are a few types whose interface was modified between different
versions of typing. For example, typing.Sequence
was modified to
subclass typing.Reversible
as of Python 3.5.3.
These changes are not backported to prevent subtle compatibility issues when mixing the differing implementations of modified classes.
Certain types have incorrect runtime behavior due to limitations of older versions of the typing module:
ParamSpec
andConcatenate
will not work withget_args
andget_origin
. Certain PEP 612 special cases in user-definedGeneric
s are also not available.
These types are only guaranteed to work for static type checking.
Running tests
To run tests, navigate into the appropriate source directory and run
test_typing_extensions.py
.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for typing_extensions-4.5.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fb33085c39dd998ac16d1431ebc293a8b3eedd00fd4a32de0ff79002c19511b4 |
|
MD5 | 159820157d72b39382de7c13ff173897 |
|
BLAKE2b-256 | 31255abcd82372d3d4a3932e1fa8c3dbf9efac10cc7c0d16e78467460571b404 |