Skip to content

Commit 64365d0

Browse files
gh-91860: Add docs for typing.dataclass_transform field specifier params (GH-94354) (GH-94372)
(cherry picked from commit 81ac9ac) Co-authored-by: Erik De Bonte <erikd@microsoft.com>
1 parent cf64db6 commit 64365d0

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

Doc/library/typing.rst

+35-1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ annotations. These include:
8484
*Introducing* :data:`Self`
8585
* :pep:`675`: Arbitrary Literal String Type
8686
*Introducing* :data:`LiteralString`
87+
* :pep:`681`: Data Class Transforms
88+
*Introducing* the :func:`@dataclass_transform<dataclass_transform>` decorator
8789

8890
.. _type-aliases:
8991

@@ -2528,7 +2530,17 @@ Functions and decorators
25282530
For example, type checkers will assume these classes have
25292531
``__init__`` methods that accept ``id`` and ``name``.
25302532

2531-
The arguments to this decorator can be used to customize this behavior:
2533+
The decorated class, metaclass, or function may accept the following bool
2534+
arguments which type checkers will assume have the same effect as they
2535+
would have on the
2536+
:func:`@dataclasses.dataclass<dataclasses.dataclass>` decorator: ``init``,
2537+
``eq``, ``order``, ``unsafe_hash``, ``frozen``, ``match_args``,
2538+
``kw_only``, and ``slots``. It must be possible for the value of these
2539+
arguments (``True`` or ``False``) to be statically evaluated.
2540+
2541+
The arguments to the ``dataclass_transform`` decorator can be used to
2542+
customize the default behaviors of the decorated class, metaclass, or
2543+
function:
25322544

25332545
* ``eq_default`` indicates whether the ``eq`` parameter is assumed to be
25342546
``True`` or ``False`` if it is omitted by the caller.
@@ -2541,6 +2553,28 @@ Functions and decorators
25412553
* Arbitrary other keyword arguments are accepted in order to allow for
25422554
possible future extensions.
25432555

2556+
Type checkers recognize the following optional arguments on field
2557+
specifiers:
2558+
2559+
* ``init`` indicates whether the field should be included in the
2560+
synthesized ``__init__`` method. If unspecified, ``init`` defaults to
2561+
``True``.
2562+
* ``default`` provides the default value for the field.
2563+
* ``default_factory`` provides a runtime callback that returns the
2564+
default value for the field. If neither ``default`` nor
2565+
``default_factory`` are specified, the field is assumed to have no
2566+
default value and must be provided a value when the class is
2567+
instantiated.
2568+
* ``factory`` is an alias for ``default_factory``.
2569+
* ``kw_only`` indicates whether the field should be marked as
2570+
keyword-only. If ``True``, the field will be keyword-only. If
2571+
``False``, it will not be keyword-only. If unspecified, the value of
2572+
the ``kw_only`` parameter on the object decorated with
2573+
``dataclass_transform`` will be used, or if that is unspecified, the
2574+
value of ``kw_only_default`` on ``dataclass_transform`` will be used.
2575+
* ``alias`` provides an alternative name for the field. This alternative
2576+
name is used in the synthesized ``__init__`` method.
2577+
25442578
At runtime, this decorator records its arguments in the
25452579
``__dataclass_transform__`` attribute on the decorated object.
25462580
It has no other runtime effect.

0 commit comments

Comments
 (0)