Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PEP 681: Remove discussion of alternate form #2386

Merged
merged 1 commit into from
Mar 7, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 4 additions & 33 deletions pep-0681.rst
Original file line number Diff line number Diff line change
Expand Up @@ -532,36 +532,6 @@ decorated with ``dataclass_transform`` is in use.
`ignored by dataclass mechanisms <#class-var_>`_.


Alternate form
--------------

To avoid delaying adoption of this proposal until after
``dataclass_transform`` has been added to the ``typing`` module, type
checkers may support the alternative form ``__dataclass_transform__``.
This form can be defined locally without any reliance on the
``typing`` or ``typing_extensions`` modules, and allows immediate
adoption of this specification by library authors. Type checkers that
have not yet adopted this specification will retain their current
behavior.

To use this alternate form, library authors should include the
following declaration within their type stubs or source files:

.. code-block:: python

_T = TypeVar("_T")

def __dataclass_transform__(
*,
eq_default: bool = True,
order_default: bool = False,
kw_only_default: bool = False,
field_descriptors: tuple[type | Callable[..., Any], ...] = (),
) -> Callable[[_T], _T]:
# If used within a stub file, the following implementation can
# be replaced with "...".
return lambda a: a

Undefined behavior
------------------

Expand All @@ -573,13 +543,14 @@ behavior is undefined. Library authors should avoid these scenarios.
Reference Implementation
========================

The `Pyright <#pyright_>`_ type checker supports the
``__dataclass_transform__`` `alternate form`_. Pyright's
`Pyright <#pyright_>`_ contains the reference implementation of type
checker support for ``dataclass_transform``. Pyright's
``dataClasses.ts`` `source file <#pyright-impl_>`_ would be a good
starting point for understanding the implementation.

The `attrs <#attrs-usage_>`_ and `pydantic <#pydantic-usage_>`_
libraries are using the ``__dataclass_transform__`` `alternate form`_.
libraries are using ``dataclass_transform`` and serve as real-world
examples of its usage.


Rejected Ideas
Expand Down