Skip to content

Commit

Permalink
Reimplement TypedDict in a similar way to Python (#191)
Browse files Browse the repository at this point in the history
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
  • Loading branch information
JelleZijlstra and AlexWaygood authored Jun 9, 2023
1 parent da85974 commit d826561
Show file tree
Hide file tree
Showing 5 changed files with 392 additions and 145 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Unreleased

- Align the implementation of `TypedDict` with the implementation in the
standard library on Python 3.9 and higher.
`typing_extensions.TypedDict` is now a function instead of a class. The
private functions `_check_fails`, `_dict_new`, and `_typeddict_new`
have been removed. `is_typeddict` now returns `False` when called with
`TypedDict` itself as the argument. Patch by Jelle Zijlstra.
- Declare support for Python 3.12. Patch by Jelle Zijlstra.
- Fix tests on Python 3.13, which removes support for creating
`TypedDict` classes through the keyword-argument syntax. Patch by
Expand Down
12 changes: 12 additions & 0 deletions doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,12 @@ Special typing primitives

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

.. versionchanged:: 4.7.0

``TypedDict`` is now a function rather than a class.
This brings ``typing_extensions.TypedDict`` closer to the implementation
of :py:mod:`typing.TypedDict` on Python 3.9 and higher.

.. class:: TypeVar(name, *constraints, bound=None, covariant=False,
contravariant=False, infer_variance=False, default=...)

Expand Down Expand Up @@ -680,6 +686,12 @@ Functions

.. versionadded:: 4.1.0

.. versionchanged:: 4.7.0

:func:`is_typeddict` now returns ``False`` when called with
:data:`TypedDict` itself as the argument, consistent with the
behavior of :py:func:`typing.is_typeddict`.

.. function:: reveal_type(obj)

See :py:func:`typing.reveal_type`. In ``typing`` since 3.11.
Expand Down
6 changes: 5 additions & 1 deletion src/_typed_dict_test_helper.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

from typing import Generic, Optional, T
from typing_extensions import TypedDict
from typing_extensions import TypedDict, Annotated, Required


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

class FooGeneric(TypedDict, Generic[T]):
a: Optional[T]


class VeryAnnotated(TypedDict, total=False):
a: Annotated[Annotated[Annotated[Required[int], "a"], "b"], "c"]
Loading

0 comments on commit d826561

Please sign in to comment.