Skip to content
Closed
Changes from all commits
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
21 changes: 21 additions & 0 deletions Doc/library/dataclasses.rst
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,27 @@ Module contents
:attr:`!C.t` will be ``20``, and the class attributes :attr:`!C.x` and
:attr:`!C.y` will not be set.

.. warning::

Do not reuse :class:`Field` objects across multiple fields or classes.
Each call to :func:`!field` returns a new object that gets modified
in place by the :deco:`dataclass` decorator. Reusing the same field
object will cause unexpected behavior::

f = field(kw_only=True)

@dataclass
class A:
x: int = f # Don't do this
y: int = f # f is now modified and broken

Instead, call :func:`!field` separately for each field::

@dataclass
class A:
x: int = field(kw_only=True)
y: int = field(kw_only=True)

.. versionchanged:: next
If *metadata* is ``None``, use an empty :class:`frozendict`, instead
of a :func:`~types.MappingProxyType` of an empty :class:`dict`.
Expand Down
Loading