Skip to content

Commit 1573f16

Browse files
[3.12] Improve docs for typing.dataclass_transform (GH-105792) (#105809)
Improve docs for `typing.dataclass_transform` (GH-105792) (cherry picked from commit 006a453) Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
1 parent 335fbd6 commit 1573f16

File tree

1 file changed

+69
-37
lines changed

1 file changed

+69
-37
lines changed

Doc/library/typing.rst

+69-37
Original file line numberDiff line numberDiff line change
@@ -2526,16 +2526,19 @@ Functions and decorators
25262526

25272527
.. versionadded:: 3.11
25282528

2529-
.. decorator:: dataclass_transform
2529+
.. decorator:: dataclass_transform(*, eq_default=True, order_default=False, \
2530+
kw_only_default=False, frozen_default=False, \
2531+
field_specifiers=(), **kwargs)
25302532
25312533
Decorator to mark an object as providing
2532-
:func:`~dataclasses.dataclass`-like behavior.
2534+
:func:`dataclass <dataclasses.dataclass>`-like behavior.
25332535

25342536
``dataclass_transform`` may be used to
25352537
decorate a class, metaclass, or a function that is itself a decorator.
25362538
The presence of ``@dataclass_transform()`` tells a static type checker that the
25372539
decorated object performs runtime "magic" that
2538-
transforms a class in a similar way to :func:`dataclasses.dataclass`.
2540+
transforms a class in a similar way to
2541+
:func:`@dataclasses.dataclass <dataclasses.dataclass>`.
25392542

25402543
Example usage with a decorator function:
25412544

@@ -2589,42 +2592,71 @@ Functions and decorators
25892592
customize the default behaviors of the decorated class, metaclass, or
25902593
function:
25912594

2592-
* ``eq_default`` indicates whether the ``eq`` parameter is assumed to be
2593-
``True`` or ``False`` if it is omitted by the caller.
2594-
* ``order_default`` indicates whether the ``order`` parameter is
2595-
assumed to be True or False if it is omitted by the caller.
2596-
* ``kw_only_default`` indicates whether the ``kw_only`` parameter is
2597-
assumed to be True or False if it is omitted by the caller.
2598-
* ``frozen_default`` indicates whether the ``frozen`` parameter is
2599-
assumed to be True or False if it is omitted by the caller.
2600-
2601-
.. versionadded:: 3.12
2602-
* ``field_specifiers`` specifies a static list of supported classes
2603-
or functions that describe fields, similar to ``dataclasses.field()``.
2604-
* Arbitrary other keyword arguments are accepted in order to allow for
2605-
possible future extensions.
2606-
2607-
Type checkers recognize the following optional arguments on field
2595+
:param bool eq_default:
2596+
Indicates whether the ``eq`` parameter is assumed to be
2597+
``True`` or ``False`` if it is omitted by the caller.
2598+
Defaults to ``True``.
2599+
2600+
:param bool order_default:
2601+
Indicates whether the ``order`` parameter is
2602+
assumed to be ``True`` or ``False`` if it is omitted by the caller.
2603+
Defaults to ``False``.
2604+
2605+
:param bool kw_only_default:
2606+
Indicates whether the ``kw_only`` parameter is
2607+
assumed to be ``True`` or ``False`` if it is omitted by the caller.
2608+
Defaults to ``False``.
2609+
2610+
:param bool frozen_default:
2611+
Indicates whether the ``frozen`` parameter is
2612+
assumed to be ``True`` or ``False`` if it is omitted by the caller.
2613+
Defaults to ``False``.
2614+
2615+
.. versionadded:: 3.12
2616+
2617+
:param field_specifiers:
2618+
Specifies a static list of supported classes
2619+
or functions that describe fields, similar to :func:`dataclasses.field`.
2620+
Defaults to ``()``.
2621+
:type field_specifiers: tuple[Callable[..., Any], ...]
2622+
2623+
:param Any \**kwargs:
2624+
Arbitrary other keyword arguments are accepted in order to allow for
2625+
possible future extensions.
2626+
2627+
Type checkers recognize the following optional parameters on field
26082628
specifiers:
26092629

2610-
* ``init`` indicates whether the field should be included in the
2611-
synthesized ``__init__`` method. If unspecified, ``init`` defaults to
2612-
``True``.
2613-
* ``default`` provides the default value for the field.
2614-
* ``default_factory`` provides a runtime callback that returns the
2615-
default value for the field. If neither ``default`` nor
2616-
``default_factory`` are specified, the field is assumed to have no
2617-
default value and must be provided a value when the class is
2618-
instantiated.
2619-
* ``factory`` is an alias for ``default_factory``.
2620-
* ``kw_only`` indicates whether the field should be marked as
2621-
keyword-only. If ``True``, the field will be keyword-only. If
2622-
``False``, it will not be keyword-only. If unspecified, the value of
2623-
the ``kw_only`` parameter on the object decorated with
2624-
``dataclass_transform`` will be used, or if that is unspecified, the
2625-
value of ``kw_only_default`` on ``dataclass_transform`` will be used.
2626-
* ``alias`` provides an alternative name for the field. This alternative
2627-
name is used in the synthesized ``__init__`` method.
2630+
.. list-table:: **Recognised parameters for field specifiers**
2631+
:header-rows: 1
2632+
:widths: 20 80
2633+
2634+
* - Parameter name
2635+
- Description
2636+
* - ``init``
2637+
- Indicates whether the field should be included in the
2638+
synthesized ``__init__`` method. If unspecified, ``init`` defaults to
2639+
``True``.
2640+
* - ``default``
2641+
- Provides the default value for the field.
2642+
* - ``default_factory``
2643+
- Provides a runtime callback that returns the
2644+
default value for the field. If neither ``default`` nor
2645+
``default_factory`` are specified, the field is assumed to have no
2646+
default value and must be provided a value when the class is
2647+
instantiated.
2648+
* - ``factory``
2649+
- An alias for the ``default_factory`` parameter on field specifiers.
2650+
* - ``kw_only``
2651+
- Indicates whether the field should be marked as
2652+
keyword-only. If ``True``, the field will be keyword-only. If
2653+
``False``, it will not be keyword-only. If unspecified, the value of
2654+
the ``kw_only`` parameter on the object decorated with
2655+
``dataclass_transform`` will be used, or if that is unspecified, the
2656+
value of ``kw_only_default`` on ``dataclass_transform`` will be used.
2657+
* - ``alias``
2658+
- Provides an alternative name for the field. This alternative
2659+
name is used in the synthesized ``__init__`` method.
26282660

26292661
At runtime, this decorator records its arguments in the
26302662
``__dataclass_transform__`` attribute on the decorated object.

0 commit comments

Comments
 (0)