Skip to content

Commit f20a6a5

Browse files
gh-91860: documentation for typing.dataclass_transform (#92768)
Co-authored-by: Alex Waygood <[email protected]>
1 parent d853758 commit f20a6a5

File tree

3 files changed

+84
-12
lines changed

3 files changed

+84
-12
lines changed

Doc/library/typing.rst

+69
Original file line numberDiff line numberDiff line change
@@ -2429,6 +2429,75 @@ Functions and decorators
24292429

24302430
.. versionadded:: 3.11
24312431

2432+
.. decorator:: dataclass_transform
2433+
2434+
:data:`~typing.dataclass_transform` may be used to
2435+
decorate a class, metaclass, or a function that is itself a decorator.
2436+
The presence of ``@dataclass_transform()`` tells a static type checker that the
2437+
decorated object performs runtime "magic" that
2438+
transforms a class, giving it :func:`dataclasses.dataclass`-like behaviors.
2439+
2440+
Example usage with a decorator function::
2441+
2442+
T = TypeVar("T")
2443+
2444+
@dataclass_transform()
2445+
def create_model(cls: type[T]) -> type[T]:
2446+
...
2447+
return cls
2448+
2449+
@create_model
2450+
class CustomerModel:
2451+
id: int
2452+
name: str
2453+
2454+
On a base class::
2455+
2456+
@dataclass_transform()
2457+
class ModelBase: ...
2458+
2459+
class CustomerModel(ModelBase):
2460+
id: int
2461+
name: str
2462+
2463+
On a metaclass::
2464+
2465+
@dataclass_transform()
2466+
class ModelMeta(type): ...
2467+
2468+
class ModelBase(metaclass=ModelMeta): ...
2469+
2470+
class CustomerModel(ModelBase):
2471+
id: int
2472+
name: str
2473+
2474+
The ``CustomerModel`` classes defined above will
2475+
be treated by type checkers similarly to classes created with
2476+
:func:`@dataclasses.dataclass <dataclasses.dataclass>`.
2477+
For example, type checkers will assume these classes have
2478+
``__init__`` methods that accept ``id`` and ``name``.
2479+
2480+
The arguments to this decorator can be used to customize this behavior:
2481+
2482+
* ``eq_default`` indicates whether the ``eq`` parameter is assumed to be
2483+
``True`` or ``False`` if it is omitted by the caller.
2484+
* ``order_default`` indicates whether the ``order`` parameter is
2485+
assumed to be True or False if it is omitted by the caller.
2486+
* ``kw_only_default`` indicates whether the ``kw_only`` parameter is
2487+
assumed to be True or False if it is omitted by the caller.
2488+
* ``field_specifiers`` specifies a static list of supported classes
2489+
or functions that describe fields, similar to ``dataclasses.field()``.
2490+
* Arbitrary other keyword arguments are accepted in order to allow for
2491+
possible future extensions.
2492+
2493+
At runtime, this decorator records its arguments in the
2494+
``__dataclass_transform__`` attribute on the decorated object.
2495+
It has no other runtime effect.
2496+
2497+
See :pep:`681` for more details.
2498+
2499+
.. versionadded:: 3.11
2500+
24322501
.. decorator:: overload
24332502

24342503
The ``@overload`` decorator allows describing functions and methods

Doc/whatsnew/3.11.rst

+5-5
Original file line numberDiff line numberDiff line change
@@ -306,17 +306,17 @@ Kumar Srinivasan and Graham Bleaney.)
306306
PEP 681: Data Class Transforms
307307
------------------------------
308308

309-
The new :data:`~typing.dataclass_transform` annotation may be used to
310-
decorate a function that is itself a decorator, a class, or a metaclass.
309+
:data:`~typing.dataclass_transform` may be used to
310+
decorate a class, metaclass, or a function that is itself a decorator.
311311
The presence of ``@dataclass_transform()`` tells a static type checker that the
312-
decorated function, class, or metaclass performs runtime "magic" that
313-
transforms a class, endowing it with dataclass-like behaviors.
312+
decorated object performs runtime "magic" that
313+
transforms a class, giving it :func:`dataclasses.dataclass`-like behaviors.
314314

315315
For example::
316316

317317
# The ``create_model`` decorator is defined by a library.
318318
@typing.dataclass_transform()
319-
def create_model(cls: Type[_T]) -> Type[_T]:
319+
def create_model(cls: Type[T]) -> Type[T]:
320320
cls.__init__ = ...
321321
cls.__eq__ = ...
322322
cls.__ne__ = ...

Lib/typing.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -3368,10 +3368,10 @@ def dataclass_transform(
33683368
33693369
Example usage with a decorator function:
33703370
3371-
_T = TypeVar("_T")
3371+
T = TypeVar("T")
33723372
33733373
@dataclass_transform()
3374-
def create_model(cls: type[_T]) -> type[_T]:
3374+
def create_model(cls: type[T]) -> type[T]:
33753375
...
33763376
return cls
33773377
@@ -3400,20 +3400,23 @@ class CustomerModel(ModelBase):
34003400
id: int
34013401
name: str
34023402
3403-
Each of the ``CustomerModel`` classes defined in this example will now
3404-
behave similarly to a dataclass created with the ``@dataclasses.dataclass``
3405-
decorator. For example, the type checker will synthesize an ``__init__``
3406-
method.
3403+
The ``CustomerModel`` classes defined above will
3404+
be treated by type checkers similarly to classes created with
3405+
``@dataclasses.dataclass``.
3406+
For example, type checkers will assume these classes have
3407+
``__init__`` methods that accept ``id`` and ``name``.
34073408
34083409
The arguments to this decorator can be used to customize this behavior:
34093410
- ``eq_default`` indicates whether the ``eq`` parameter is assumed to be
3410-
True or False if it is omitted by the caller.
3411+
``True`` or ``False`` if it is omitted by the caller.
34113412
- ``order_default`` indicates whether the ``order`` parameter is
34123413
assumed to be True or False if it is omitted by the caller.
34133414
- ``kw_only_default`` indicates whether the ``kw_only`` parameter is
34143415
assumed to be True or False if it is omitted by the caller.
34153416
- ``field_specifiers`` specifies a static list of supported classes
34163417
or functions that describe fields, similar to ``dataclasses.field()``.
3418+
- Arbitrary other keyword arguments are accepted in order to allow for
3419+
possible future extensions.
34173420
34183421
At runtime, this decorator records its arguments in the
34193422
``__dataclass_transform__`` attribute on the decorated object.

0 commit comments

Comments
 (0)