Skip to content

Commit 6a3a6ba

Browse files
gh-91860: documentation for typing.dataclass_transform (GH-92768)
Co-authored-by: Alex Waygood <[email protected]> (cherry picked from commit f20a6a5) Co-authored-by: Shantanu <[email protected]>
1 parent 6ec050f commit 6a3a6ba

File tree

3 files changed

+84
-12
lines changed

3 files changed

+84
-12
lines changed

Doc/library/typing.rst

Lines changed: 69 additions & 0 deletions
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

Lines changed: 5 additions & 5 deletions
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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3340,10 +3340,10 @@ def dataclass_transform(
33403340
33413341
Example usage with a decorator function:
33423342
3343-
_T = TypeVar("_T")
3343+
T = TypeVar("T")
33443344
33453345
@dataclass_transform()
3346-
def create_model(cls: type[_T]) -> type[_T]:
3346+
def create_model(cls: type[T]) -> type[T]:
33473347
...
33483348
return cls
33493349
@@ -3372,20 +3372,23 @@ class CustomerModel(ModelBase):
33723372
id: int
33733373
name: str
33743374
3375-
Each of the ``CustomerModel`` classes defined in this example will now
3376-
behave similarly to a dataclass created with the ``@dataclasses.dataclass``
3377-
decorator. For example, the type checker will synthesize an ``__init__``
3378-
method.
3375+
The ``CustomerModel`` classes defined above will
3376+
be treated by type checkers similarly to classes created with
3377+
``@dataclasses.dataclass``.
3378+
For example, type checkers will assume these classes have
3379+
``__init__`` methods that accept ``id`` and ``name``.
33793380
33803381
The arguments to this decorator can be used to customize this behavior:
33813382
- ``eq_default`` indicates whether the ``eq`` parameter is assumed to be
3382-
True or False if it is omitted by the caller.
3383+
``True`` or ``False`` if it is omitted by the caller.
33833384
- ``order_default`` indicates whether the ``order`` parameter is
33843385
assumed to be True or False if it is omitted by the caller.
33853386
- ``kw_only_default`` indicates whether the ``kw_only`` parameter is
33863387
assumed to be True or False if it is omitted by the caller.
33873388
- ``field_specifiers`` specifies a static list of supported classes
33883389
or functions that describe fields, similar to ``dataclasses.field()``.
3390+
- Arbitrary other keyword arguments are accepted in order to allow for
3391+
possible future extensions.
33893392
33903393
At runtime, this decorator records its arguments in the
33913394
``__dataclass_transform__`` attribute on the decorated object.

0 commit comments

Comments
 (0)