Debian Codegraph @

doc/index.rst

raw
1 .. module:: typing_extensions
2
3 Welcome to typing_extensions's documentation!
4 =============================================
5
6 ``typing_extensions`` complements the standard-library :py:mod:`typing` module,
7 providing runtime support for type hints as specified by :pep:`484` and subsequent
8 PEPs. The module serves two related purposes:
9
10 - Enable use of new type system features on older Python versions. For example,
11 :py:data:`typing.TypeGuard` is new in Python 3.10, but ``typing_extensions`` allows
12 users on previous Python versions to use it too.
13 - Enable experimentation with type system features proposed in new PEPs before they are accepted and
14 added to the :py:mod:`typing` module.
15
16 New features may be added to ``typing_extensions`` as soon as they are specified
17 in a PEP that has been added to the `python/peps <https://github.com/python/peps>`_
18 repository. If the PEP is accepted, the feature will then be added to the
19 :py:mod:`typing` module for the next CPython release. No typing PEP that
20 affected ``typing_extensions`` has been rejected so far, so we haven't yet
21 figured out how to deal with that possibility.
22
23 Bugfixes and new typing features that don't require a PEP may be added to
24 ``typing_extensions`` once they are merged into CPython's main branch.
25
26 ``typing_extensions`` also re-exports all names from the :py:mod:`typing` module,
27 including those that have always been present in the module. This allows users to
28 import names from ``typing_extensions`` without having to remember exactly when
29 each object was added to :py:mod:`typing`. There are a few exceptions:
30 :py:class:`typing.ByteString`, which is deprecated and due to be removed in Python
31 3.14, is not re-exported. Similarly, the ``typing.io`` and ``typing.re`` submodules,
32 which are removed in Python 3.13, are excluded.
33
34 Versioning and backwards compatibility
35 --------------------------------------
36
37 Starting with version 4.0.0, ``typing_extensions`` uses
38 `Semantic Versioning <https://semver.org>`_. A changelog is
39 maintained `on GitHub <https://github.com/python/typing_extensions/blob/main/CHANGELOG.md>`_.
40
41 The major version is incremented for all backwards-incompatible changes.
42 Therefore, it's safe to depend
43 on ``typing_extensions`` like this: ``typing_extensions >=x.y, <(x+1)``,
44 where ``x.y`` is the first version that includes all features you need.
45 In view of the wide usage of ``typing_extensions`` across the ecosystem,
46 we are highly hesitant to break backwards compatibility, and we do not
47 expect to increase the major version number in the foreseeable future.
48
49 Feature releases, with version numbers of the form 4.N.0, are made at
50 irregular intervals when enough new features accumulate. Before a
51 feature release, at least one release candidate (with a version number
52 of the form 4.N.0rc1) should be released to give downstream users time
53 to test. After at least a week of testing, the new feature version
54 may then be released. If necessary, additional release candidates can
55 be added.
56
57 Bugfix releases, with version numbers of the form 4.N.1 or higher,
58 may be made if bugs are discovered after a feature release.
59
60 We provide no backward compatibility guarantees for prereleases (e.g.,
61 release candidates) and for unreleased code in our Git repository.
62
63 Before version 4.0.0, the versioning scheme loosely followed the Python
64 version from which features were backported; for example,
65 ``typing_extensions`` 3.10.0.0 was meant to reflect ``typing`` as of
66 Python 3.10.0. During this period, no changelog was maintained.
67
68 In the documentation below, for each object added since version 4.0
69 there is a note indicating the version in which it was added. Objects
70 for which no such note is present have been present since before version 4.0.
71
72 Runtime use of types
73 ~~~~~~~~~~~~~~~~~~~~
74
75 We aim for complete backwards compatibility in terms of the names we export:
76 code like ``from typing_extensions import X`` that works on one
77 typing-extensions release will continue to work on the next.
78 It is more difficult to maintain compatibility for users that introspect
79 types at runtime, as almost any detail can potentially break compatibility.
80 Users who introspect types should follow these guidelines to minimize
81 the risk of compatibility issues:
82
83 - Always check for both the :mod:`typing` and ``typing_extensions`` versions
84 of objects, even if they are currently the same on some Python version.
85 Future ``typing_extensions`` releases may re-export a separate version of
86 the object to backport some new feature or bugfix.
87 - Use public APIs like :func:`get_origin` and :func:`get_original_bases` to
88 access internal information about types, instead of accessing private
89 attributes directly. If some information is not available through a public
90 attribute, consider opening an issue in CPython to add such an API.
91
92 Here is an example recipe for a general-purpose function that could be used for
93 reasonably performant runtime introspection of typing objects. The function
94 will be resilient against any potential changes in ``typing_extensions`` that
95 alter whether an object is reimplemented in ``typing_extensions``, rather than
96 simply being re-exported from the :mod:`typing` module::
97
98 import functools
99 import typing
100 import typing_extensions
101 from typing import Tuple, Any
102
103 # Use an unbounded cache for this function, for optimal performance
104 @functools.lru_cache(maxsize=None)
105 def get_typing_objects_by_name_of(name: str) -> Tuple[Any, ...]:
106 result = tuple(
107 getattr(module, name)
108 # You could potentially also include mypy_extensions here,
109 # if your library supports mypy_extensions
110 for module in (typing, typing_extensions)
111 if hasattr(module, name)
112 )
113 if not result:
114 raise ValueError(
115 f"Neither typing nor typing_extensions has an object called {name!r}"
116 )
117 return result
118
119
120 # Use a cache here as well, but make it a bounded cache
121 # (the default cache size is 128)
122 @functools.lru_cache()
123 def is_typing_name(obj: object, name: str) -> bool:
124 return any(obj is thing for thing in get_typing_objects_by_name_of(name))
125
126 Example usage::
127
128 >>> import typing, typing_extensions
129 >>> from functools import partial
130 >>> from typing_extensions import get_origin
131 >>> is_literal = partial(is_typing_name, name="Literal")
132 >>> is_literal(typing.Literal)
133 True
134 >>> is_literal(typing_extensions.Literal)
135 True
136 >>> is_literal(typing.Any)
137 False
138 >>> is_literal(get_origin(typing.Literal[42]))
139 True
140 >>> is_literal(get_origin(typing_extensions.Final[int]))
141 False
142
143 Python version support
144 ----------------------
145
146 ``typing_extensions`` currently supports Python versions 3.9 and higher. In the future,
147 support for older Python versions will be dropped some time after that version
148 reaches end of life.
149
150 Version compatibility table
151 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
152
153 The following table lists objects provided by ``typing_extensions`` that
154 backport or extend features from the standard library, along with the
155 Python version in which each feature was added to :py:mod:`typing` and the
156 ``typing_extensions`` release that first included it.
157
158 .. list-table::
159 :header-rows: 1
160 :widths: 25 15 20 15
161
162 * - Object
163 - In :mod:`typing` (Python)
164 - In ``typing_extensions``
165 - PEP
166 * - :data:`Annotated`
167 - 3.9
168 - 3.7.4
169 - :pep:`593`
170 * - :data:`Any` (as base class)
171 - 3.11
172 - 4.4.0
173 -
174 * - :class:`Buffer`
175 - 3.12
176 - 4.6.0
177 - :pep:`688`
178 * - :class:`CapsuleType`
179 - 3.13
180 - 4.12.0
181 -
182 * - :data:`Concatenate`
183 - 3.10
184 - 3.7.4
185 - :pep:`612`
186 * - :func:`dataclass_transform`
187 - 3.11
188 - 4.1.0
189 - :pep:`681`
190 * - :func:`deprecated`
191 - 3.13
192 - 4.5.0
193 - :pep:`702`
194 * - :func:`disjoint_base`
195 -
196 - 4.15.0
197 - :pep:`800`
198 * - :func:`evaluate_forward_ref`
199 - 3.14
200 - 4.13.0
201 -
202 * - :class:`Format`
203 -
204 - 4.13.0
205 - :pep:`649`
206 * - :func:`get_annotations`
207 - 3.10
208 - 4.13.0
209 - :pep:`649`
210 * - :func:`get_original_bases`
211 - 3.12
212 - 4.6.0
213 -
214 * - :func:`get_protocol_members`
215 - 3.13
216 - 4.7.0
217 -
218 * - :data:`Literal`
219 - 3.8
220 - 2.4.0
221 - :pep:`586`
222 * - :data:`LiteralString`
223 - 3.11
224 - 4.1.0
225 - :pep:`675`
226 * - :class:`NamedTuple` (generic)
227 - 3.11
228 - 4.3.0
229 -
230 * - :data:`Never`
231 - 3.11
232 - 4.1.0
233 -
234 * - :class:`NewType` (picklable)
235 - 3.10
236 - 4.6.0
237 -
238 * - :data:`NoDefault`
239 - 3.13
240 - 4.12.0
241 - :pep:`696`
242 * - :data:`NoExtraItems`
243 - 3.15
244 - 4.13.0
245 - :pep:`728`
246 * - :data:`NotRequired`
247 - 3.11
248 - 4.0.0
249 - :pep:`655`
250 * - :class:`ParamSpec`
251 - 3.10
252 - 3.7.4
253 - :pep:`612`
254 * - :class:`Protocol`
255 - 3.8
256 - 2.4.0
257 - :pep:`544`
258 * - :data:`ReadOnly`
259 - 3.13
260 - 4.9.0
261 - :pep:`705`
262 * - :data:`Required`
263 - 3.11
264 - 4.0.0
265 - :pep:`655`
266 * - :data:`Self`
267 - 3.11
268 - 4.0.0
269 - :pep:`673`
270 * - :class:`sentinel`
271 - 3.15
272 - 4.14.0 (originally under the name `Sentinel`)
273 - :pep:`661`
274 * - :class:`TypeAliasType`
275 - 3.12
276 - 4.6.0
277 - :pep:`695`
278 * - :data:`TypeForm`
279 -
280 - 4.13.0
281 - :pep:`747`
282 * - :data:`TypeGuard`
283 - 3.10
284 - 3.10
285 - :pep:`647`
286 * - :data:`TypeIs`
287 - 3.13
288 - 4.10.0
289 - :pep:`742`
290 * - :class:`TypeVar` (``default``/``infer_variance``)
291 - 3.12/3.13
292 - 4.4.0
293 - :pep:`695`, :pep:`696`
294 * - :class:`TypeVarTuple`
295 - 3.11
296 - 4.1.0
297 - :pep:`646`
298 * - :func:`assert_never`
299 - 3.11
300 - 4.1.0
301 -
302 * - :func:`assert_type`
303 - 3.11
304 - 4.2.0
305 -
306 * - :func:`get_overloads`
307 - 3.11
308 - 4.2.0
309 -
310 * - :func:`is_protocol`
311 - 3.13
312 - 4.7.0
313 -
314 * - :func:`is_typeddict`
315 - 3.10
316 - 4.1.0
317 -
318 * - :func:`reveal_type`
319 - 3.11
320 - 4.1.0
321 -
322 * - :func:`type_repr`
323 - 3.14
324 - 4.15.0
325 -
326
327 Module contents
328 ---------------
329
330 As most of the features in ``typing_extensions`` exist in :py:mod:`typing`
331 in newer versions of Python, the documentation here is brief and focuses
332 on aspects that are specific to ``typing_extensions``, such as limitations
333 on specific Python versions.
334
335 Special typing primitives
336 ~~~~~~~~~~~~~~~~~~~~~~~~~
337
338 .. data:: Annotated
339
340 See :py:data:`typing.Annotated` and :pep:`593`. In ``typing`` since 3.9.
341
342 .. versionchanged:: 4.1.0
343
344 ``Annotated`` can now wrap :data:`ClassVar` and :data:`Final`.
345
346 .. data:: Any
347
348 See :py:data:`typing.Any`.
349
350 Since Python 3.11, ``typing.Any`` can be used as a base class.
351 ``typing_extensions.Any`` supports this feature on older versions.
352
353 .. versionadded:: 4.4.0
354
355 Added to support inheritance from ``Any``.
356
357 .. data:: Concatenate
358
359 See :py:data:`typing.Concatenate` and :pep:`612`. In ``typing`` since 3.10.
360
361 The backport does not support certain operations involving ``...`` as
362 a parameter; see :issue:`48` and :pr:`481` for details.
363
364 .. data:: Final
365
366 See :py:data:`typing.Final` and :pep:`591`. In ``typing`` since 3.8.
367
368 .. data:: Literal
369
370 See :py:data:`typing.Literal` and :pep:`586`. In ``typing`` since 3.8.
371
372 :py:data:`typing.Literal` does not flatten or deduplicate parameters on Python <3.9.1, and a
373 caching bug was fixed in 3.10.1/3.9.8. The ``typing_extensions`` version
374 flattens and deduplicates parameters on all Python versions, and the caching
375 bug is also fixed on all versions.
376
377 .. versionchanged:: 4.6.0
378
379 Backported the bug fixes from :pr-cpy:`29334`, :pr-cpy:`23294`, and :pr-cpy:`23383`.
380
381 .. data:: LiteralString
382
383 See :py:data:`typing.LiteralString` and :pep:`675`. In ``typing`` since 3.11.
384
385 .. versionadded:: 4.1.0
386
387 .. class:: NamedTuple
388
389 See :py:class:`typing.NamedTuple`.
390
391 ``typing_extensions`` backports several changes
392 to ``NamedTuple`` on Python 3.11 and lower: in 3.11,
393 support for generic ``NamedTuple``\ s was added, and
394 in 3.12, the ``__orig_bases__`` attribute was added.
395
396 .. versionadded:: 4.3.0
397
398 Added to provide support for generic ``NamedTuple``\ s.
399
400 .. versionchanged:: 4.6.0
401
402 Support for the ``__orig_bases__`` attribute was added.
403
404 .. versionchanged:: 4.7.0
405
406 The undocumented keyword argument syntax for creating NamedTuple classes
407 (``NT = NamedTuple("NT", x=int)``) is deprecated, and will be disallowed
408 in Python 3.15. Use the class-based syntax or the functional syntax instead.
409
410 .. versionchanged:: 4.7.0
411
412 When using the functional syntax to create a NamedTuple class, failing to
413 pass a value to the 'fields' parameter (``NT = NamedTuple("NT")``) is
414 deprecated. Passing ``None`` to the 'fields' parameter
415 (``NT = NamedTuple("NT", None)``) is also deprecated. Both will be
416 disallowed in Python 3.15. To create a NamedTuple class with zero fields,
417 use ``class NT(NamedTuple): pass`` or ``NT = NamedTuple("NT", [])``.
418
419
420 .. data:: Never
421
422 See :py:data:`typing.Never`. In ``typing`` since 3.11.
423
424 .. versionadded:: 4.1.0
425
426 .. class:: NewType(name, tp)
427
428 See :py:class:`typing.NewType`. In ``typing`` since 3.5.2.
429
430 Instances of ``NewType`` were made picklable in 3.10 and an error message was
431 improved in 3.11; ``typing_extensions`` backports these changes.
432
433 .. versionchanged:: 4.6.0
434
435 The improvements from Python 3.10 and 3.11 were backported.
436
437 .. data:: NoDefault
438
439 See :py:data:`typing.NoDefault`. In ``typing`` since 3.13.
440
441 .. versionadded:: 4.12.0
442
443 .. data:: NoExtraItems
444
445 A sentinel used when the ``extra_items`` class argument to :class:`TypedDict` is not
446 provided. In ``typing`` since 3.15.
447
448 .. versionadded:: 4.13.0
449
450 .. data:: NotRequired
451
452 See :py:data:`typing.NotRequired` and :pep:`655`. In ``typing`` since 3.11.
453
454 .. versionadded:: 4.0.0
455
456 .. class:: ParamSpec(name, *, bound=None, covariant=False,
457 contravariant=False, infer_variance=False, default=NoDefault)
458
459 See :py:class:`typing.ParamSpec` and :pep:`612`. In ``typing`` since 3.10.
460
461 The ``typing_extensions`` version adds support for the
462 ``default=`` argument from :pep:`696`.
463
464 On older Python versions, ``typing_extensions.ParamSpec`` may not work
465 correctly with introspection tools like :func:`get_args` and
466 :func:`get_origin`. Certain special cases in user-defined
467 :py:class:`typing.Generic`\ s are also not available (e.g., see :issue:`126`).
468
469 .. versionchanged:: 4.4.0
470
471 Added support for the ``default=`` argument.
472
473 .. versionchanged:: 4.6.0
474
475 The implementation was changed for compatibility with Python 3.12.
476
477 .. versionchanged:: 4.8.0
478
479 Passing an ellipsis literal (``...``) to *default* now works on Python
480 3.10 and lower.
481
482 .. versionchanged:: 4.12.0
483
484 The :attr:`!__default__` attribute is now set to ``None`` if
485 ``default=None`` is passed, and to :data:`NoDefault` if no value is passed.
486
487 Previously, passing ``None`` would result in :attr:`!__default__` being set
488 to :py:class:`types.NoneType`, and passing no value for the parameter would
489 result in :attr:`!__default__` being set to ``None``.
490
491 .. versionchanged:: 4.12.0
492
493 ParamSpecs now have a ``has_default()`` method, for compatibility
494 with :py:class:`typing.ParamSpec` on Python 3.13+.
495
496 .. versionchanged:: 4.16.0
497
498 The ``infer_variance``, ``covariant``, and ``contravariant`` arguments are now
499 officially supported and their validation is improved.
500
501 .. class:: ParamSpecArgs
502 ParamSpecKwargs
503
504 See :py:class:`typing.ParamSpecArgs` and :py:class:`typing.ParamSpecKwargs`.
505 In ``typing`` since 3.10.
506
507 .. class:: Protocol
508
509 See :py:class:`typing.Protocol` and :pep:`544`. In ``typing`` since 3.8.
510
511 Python 3.12 improves the performance of runtime-checkable protocols;
512 ``typing_extensions`` backports this improvement.
513
514 .. versionchanged:: 4.6.0
515
516 Backported the ability to define ``__init__`` methods on Protocol classes.
517
518 .. versionchanged:: 4.6.0
519
520 Backported changes to runtime-checkable protocols from Python 3.12,
521 including :pr-cpy:`103034` and :pr-cpy:`26067`.
522
523 .. versionchanged:: 4.7.0
524
525 Classes can now inherit from both :py:class:`typing.Protocol` and
526 ``typing_extensions.Protocol`` simultaneously. Previously, this led to
527 :py:exc:`TypeError` being raised due to a metaclass conflict.
528
529 It is recommended to avoid doing this if possible. Not all features and
530 bugfixes that ``typing_extensions.Protocol`` backports from newer Python
531 versions are guaranteed to work if :py:class:`typing.Protocol` is also
532 present in a protocol class's :py:term:`method resolution order`. See
533 :issue:`245` for some examples.
534
535 .. data:: ReadOnly
536
537 See :py:data:`typing.ReadOnly` and :pep:`705`. In ``typing`` since 3.13.
538
539 Indicates that a :class:`TypedDict` item may not be modified.
540
541 .. versionadded:: 4.9.0
542
543 .. data:: Required
544
545 See :py:data:`typing.Required` and :pep:`655`. In ``typing`` since 3.11.
546
547 .. versionadded:: 4.0.0
548
549 .. data:: Self
550
551 See :py:data:`typing.Self` and :pep:`673`. In ``typing`` since 3.11.
552
553 .. versionadded:: 4.0.0
554
555 .. data:: TypeAlias
556
557 See :py:data:`typing.TypeAlias` and :pep:`613`. In ``typing`` since 3.10.
558
559 .. class:: TypeAliasType(name, value, *, type_params=())
560
561 See :py:class:`typing.TypeAliasType` and :pep:`695`. In ``typing`` since 3.12.
562
563 .. versionadded:: 4.6.0
564
565 .. data:: TypeForm
566
567 See :pep:`747`. A special form representing the value of a type expression.
568
569 .. versionadded:: 4.13.0
570
571 .. data:: TypeGuard
572
573 See :py:data:`typing.TypeGuard` and :pep:`647`. In ``typing`` since 3.10.
574
575 .. data:: TypeIs
576
577 See :py:data:`typing.TypeIs` and :pep:`742`. In ``typing`` since 3.13.
578
579 Similar to :data:`TypeGuard`, but allows more type narrowing.
580
581 .. versionadded:: 4.10.0
582
583 .. class:: TypedDict(dict, total=True, closed=False, extra_items=<no extra items>)
584
585 See :py:class:`typing.TypedDict` and :pep:`589`. In ``typing`` since 3.8, but
586 changed and enhanced in several ways since then.
587 ``typing_extensions`` backports various bug fixes and improvements
588 to ``TypedDict``.
589
590 Features backported by `typing_extensions` include:
591
592 * :py:class:`typing.TypedDict` does not store runtime information about which
593 (if any) keys are non-required in Python 3.8, and does not honor the
594 *total* keyword with old-style ``TypedDict()`` on Python 3.9.0 and 3.9.1.
595 * :py:class:`typing.TypedDict` does not support multiple inheritance with
596 :py:class:`typing.Generic` on Python <3.11
597 * :py:class:`typing.TypedDict` classes do not consistently have the
598 :attr:`!__orig_bases__` attribute on Python <3.12.
599 * :py:class:`typing.TypedDict` does not expose the
600 :py:attr:`~typing.TypedDict.__readonly_keys__` and
601 :py:attr:`~typing.TypedDict.__mutable_keys__` attributes on Python <3.13.
602 * :py:class:`typing.TypedDict` does not support the *extra_items* or *closed*
603 keyword arguments on Python <3.15, and also does not expose the
604 :py:attr:`~typing.TypedDict.__extra_items__` and
605 :py:attr:`~typing.TypedDict.__closed__` attributes on Python <3.15.
606
607 ``typing_extensions`` also currently includes experimental support for
608 :pep:`764` (inline ``TypedDict``\ s), which is not yet supported in the
609 standard library on any Python version.
610
611 Historically, ``TypedDict`` has supported an alternative creation syntax
612 where the fields are supplied as keyword arguments (e.g.,
613 ``TypedDict("TD", a=int, b=str)``). In CPython, this feature was deprecated
614 in Python 3.11 and removed in Python 3.13. ``typing_extensions.TypedDict``
615 raises a :py:exc:`DeprecationWarning` when this syntax is used in Python 3.12
616 or lower and fails with a :py:exc:`TypeError` in Python 3.13 and higher.
617
618 Introspection attributes
619 ------------------------
620
621 ``typing_extensions`` supports the :data:`ReadOnly` qualifier
622 introduced by :pep:`705`. It is reflected in the following attributes:
623
624 .. attribute:: __readonly_keys__
625
626 A :py:class:`frozenset` containing the names of all read-only keys. Keys
627 are read-only if they carry the :data:`ReadOnly` qualifier.
628
629 .. versionadded:: 4.9.0
630
631 .. attribute:: __mutable_keys__
632
633 A :py:class:`frozenset` containing the names of all mutable keys. Keys
634 are mutable if they do not carry the :data:`ReadOnly` qualifier.
635
636 .. versionadded:: 4.9.0
637
638 The *closed* and *extra_items* keyword arguments introduced by
639 :pep:`728` and supported in Python 3.15 and newer are supported.
640
641 For runtime introspection, two attributes can be looked at:
642
643 .. attribute:: __closed__
644
645 A boolean flag indicating whether the current ``TypedDict`` is
646 considered closed. This reflects the ``closed`` class argument.
647
648 .. versionadded:: 4.10.0
649
650 .. attribute:: __extra_items__
651
652 The type of the extra items allowed on the ``TypedDict``.
653 This attribute defaults to :data:`NoExtraItems` if the ``extra_items``
654 class argument is not provided.
655
656 .. versionadded:: 4.10.0
657
658 History
659 -------
660
661 .. versionchanged:: 4.3.0
662
663 Added support for generic ``TypedDict``\ s.
664
665 .. versionchanged:: 4.6.0
666
667 A :py:exc:`DeprecationWarning` is now emitted when a call-based
668 ``TypedDict`` is constructed using keyword arguments.
669
670 .. versionchanged:: 4.6.0
671
672 Support for the ``__orig_bases__`` attribute was added.
673
674 .. versionchanged:: 4.7.0
675
676 ``TypedDict`` is now a function rather than a class.
677 This brings ``typing_extensions.TypedDict`` closer to the implementation
678 of :py:class:`typing.TypedDict` on Python 3.9 and higher.
679
680 .. versionchanged:: 4.7.0
681
682 When using the functional syntax to create a TypedDict class, failing to
683 pass a value to the 'fields' parameter (``TD = TypedDict("TD")``) is
684 deprecated. Passing ``None`` to the 'fields' parameter
685 (``TD = TypedDict("TD", None)``) is also deprecated. Both will be
686 disallowed in Python 3.15. To create a TypedDict class with 0 fields,
687 use ``class TD(TypedDict): pass`` or ``TD = TypedDict("TD", {})``.
688
689 .. versionchanged:: 4.9.0
690
691 Support for the :data:`ReadOnly` qualifier was added.
692
693 .. versionchanged:: 4.10.0
694
695 The keyword argument ``closed`` and the special key ``__extra_items__``
696 when ``closed=True`` is given were supported.
697
698 .. versionchanged:: 4.13.0
699
700 :pep:`728` support was updated to a newer version. Extra items are now
701 indicated with an ``extra_items`` class argument, not a special key
702 ``__extra_items__``.
703
704 A value assigned to ``__total__`` in the class body of a
705 ``TypedDict`` will be overwritten by the ``total`` argument of the
706 ``TypedDict`` constructor.
707
708 .. class:: TypeVar(name, *constraints, bound=None, covariant=False,
709 contravariant=False, infer_variance=False, default=NoDefault)
710
711 See :py:class:`typing.TypeVar`.
712
713 The ``typing_extensions`` version adds support for the
714 ``default=`` argument from :pep:`696`, as well as the
715 ``infer_variance=`` argument from :pep:`695` (also available
716 in Python 3.12).
717
718 .. versionadded:: 4.4.0
719
720 Added in order to support the new ``default=`` and
721 ``infer_variance=`` arguments.
722
723 .. versionchanged:: 4.6.0
724
725 The implementation was changed for compatibility with Python 3.12.
726
727 .. versionchanged:: 4.12.0
728
729 The :attr:`!__default__` attribute is now set to ``None`` if
730 ``default=None`` is passed, and to :data:`NoDefault` if no value is passed.
731
732 Previously, passing ``None`` would result in :attr:`!__default__` being set
733 to :py:class:`types.NoneType`, and passing no value for the parameter would
734 result in :attr:`!__default__` being set to ``None``.
735
736 .. versionchanged:: 4.12.0
737
738 TypeVars now have a ``has_default()`` method, for compatibility
739 with :py:class:`typing.TypeVar` on Python 3.13+.
740
741 .. class:: TypeVarTuple(name, *, bound=None, covariant=False,
742 contravariant=False, infer_variance=False, default=NoDefault)
743
744 See :py:class:`typing.TypeVarTuple` and :pep:`646`. In ``typing`` since 3.11.
745
746 The ``typing_extensions`` version adds support for the ``default=`` argument
747 from :pep:`696`, and for the ``bound=``, ``infer_variance=``, ``covariant=``
748 and ``contravariant=`` arguments that were added in Python 3.15.
749
750 .. versionadded:: 4.1.0
751
752 .. versionchanged:: 4.4.0
753
754 Added support for the ``default=`` argument.
755
756 .. versionchanged:: 4.6.0
757
758 The implementation was changed for compatibility with Python 3.12.
759
760 .. versionchanged:: 4.12.0
761
762 The :attr:`!__default__` attribute is now set to ``None`` if
763 ``default=None`` is passed, and to :data:`NoDefault` if no value is passed.
764
765 Previously, passing ``None`` would result in :attr:`!__default__` being set
766 to :py:class:`types.NoneType`, and passing no value for the parameter would
767 result in :attr:`!__default__` being set to ``None``.
768
769 .. versionchanged:: 4.12.0
770
771 TypeVarTuples now have a ``has_default()`` method, for compatibility
772 with :py:class:`typing.TypeVarTuple` on Python 3.13+.
773
774 .. versionchanged:: 4.12.0
775
776 It is now disallowed to use a `TypeVar` with a default value after a
777 `TypeVarTuple` in a type parameter list. This matches the CPython
778 implementation of PEP 696 on Python 3.13+.
779
780 .. versionchanged:: 4.16.0
781
782 The ``bound``, ``infer_variance``, ``covariant``, and ``contravariant``
783 arguments are now supported.
784
785 .. data:: Unpack
786
787 See :py:data:`typing.Unpack` and :pep:`646`. In ``typing`` since 3.11.
788
789 In Python 3.12, the ``repr()`` was changed as a result of :pep:`692`.
790 ``typing_extensions`` backports this change.
791
792 Generic type aliases involving ``Unpack`` may not work correctly on
793 Python 3.10 and lower; see :issue:`103` for details.
794
795 .. versionadded:: 4.1.0
796
797 .. versionchanged:: 4.6.0
798
799 Backport ``repr()`` changes from Python 3.12.
800
801 Abstract Base Classes
802 ~~~~~~~~~~~~~~~~~~~~~
803
804 .. class:: Buffer
805
806 See :py:class:`collections.abc.Buffer`. Added to the standard library
807 in Python 3.12.
808
809 .. versionadded:: 4.6.0
810
811 Protocols
812 ~~~~~~~~~
813
814 .. class:: SupportsAbs
815
816 See :py:class:`typing.SupportsAbs`.
817
818 ``typing_extensions`` backports a more performant version of this
819 protocol on Python 3.11 and lower.
820
821 .. versionadded:: 4.6.0
822
823 .. class:: SupportsBytes
824
825 See :py:class:`typing.SupportsBytes`.
826
827 ``typing_extensions`` backports a more performant version of this
828 protocol on Python 3.11 and lower.
829
830 .. versionadded:: 4.6.0
831
832 .. class:: SupportsComplex
833
834 See :py:class:`typing.SupportsComplex`.
835
836 ``typing_extensions`` backports a more performant version of this
837 protocol on Python 3.11 and lower.
838
839 .. versionadded:: 4.6.0
840
841 .. class:: SupportsFloat
842
843 See :py:class:`typing.SupportsFloat`.
844
845 ``typing_extensions`` backports a more performant version of this
846 protocol on Python 3.11 and lower.
847
848 .. versionadded:: 4.6.0
849
850 .. class:: SupportsIndex
851
852 See :py:class:`typing.SupportsIndex`. In ``typing`` since 3.8.
853
854 ``typing_extensions`` backports a more performant version of this
855 protocol on Python 3.11 and lower.
856
857 .. versionchanged:: 4.6.0
858
859 Backported the performance improvements from Python 3.12.
860
861 .. class:: SupportsInt
862
863 See :py:class:`typing.SupportsInt`.
864
865 ``typing_extensions`` backports a more performant version of this
866 protocol on Python 3.11 and lower.
867
868 .. versionadded:: 4.6.0
869
870 .. class:: SupportsRound
871
872 See :py:class:`typing.SupportsRound`.
873
874 ``typing_extensions`` backports a more performant version of this
875 protocol on Python 3.11 and lower.
876
877 .. versionadded:: 4.6.0
878
879 .. class:: Reader
880
881 See :py:class:`io.Reader`. Added to the standard library in Python 3.14.
882
883 .. versionadded:: 4.14.0
884
885 .. class:: Writer
886
887 See :py:class:`io.Writer`. Added to the standard library in Python 3.14.
888
889 .. versionadded:: 4.14.0
890
891 Decorators
892 ~~~~~~~~~~
893
894 .. decorator:: dataclass_transform(*, eq_default=False, order_default=False, kw_only_default=False, frozen_default=False, field_specifiers=(), **kwargs)
895
896 See :py:func:`typing.dataclass_transform` and :pep:`681`. In ``typing`` since 3.11.
897
898 Python 3.12 adds the ``frozen_default`` parameter; ``typing_extensions``
899 backports this parameter.
900
901 .. versionadded:: 4.1.0
902
903 .. versionchanged:: 4.2.0
904
905 The ``field_descriptors`` parameter was renamed to ``field_specifiers``.
906 For compatibility, the decorator now accepts arbitrary keyword arguments.
907
908 .. versionchanged:: 4.5.0
909
910 The ``frozen_default`` parameter was added.
911
912 .. decorator:: deprecated(msg, *, category=DeprecationWarning, stacklevel=1)
913
914 See :py:func:`warnings.deprecated` and :pep:`702`. In the :mod:`warnings` module
915 since Python 3.13.
916
917 .. versionadded:: 4.5.0
918
919 .. versionchanged:: 4.9.0
920
921 Inheriting from a deprecated class now also raises a runtime
922 :py:exc:`DeprecationWarning`.
923
924 .. decorator:: disjoint_base
925
926 See :pep:`800`. A class decorator that marks a class as a "disjoint base", meaning that
927 child classes of the decorated class cannot inherit from other disjoint bases that are not
928 parent classes of the decorated class.
929
930 This helps type checkers to detect unreachable code and to understand when two types
931 can overlap.
932
933 .. versionadded:: 4.15.0
934
935 .. decorator:: final
936
937 See :py:func:`typing.final` and :pep:`591`. In ``typing`` since 3.8.
938
939 Since Python 3.11, this decorator supports runtime introspection
940 by setting the ``__final__`` attribute wherever possible; ``typing_extensions.final``
941 backports this feature.
942
943 .. versionchanged:: 4.1.0
944
945 The decorator now attempts to set the ``__final__`` attribute on decorated objects.
946
947 .. decorator:: overload
948
949 See :py:func:`typing.overload`.
950
951 Since Python 3.11, this decorator supports runtime introspection
952 through :func:`get_overloads`; ``typing_extensions.overload``
953 backports this feature.
954
955 .. versionchanged:: 4.2.0
956
957 Introspection support via :func:`get_overloads` was added.
958
959 .. decorator:: override
960
961 See :py:func:`typing.override` and :pep:`698`. In ``typing`` since 3.12.
962
963 .. versionadded:: 4.4.0
964
965 .. versionchanged:: 4.5.0
966
967 The decorator now attempts to set the ``__override__`` attribute on the decorated
968 object.
969
970 .. decorator:: runtime_checkable
971
972 See :py:func:`typing.runtime_checkable`. In ``typing`` since 3.8.
973
974 In Python 3.12, the performance of runtime-checkable protocols was
975 improved, and ``typing_extensions`` backports these performance
976 improvements.
977
978 Functions
979 ~~~~~~~~~
980
981 .. function:: assert_never(arg)
982
983 See :py:func:`typing.assert_never`. In ``typing`` since 3.11.
984
985 .. versionadded:: 4.1.0
986
987 .. function:: assert_type(val, typ)
988
989 See :py:func:`typing.assert_type`. In ``typing`` since 3.11.
990
991 .. versionadded:: 4.2.0
992
993 .. function:: clear_overloads()
994
995 See :py:func:`typing.clear_overloads`. In ``typing`` since 3.11.
996
997 .. versionadded:: 4.2.0
998
999 .. function:: evaluate_forward_ref(forward_ref, *, owner=None, globals=None, locals=None, type_params=None, format=None)
1000
1001 Evaluate an :py:class:`typing.ForwardRef` as a :py:term:`type hint`.
1002
1003 This is similar to calling :py:meth:`annotationlib.ForwardRef.evaluate`,
1004 but unlike that method, :func:`!evaluate_forward_ref` also:
1005
1006 * Recursively evaluates forward references nested within the type hint.
1007 However, the amount of recursion is limited in Python 3.8 and 3.10.
1008 * Raises :exc:`TypeError` when it encounters certain objects that are
1009 not valid type hints.
1010 * Replaces type hints that evaluate to :const:`!None` with
1011 :class:`types.NoneType`.
1012 * Supports the :attr:`Format.FORWARDREF` and
1013 :attr:`Format.STRING` formats.
1014
1015 *forward_ref* must be an instance of :py:class:`typing.ForwardRef`.
1016 *owner*, if given, should be the object that holds the annotations that
1017 the forward reference derived from, such as a module, class object, or function.
1018 It is used to infer the namespaces to use for looking up names.
1019 *globals* and *locals* can also be explicitly given to provide
1020 the global and local namespaces.
1021 *type_params* is a tuple of :py:ref:`type parameters <type-params>` that
1022 are in scope when evaluating the forward reference.
1023 This parameter must be provided (though it may be an empty tuple) if *owner*
1024 is not given and the forward reference does not already have an owner set.
1025 *format* specifies the format of the annotation and is a member of
1026 the :class:`Format` enum, defaulting to :attr:`Format.VALUE`.
1027
1028 .. caution::
1029
1030 This function may execute arbitrary code contained in annotations.
1031 See :ref:`annotations-security` for more information.
1032
1033 .. versionadded:: 4.13.0
1034
1035 .. function:: get_annotations(obj, *, globals=None, locals=None, eval_str=False, format=Format.VALUE)
1036
1037 See :py:func:`inspect.get_annotations`. In the standard library since Python 3.10.
1038
1039 ``typing_extensions`` adds the keyword argument ``format``, as specified
1040 by :pep:`649`. The supported formats are listed in the :class:`Format` enum.
1041 The default format, :attr:`Format.VALUE`, behaves the same across all versions.
1042 For the other two formats, ``typing_extensions`` provides a rough approximation
1043 of the :pep:`649` behavior on versions of Python that do not support it.
1044
1045 The purpose of this backport is to allow users who would like to use
1046 :attr:`Format.FORWARDREF` or :attr:`Format.STRING` semantics once
1047 :pep:`649` is implemented, but who also
1048 want to support earlier Python versions, to simply write::
1049
1050 typing_extensions.get_annotations(obj, format=Format.FORWARDREF)
1051
1052 .. caution::
1053
1054 This function may execute arbitrary code contained in annotations.
1055 See :ref:`annotations-security` for more information.
1056
1057 .. versionadded:: 4.13.0
1058
1059 .. function:: get_args(tp)
1060
1061 See :py:func:`typing.get_args`. In ``typing`` since 3.8.
1062
1063 This function was changed in 3.9 and 3.10 to deal with :data:`Annotated`
1064 and :class:`ParamSpec` correctly; ``typing_extensions`` backports these
1065 fixes.
1066
1067 .. function:: get_origin(tp)
1068
1069 See :py:func:`typing.get_origin`. In ``typing`` since 3.8.
1070
1071 This function was changed in 3.9 and 3.10 to deal with :data:`Annotated`
1072 and :class:`ParamSpec` correctly; ``typing_extensions`` backports these
1073 fixes.
1074
1075 .. function:: get_original_bases(cls)
1076
1077 See :py:func:`types.get_original_bases`. Added to the standard library
1078 in Python 3.12.
1079
1080 This function should always produce correct results when called on classes
1081 constructed using features from ``typing_extensions``. However, it may
1082 produce incorrect results when called on some :py:class:`NamedTuple` or
1083 :py:class:`TypedDict` classes on Python <=3.11.
1084
1085 .. versionadded:: 4.6.0
1086
1087 .. function:: get_overloads(func)
1088
1089 See :py:func:`typing.get_overloads`. In ``typing`` since 3.11.
1090
1091 Before Python 3.11, this works only with overloads created through
1092 :func:`overload`, not with :py:func:`typing.overload`.
1093
1094 .. versionadded:: 4.2.0
1095
1096 .. function:: get_protocol_members(tp)
1097
1098 See :py:func:`typing.get_protocol_members`. In ``typing`` since 3.13.
1099
1100 Return the set of members defined in a :class:`Protocol`. This works with protocols
1101 defined using either :class:`typing.Protocol` or :class:`typing_extensions.Protocol`.
1102
1103 ::
1104
1105 >>> from typing_extensions import Protocol, get_protocol_members
1106 >>> class P(Protocol):
1107 ... def a(self) -> str: ...
1108 ... b: int
1109 >>> get_protocol_members(P)
1110 frozenset({'a', 'b'})
1111
1112 Raise :py:exc:`TypeError` for arguments that are not Protocols.
1113
1114 .. versionadded:: 4.7.0
1115
1116 .. function:: get_type_hints(obj, globalns=None, localns=None, include_extras=False)
1117
1118 See :py:func:`typing.get_type_hints`.
1119
1120 In Python 3.11, this function was changed to support the new
1121 :py:data:`typing.Required` and :py:data:`typing.NotRequired`.
1122 ``typing_extensions`` backports these fixes.
1123
1124 .. caution::
1125
1126 This function may execute arbitrary code contained in annotations.
1127 See :ref:`annotations-security` for more information.
1128
1129 .. versionchanged:: 4.1.0
1130
1131 Interaction with :data:`Required` and :data:`NotRequired`.
1132
1133 .. versionchanged:: 4.11.0
1134
1135 When ``include_extra=False``, ``get_type_hints()`` now strips
1136 :data:`ReadOnly` from the annotation.
1137
1138 .. function:: is_protocol(tp)
1139
1140 See :py:func:`typing.is_protocol`. In ``typing`` since 3.13.
1141
1142 Determine if a type is a :class:`Protocol`. This works with protocols
1143 defined using either :py:class:`typing.Protocol` or :class:`typing_extensions.Protocol`.
1144
1145 For example::
1146
1147 class P(Protocol):
1148 def a(self) -> str: ...
1149 b: int
1150
1151 is_protocol(P) # => True
1152 is_protocol(int) # => False
1153
1154 .. versionadded:: 4.7.0
1155
1156 .. function:: is_typeddict(tp)
1157
1158 See :py:func:`typing.is_typeddict`. In ``typing`` since 3.10.
1159
1160 On versions where :class:`TypedDict` is not the same as
1161 :py:class:`typing.TypedDict`, this function recognizes
1162 ``TypedDict`` classes created through either mechanism.
1163
1164 .. versionadded:: 4.1.0
1165
1166 .. versionchanged:: 4.7.0
1167
1168 :func:`is_typeddict` now returns ``False`` when called with
1169 :data:`TypedDict` itself as the argument, consistent with the
1170 behavior of :py:func:`typing.is_typeddict`.
1171
1172 .. function:: reveal_type(obj)
1173
1174 See :py:func:`typing.reveal_type`. In ``typing`` since 3.11.
1175
1176 .. versionadded:: 4.1.0
1177
1178 .. function:: type_repr(value)
1179
1180 See :py:func:`annotationlib.type_repr`. In ``annotationlib`` since 3.14.
1181
1182 Convert an arbitrary Python value to a format suitable for use by
1183 the :attr:`Format.STRING`.
1184
1185 .. versionadded:: 4.15.0
1186
1187 Enums
1188 ~~~~~
1189
1190 .. class:: Format
1191
1192 The formats for evaluating annotations introduced by :pep:`649`.
1193 Members of this enum can be passed as the *format* argument
1194 to :func:`get_annotations`.
1195
1196 The final place of this enum in the standard library has not yet
1197 been determined (see :pep:`649` and :pep:`749`), but the names
1198 and integer values are stable and will continue to work.
1199
1200 .. attribute:: VALUE
1201
1202 Equal to 1. The default value. The function will return the conventional Python values
1203 for the annotations. This format is identical to the return value for
1204 the function under earlier versions of Python.
1205
1206 .. attribute:: VALUE_WITH_FAKE_GLOBALS
1207
1208 Equal to 2. Special value used to signal that an annotate function is being
1209 evaluated in a special environment with fake globals. When passed this
1210 value, annotate functions should either return the same value as for
1211 the :attr:`Format.VALUE` format, or raise :exc:`NotImplementedError`
1212 to signal that they do not support execution in this environment.
1213 This format is only used internally and should not be passed to
1214 the functions in this module.
1215
1216 .. attribute:: FORWARDREF
1217
1218 Equal to 3. When :pep:`649` is implemented, this format will attempt to return the
1219 conventional Python values for the annotations. However, if it encounters
1220 an undefined name, it dynamically creates a proxy object (a ForwardRef)
1221 that substitutes for that value in the expression.
1222
1223 ``typing_extensions`` emulates this value on versions of Python which do
1224 not support :pep:`649` by returning the same value as for ``VALUE`` semantics.
1225
1226 .. attribute:: STRING
1227
1228 Equal to 4. When :pep:`649` is implemented, this format will produce an annotation
1229 dictionary where the values have been replaced by strings containing
1230 an approximation of the original source code for the annotation expressions.
1231
1232 ``typing_extensions`` emulates this by evaluating the annotations using
1233 ``VALUE`` semantics and then stringifying the results.
1234
1235 .. versionadded:: 4.13.0
1236
1237 Annotation metadata
1238 ~~~~~~~~~~~~~~~~~~~
1239
1240 .. class:: Doc(documentation, /)
1241
1242 Define the documentation of a type annotation using :data:`Annotated`, to be
1243 used in class attributes, function and method parameters, return values,
1244 and variables.
1245
1246 The value should be a positional-only string literal to allow static tools
1247 like editors and documentation generators to use it.
1248
1249 This complements docstrings.
1250
1251 The string value passed is available in the attribute ``documentation``.
1252
1253 Example::
1254
1255 >>> from typing_extensions import Annotated, Doc
1256 >>> def hi(to: Annotated[str, Doc("Who to say hi to")]) -> None: ...
1257
1258 .. versionadded:: 4.8.0
1259
1260 See :pep:`727`.
1261
1262 .. attribute:: documentation
1263
1264 The documentation string passed to :class:`Doc`.
1265
1266
1267 Capsule objects
1268 ~~~~~~~~~~~~~~~
1269
1270 .. class:: CapsuleType
1271
1272 The type of :py:ref:`capsule objects <capsules>`.
1273 See :py:class:`types.CapsuleType`, where it has existed since Python 3.13.
1274
1275 Note that this may not exist on all implementations of Python; it is only
1276 guaranteed to exist on CPython.
1277
1278 .. versionadded:: 4.12.0
1279
1280
1281 Sentinel objects
1282 ~~~~~~~~~~~~~~~~
1283
1284 .. class:: sentinel(name, /, *, repr=None)
1285
1286 A type used to define sentinel values. The *name* argument should be the
1287 name of the variable to which the return value shall be assigned.
1288
1289 Assigning attributes to a sentinel is deprecated (except for __module__).
1290
1291 Example::
1292
1293 >>> from typing_extensions import sentinel, assert_type
1294 >>> MISSING = sentinel('MISSING')
1295 >>> def func(arg: int | MISSING = MISSING) -> None:
1296 ... if arg is MISSING:
1297 ... assert_type(arg, MISSING)
1298 ... else:
1299 ... assert_type(arg, int)
1300 ...
1301 >>> func(MISSING)
1302
1303 .. versionadded:: 4.14.0
1304
1305 See :pep:`661`
1306
1307 .. versionchanged:: 4.16.0
1308
1309 The implementation of this class has been updated to conform to
1310 the accepted version of :pep:`661`:
1311
1312 - Sentinels can now be pickled. They are reduced as singletons, which
1313 means that they also preserve their identity when copied or deep-copied.
1314 - `Sentinel` has been renamed to `sentinel`. `Sentinel` is retained as a
1315 soft-deprecated alias, for backwards compatibility.
1316 - The default repr of a sentinel `X = sentinel("X")` is now `X`, rather
1317 than `<X>`.
1318 - Passing `repr` as a positional argument to the constructor is deprecated.
1319 - Passing `name` as a keyword argument to the constructor is deprecated.
1320 - Subclassing `sentinel` is deprecated.
1321 - Assigning arbitrary attributes to a sentinel is deprecated.
1322
1323
1324 Pure aliases
1325 ~~~~~~~~~~~~
1326
1327 Most of these are simply re-exported from the :mod:`typing` module on all supported
1328 versions of Python, but all are listed here for completeness.
1329
1330 .. class:: AbstractSet
1331
1332 See :py:class:`typing.AbstractSet`.
1333
1334 .. versionadded:: 4.7.0
1335
1336 .. data:: AnyStr
1337
1338 See :py:data:`typing.AnyStr`.
1339
1340 .. versionadded:: 4.7.0
1341
1342 .. class:: AsyncContextManager
1343
1344 See :py:class:`typing.AsyncContextManager`. In ``typing`` since 3.5.4 and 3.6.2.
1345
1346 .. versionchanged:: 4.12.0
1347
1348 ``AsyncContextManager`` now has an optional second parameter, defaulting to
1349 ``Optional[bool]``, signifying the return type of the ``__aexit__`` method.
1350
1351 .. class:: AsyncGenerator
1352
1353 See :py:class:`typing.AsyncGenerator`. In ``typing`` since 3.6.1.
1354
1355 .. versionchanged:: 4.12.0
1356
1357 The second type parameter is now optional (it defaults to ``None``).
1358
1359 .. class:: AsyncIterable
1360
1361 See :py:class:`typing.AsyncIterable`. In ``typing`` since 3.5.2.
1362
1363 .. class:: AsyncIterator
1364
1365 See :py:class:`typing.AsyncIterator`. In ``typing`` since 3.5.2.
1366
1367 .. class:: Awaitable
1368
1369 See :py:class:`typing.Awaitable`. In ``typing`` since 3.5.2.
1370
1371 .. class:: BinaryIO
1372
1373 See :py:class:`typing.BinaryIO`.
1374
1375 .. versionadded:: 4.7.0
1376
1377 .. data:: Callable
1378
1379 See :py:data:`typing.Callable`.
1380
1381 .. versionadded:: 4.7.0
1382
1383 .. class:: ChainMap
1384
1385 See :py:class:`typing.ChainMap`. In ``typing`` since 3.5.4 and 3.6.1.
1386
1387 .. data:: ClassVar
1388
1389 See :py:data:`typing.ClassVar` and :pep:`526`. In ``typing`` since 3.5.3.
1390
1391 .. class:: Collection
1392
1393 See :py:class:`typing.Collection`.
1394
1395 .. versionadded:: 4.7.0
1396
1397 .. class:: Container
1398
1399 See :py:class:`typing.Container`.
1400
1401 .. versionadded:: 4.7.0
1402
1403 .. class:: ContextManager
1404
1405 See :py:class:`typing.ContextManager`. In ``typing`` since 3.5.4.
1406
1407 .. versionchanged:: 4.12.0
1408
1409 ``ContextManager`` now has an optional second parameter, defaulting to
1410 ``Optional[bool]``, signifying the return type of the ``__exit__`` method.
1411
1412 .. class:: Coroutine
1413
1414 See :py:class:`typing.Coroutine`. In ``typing`` since 3.5.3.
1415
1416 .. class:: Counter
1417
1418 See :py:class:`typing.Counter`. In ``typing`` since 3.5.4 and 3.6.1.
1419
1420 .. class:: DefaultDict
1421
1422 See :py:class:`typing.DefaultDict`. In ``typing`` since 3.5.2.
1423
1424 .. class:: Deque
1425
1426 See :py:class:`typing.Deque`. In ``typing`` since 3.5.4 and 3.6.1.
1427
1428 .. class:: Dict
1429
1430 See :py:class:`typing.Dict`.
1431
1432 .. versionadded:: 4.7.0
1433
1434 .. class:: ForwardRef
1435
1436 See :py:class:`typing.ForwardRef`.
1437
1438 .. versionadded:: 4.7.0
1439
1440 .. class:: FrozenSet
1441
1442 See :py:class:`typing.FrozenSet`.
1443
1444 .. versionadded:: 4.7.0
1445
1446 .. class:: Generator
1447
1448 See :py:class:`typing.Generator`.
1449
1450 .. versionadded:: 4.7.0
1451
1452 .. versionchanged:: 4.12.0
1453
1454 The second type and third type parameters are now optional
1455 (they both default to ``None``).
1456
1457 .. class:: Generic
1458
1459 See :py:class:`typing.Generic`.
1460
1461 .. versionadded:: 4.7.0
1462
1463 .. class:: Hashable
1464
1465 See :py:class:`typing.Hashable`.
1466
1467 .. versionadded:: 4.7.0
1468
1469 .. class:: IO
1470
1471 See :py:class:`typing.IO`.
1472
1473 .. versionadded:: 4.7.0
1474
1475 .. class:: ItemsView
1476
1477 See :py:class:`typing.ItemsView`.
1478
1479 .. versionadded:: 4.7.0
1480
1481 .. class:: Iterable
1482
1483 See :py:class:`typing.Iterable`.
1484
1485 .. versionadded:: 4.7.0
1486
1487 .. class:: Iterator
1488
1489 See :py:class:`typing.Iterator`.
1490
1491 .. versionadded:: 4.7.0
1492
1493 .. class:: KeysView
1494
1495 See :py:class:`typing.KeysView`.
1496
1497 .. versionadded:: 4.7.0
1498
1499 .. class:: List
1500
1501 See :py:class:`typing.List`.
1502
1503 .. versionadded:: 4.7.0
1504
1505 .. class:: Mapping
1506
1507 See :py:class:`typing.Mapping`.
1508
1509 .. versionadded:: 4.7.0
1510
1511 .. class:: MappingView
1512
1513 See :py:class:`typing.MappingView`.
1514
1515 .. versionadded:: 4.7.0
1516
1517 .. class:: Match
1518
1519 See :py:class:`typing.Match`.
1520
1521 .. versionadded:: 4.7.0
1522
1523 .. class:: MutableMapping
1524
1525 See :py:class:`typing.MutableMapping`.
1526
1527 .. versionadded:: 4.7.0
1528
1529 .. class:: MutableSequence
1530
1531 See :py:class:`typing.MutableSequence`.
1532
1533 .. versionadded:: 4.7.0
1534
1535 .. class:: MutableSet
1536
1537 See :py:class:`typing.MutableSet`.
1538
1539 .. versionadded:: 4.7.0
1540
1541 .. data:: NoReturn
1542
1543 See :py:data:`typing.NoReturn`. In ``typing`` since 3.5.4 and 3.6.2.
1544
1545 .. data:: Optional
1546
1547 See :py:data:`typing.Optional`.
1548
1549 .. versionadded:: 4.7.0
1550
1551 .. class:: OrderedDict
1552
1553 See :py:class:`typing.OrderedDict`. In ``typing`` since 3.7.2.
1554
1555 .. class:: Pattern
1556
1557 See :py:class:`typing.Pattern`.
1558
1559 .. versionadded:: 4.7.0
1560
1561 .. class:: Reversible
1562
1563 See :py:class:`typing.Reversible`.
1564
1565 .. versionadded:: 4.7.0
1566
1567 .. class:: Sequence
1568
1569 See :py:class:`typing.Sequence`.
1570
1571 .. versionadded:: 4.7.0
1572
1573 .. class:: Set
1574
1575 See :py:class:`typing.Set`.
1576
1577 .. versionadded:: 4.7.0
1578
1579 .. class:: Sized
1580
1581 See :py:class:`typing.Sized`.
1582
1583 .. versionadded:: 4.7.0
1584
1585 .. class:: Text
1586
1587 See :py:class:`typing.Text`. In ``typing`` since 3.5.2.
1588
1589 .. class:: TextIO
1590
1591 See :py:class:`typing.TextIO`.
1592
1593 .. versionadded:: 4.7.0
1594
1595 .. data:: Tuple
1596
1597 See :py:data:`typing.Tuple`.
1598
1599 .. versionadded:: 4.7.0
1600
1601 .. class:: Type
1602
1603 See :py:class:`typing.Type`. In ``typing`` since 3.5.2.
1604
1605 .. data:: TYPE_CHECKING
1606
1607 See :py:data:`typing.TYPE_CHECKING`. In ``typing`` since 3.5.2.
1608
1609 .. data:: Union
1610
1611 See :py:class:`typing.Union`.
1612
1613 .. versionadded:: 4.7.0
1614
1615 .. class:: ValuesView
1616
1617 See :py:class:`typing.ValuesView`.
1618
1619 .. versionadded:: 4.7.0
1620
1621 .. function:: cast
1622
1623 See :py:func:`typing.cast`.
1624
1625 .. versionadded:: 4.7.0
1626
1627 .. decorator:: no_type_check
1628
1629 See :py:func:`typing.no_type_check`.
1630
1631 .. versionadded:: 4.7.0
1632
1633 .. decorator:: no_type_check_decorator
1634
1635 See :py:func:`typing.no_type_check_decorator`.
1636
1637 .. versionadded:: 4.7.0
1638
1639 Security
1640 --------
1641
1642 ``typing_extensions`` is among the most widely used packages in the
1643 Python ecosystem. Therefore, we take security seriously and strive
1644 to use a transparent, secure release process.
1645
1646 We commit to the following in order to keep the package secure in the
1647 future:
1648
1649 * ``typing_extensions`` will never include any native extensions, only
1650 pure Python code.
1651 * ``typing_extensions`` will not have any third-party dependencies.
1652 * We will follow best practices for a secure release process.
1653
1654 If you have any feedback on our security process, please `open an issue
1655 <https://github.com/python/typing_extensions/issues/new>`__. To report
1656 an issue privately, use `GitHub's private reporting feature
1657 <https://github.com/python/typing_extensions/security>`__.
1658
1659 .. _annotations-security:
1660
1661 Introspection of annotations
1662 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1663
1664 Some functions in this module are designed to introspect annotations at
1665 runtime. These functions may therefore execute code contained in annotations,
1666 which can then perform arbitrary operations. For example,
1667 :func:`get_annotations` may call an arbitrary :term:`annotate function`, and
1668 :meth:`evaluate_forward_ref` may call :func:`eval` on an arbitrary string. Code contained
1669 in an annotation might make arbitrary system calls, enter an infinite loop, or perform any
1670 other operation. This is also true for any access of the :attr:`~object.__annotations__` attribute
1671 (as of Python 3.14),
1672 and for various functions in the :mod:`typing` module that work with annotations, such as
1673 :func:`typing.get_type_hints`.
1674
1675 Any security issue arising from this also applies immediately after importing
1676 code that may contain untrusted annotations: importing code can always cause arbitrary operations
1677 to be performed. However, it is unsafe to accept strings or other input from an untrusted source and
1678 pass them to any of the APIs for introspecting annotations, for example by editing an
1679 ``__annotations__`` dictionary or directly creating a :class:`ForwardRef` object.