Module contents
---------------
-.. decorator:: dataclass(*, init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False)
+.. decorator:: dataclass(*, init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False, weakref_slot=False)
This function is a :term:`decorator` that is used to add generated
:term:`special method`\s to classes, as described below.
class C:
...
- @dataclass(init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False)
+ @dataclass(init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False, weakref_slot=False)
class C:
...
base class ``__slots__`` may be any iterable, but *not* an iterator.
+ - ``weakref_slot``: If true (the default is ``False``), add a slot
+ named "__weakref__", which is required to make an instance
+ weakref-able. It is an error to specify ``weakref_slot=True``
+ without also specifying ``slots=True``.
+
+ .. versionadded:: 3.11
+
``field``\s may optionally specify a default value, using normal
Python syntax::
:func:`astuple` raises :exc:`TypeError` if ``obj`` is not a dataclass
instance.
-.. function:: make_dataclass(cls_name, fields, *, bases=(), namespace=None, init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False)
+.. function:: make_dataclass(cls_name, fields, *, bases=(), namespace=None, init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False, weakref_slot=False)
Creates a new dataclass with name ``cls_name``, fields as defined
in ``fields``, base classes as given in ``bases``, and initialized
or ``(name, type, Field)``. If just ``name`` is supplied,
``typing.Any`` is used for ``type``. The values of ``init``,
``repr``, ``eq``, ``order``, ``unsafe_hash``, ``frozen``,
- ``match_args``, ``kw_only``, and ``slots`` have the same meaning as
- they do in :func:`dataclass`.
+ ``match_args``, ``kw_only``, ``slots``, and ``weakref_slot`` have
+ the same meaning as they do in :func:`dataclass`.
This function is not strictly required, because any Python
mechanism for creating a new class with ``__annotations__`` can
def _process_class(cls, init, repr, eq, order, unsafe_hash, frozen,
- match_args, kw_only, slots):
+ match_args, kw_only, slots, weakref_slot):
# Now that dicts retain insertion order, there's no reason to use
# an ordered dict. I am leveraging that ordering here, because
# derived class fields overwrite base class fields, but the order
_set_new_attribute(cls, '__match_args__',
tuple(f.name for f in std_init_fields))
+ # It's an error to specify weakref_slot if slots is False.
+ if weakref_slot and not slots:
+ raise TypeError('weakref_slot is True but slots is False')
if slots:
- cls = _add_slots(cls, frozen)
+ cls = _add_slots(cls, frozen, weakref_slot)
abc.update_abstractmethods(cls)
raise TypeError(f"Slots of '{cls.__name__}' cannot be determined")
-def _add_slots(cls, is_frozen):
+def _add_slots(cls, is_frozen, weakref_slot):
# Need to create a new class, since we can't set __slots__
# after a class has been created.
inherited_slots = set(
itertools.chain.from_iterable(map(_get_slots, cls.__mro__[1:-1]))
)
+ # The slots for our class. Remove slots from our base classes. Add
+ # '__weakref__' if weakref_slot was given.
cls_dict["__slots__"] = tuple(
- itertools.filterfalse(inherited_slots.__contains__, field_names)
+ itertools.chain(
+ itertools.filterfalse(inherited_slots.__contains__, field_names),
+ ("__weakref__",) if weakref_slot else ())
)
+
for field_name in field_names:
# Remove our attributes, if present. They'll still be
# available in _MARKER.
def dataclass(cls=None, /, *, init=True, repr=True, eq=True, order=False,
unsafe_hash=False, frozen=False, match_args=True,
- kw_only=False, slots=False):
+ kw_only=False, slots=False, weakref_slot=False):
"""Returns the same class as was passed in, with dunder methods
added based on the fields defined in the class.
def wrap(cls):
return _process_class(cls, init, repr, eq, order, unsafe_hash,
- frozen, match_args, kw_only, slots)
+ frozen, match_args, kw_only, slots,
+ weakref_slot)
# See if we're being called as @dataclass or @dataclass().
if cls is None:
def make_dataclass(cls_name, fields, *, bases=(), namespace=None, init=True,
repr=True, eq=True, order=False, unsafe_hash=False,
- frozen=False, match_args=True, kw_only=False, slots=False):
+ frozen=False, match_args=True, kw_only=False, slots=False,
+ weakref_slot=False):
"""Return a new dynamically created dataclass.
The dataclass name will be 'cls_name'. 'fields' is an iterable
# Apply the normal decorator.
return dataclass(cls, init=init, repr=repr, eq=eq, order=order,
unsafe_hash=unsafe_hash, frozen=frozen,
- match_args=match_args, kw_only=kw_only, slots=slots)
+ match_args=match_args, kw_only=kw_only, slots=slots,
+ weakref_slot=weakref_slot)
def replace(obj, /, **changes):
import inspect
import builtins
import types
+import weakref
import unittest
from unittest.mock import Mock
from typing import ClassVar, Any, List, Union, Tuple, Dict, Generic, TypeVar, Optional, Protocol
self.assertEqual(obj.a, 'a')
self.assertEqual(obj.b, 'b')
+ def test_slots_no_weakref(self):
+ @dataclass(slots=True)
+ class A:
+ # No weakref.
+ pass
+
+ self.assertNotIn("__weakref__", A.__slots__)
+ a = A()
+ with self.assertRaisesRegex(TypeError,
+ "cannot create weak reference"):
+ weakref.ref(a)
+
+ def test_slots_weakref(self):
+ @dataclass(slots=True, weakref_slot=True)
+ class A:
+ a: int
+
+ self.assertIn("__weakref__", A.__slots__)
+ a = A(1)
+ weakref.ref(a)
+
+ def test_slots_weakref_base_str(self):
+ class Base:
+ __slots__ = '__weakref__'
+
+ @dataclass(slots=True)
+ class A(Base):
+ a: int
+
+ # __weakref__ is in the base class, not A. But an A is still weakref-able.
+ self.assertIn("__weakref__", Base.__slots__)
+ self.assertNotIn("__weakref__", A.__slots__)
+ a = A(1)
+ weakref.ref(a)
+
+ def test_slots_weakref_base_tuple(self):
+ # Same as test_slots_weakref_base, but use a tuple instead of a string
+ # in the base class.
+ class Base:
+ __slots__ = ('__weakref__',)
+
+ @dataclass(slots=True)
+ class A(Base):
+ a: int
+
+ # __weakref__ is in the base class, not A. But an A is still
+ # weakref-able.
+ self.assertIn("__weakref__", Base.__slots__)
+ self.assertNotIn("__weakref__", A.__slots__)
+ a = A(1)
+ weakref.ref(a)
+
+ def test_weakref_slot_without_slot(self):
+ with self.assertRaisesRegex(TypeError,
+ "weakref_slot is True but slots is False"):
+ @dataclass(weakref_slot=True)
+ class A:
+ a: int
+
+ def test_weakref_slot_make_dataclass(self):
+ A = make_dataclass('A', [('a', int),], slots=True, weakref_slot=True)
+ self.assertIn("__weakref__", A.__slots__)
+ a = A(1)
+ weakref.ref(a)
+
+ # And make sure if raises if slots=True is not given.
+ with self.assertRaisesRegex(TypeError,
+ "weakref_slot is True but slots is False"):
+ B = make_dataclass('B', [('a', int),], weakref_slot=True)
+
+
class TestDescriptors(unittest.TestCase):
def test_set_name(self):
# See bpo-33141.
--- /dev/null
+For @dataclass, add weakref_slot. Default is False. If True, and if
+slots=True, add a slot named "__weakref__", which will allow instances to be
+weakref'd. Contributed by Eric V. Smith