Skip to content

Commit d826561

Browse files
Reimplement TypedDict in a similar way to Python (#191)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
1 parent da85974 commit d826561

File tree

5 files changed

+392
-145
lines changed

5 files changed

+392
-145
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Unreleased
22

3+
- Align the implementation of `TypedDict` with the implementation in the
4+
standard library on Python 3.9 and higher.
5+
`typing_extensions.TypedDict` is now a function instead of a class. The
6+
private functions `_check_fails`, `_dict_new`, and `_typeddict_new`
7+
have been removed. `is_typeddict` now returns `False` when called with
8+
`TypedDict` itself as the argument. Patch by Jelle Zijlstra.
39
- Declare support for Python 3.12. Patch by Jelle Zijlstra.
410
- Fix tests on Python 3.13, which removes support for creating
511
`TypedDict` classes through the keyword-argument syntax. Patch by

doc/index.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,12 @@ Special typing primitives
347347

348348
Support for the ``__orig_bases__`` attribute was added.
349349

350+
.. versionchanged:: 4.7.0
351+
352+
``TypedDict`` is now a function rather than a class.
353+
This brings ``typing_extensions.TypedDict`` closer to the implementation
354+
of :py:mod:`typing.TypedDict` on Python 3.9 and higher.
355+
350356
.. class:: TypeVar(name, *constraints, bound=None, covariant=False,
351357
contravariant=False, infer_variance=False, default=...)
352358

@@ -680,6 +686,12 @@ Functions
680686

681687
.. versionadded:: 4.1.0
682688

689+
.. versionchanged:: 4.7.0
690+
691+
:func:`is_typeddict` now returns ``False`` when called with
692+
:data:`TypedDict` itself as the argument, consistent with the
693+
behavior of :py:func:`typing.is_typeddict`.
694+
683695
.. function:: reveal_type(obj)
684696

685697
See :py:func:`typing.reveal_type`. In ``typing`` since 3.11.

src/_typed_dict_test_helper.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
from typing import Generic, Optional, T
4-
from typing_extensions import TypedDict
4+
from typing_extensions import TypedDict, Annotated, Required
55

66

77
# this class must not be imported into test_typing_extensions.py at top level, otherwise
@@ -16,3 +16,7 @@ class Foo(TypedDict):
1616

1717
class FooGeneric(TypedDict, Generic[T]):
1818
a: Optional[T]
19+
20+
21+
class VeryAnnotated(TypedDict, total=False):
22+
a: Annotated[Annotated[Annotated[Required[int], "a"], "b"], "c"]

0 commit comments

Comments
 (0)