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

docs: document dataclass_transform behavior #16017

Merged
Changes from 1 commit
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
43 changes: 31 additions & 12 deletions docs/source/additional_features.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@ and :pep:`557`.
Caveats/Known Issues
====================

Some functions in the :py:mod:`dataclasses` module, such as :py:func:`~dataclasses.replace` and :py:func:`~dataclasses.asdict`,
Some functions in the :py:mod:`dataclasses` module, such as :py:func:`~dataclasses.asdict`,
have imprecise (too permissive) types. This will be fixed in future releases.

Mypy does not yet recognize aliases of :py:func:`dataclasses.dataclass <dataclasses.dataclass>`, and will
probably never recognize dynamically computed decorators. The following examples
do **not** work:
probably never recognize dynamically computed decorators. The following example
does **not** work:

.. code-block:: python

Expand All @@ -94,16 +94,35 @@ do **not** work:
"""
attribute: int

@dataclass_wrapper
class DynamicallyDecorated:
"""
Mypy doesn't recognize this as a dataclass because it is decorated by a
function returning `dataclass` rather than by `dataclass` itself.
"""
attribute: int

AliasDecorated(attribute=1) # error: Unexpected keyword argument
DynamicallyDecorated(attribute=1) # error: Unexpected keyword argument


To have Mypy recognize a wrapper of :py:func:`dataclasses.dataclass <dataclasses.dataclass>`
as a dataclass decorator, consider using the :py:func:`~typing.dataclass_transform` decorator:

.. code-block:: python

from dataclasses import dataclass, Field
from typing import dataclass_transform

@dataclass_transform(fields=(Field,))
ikonst marked this conversation as resolved.
Show resolved Hide resolved
def my_dataclass(cls: type[T]) -> type[T]:
...
return dataclass(cls)


Data Class Transforms
*********************

Mypy supports the :py:func:`~typing.dataclass_transform` decorator as described in
`PEP 681 <https://www.python.org/dev/peps/pep-0681/#the-dataclass-transform-decorator>`_.

.. note::

Pragmatically, such classes would appear to have the internal attribute :code:`__dataclass_fields__`
ikonst marked this conversation as resolved.
Show resolved Hide resolved
(even though they might lack it in runtime) and dataclass functions such as :py:func:`dataclasses.is_dataclass`
and :py:func:`dataclasses.fields` would appear to accept them as if they were dataclasses
ikonst marked this conversation as resolved.
Show resolved Hide resolved
(even though they may fail at runtime).

.. _attrs_package:

Expand Down