[Python-checkins] Add weakref_slot to dataclass decorator, to allow instances with slots to be weakref-able. (#92160)
ericvsmith
webhook-mailer at python.org
Mon May 2 12:36:49 EDT 2022
https://github.com/python/cpython/commit/5f9c0f5ddf441dedeb085b0d9f9c9488ca6bd44d
commit: 5f9c0f5ddf441dedeb085b0d9f9c9488ca6bd44d
branch: main
author: Eric V. Smith <ericvsmith at users.noreply.github.com>
committer: ericvsmith <ericvsmith at users.noreply.github.com>
date: 2022-05-02T10:36:39-06:00
summary:
Add weakref_slot to dataclass decorator, to allow instances with slots to be weakref-able. (#92160)
files:
A Misc/NEWS.d/next/Library/2022-05-02-09-09-47.gh-issue-91215.l1p7CJ.rst
M Doc/library/dataclasses.rst
M Lib/dataclasses.py
M Lib/test/test_dataclasses.py
diff --git a/Doc/library/dataclasses.rst b/Doc/library/dataclasses.rst
index 08568da19d71a..ec50696ea89d4 100644
--- a/Doc/library/dataclasses.rst
+++ b/Doc/library/dataclasses.rst
@@ -46,7 +46,7 @@ directly specified in the ``InventoryItem`` definition shown above.
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.
@@ -79,7 +79,7 @@ Module contents
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:
...
@@ -198,6 +198,13 @@ Module contents
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::
@@ -381,7 +388,7 @@ Module contents
: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
@@ -390,8 +397,8 @@ Module contents
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
diff --git a/Lib/dataclasses.py b/Lib/dataclasses.py
index 1acb7128f1a0c..4645ebfa71e71 100644
--- a/Lib/dataclasses.py
+++ b/Lib/dataclasses.py
@@ -883,7 +883,7 @@ def _hash_exception(cls, fields, globals):
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
@@ -1101,8 +1101,11 @@ def _process_class(cls, init, repr, eq, order, unsafe_hash, frozen,
_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)
@@ -1137,7 +1140,7 @@ def _get_slots(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.
@@ -1152,9 +1155,14 @@ def _add_slots(cls, is_frozen):
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.
@@ -1179,7 +1187,7 @@ def _add_slots(cls, is_frozen):
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.
@@ -1197,7 +1205,8 @@ def dataclass(cls=None, /, *, init=True, repr=True, eq=True, order=False,
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:
@@ -1356,7 +1365,8 @@ def _astuple_inner(obj, tuple_factory):
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
@@ -1423,7 +1433,8 @@ def exec_body_callback(ns):
# 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):
diff --git a/Lib/test/test_dataclasses.py b/Lib/test/test_dataclasses.py
index 847bcd46a6926..6a36da104ac89 100644
--- a/Lib/test/test_dataclasses.py
+++ b/Lib/test/test_dataclasses.py
@@ -9,6 +9,7 @@
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
@@ -3038,6 +3039,77 @@ class A:
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.
diff --git a/Misc/NEWS.d/next/Library/2022-05-02-09-09-47.gh-issue-91215.l1p7CJ.rst b/Misc/NEWS.d/next/Library/2022-05-02-09-09-47.gh-issue-91215.l1p7CJ.rst
new file mode 100644
index 0000000000000..3a9897cb79642
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-05-02-09-09-47.gh-issue-91215.l1p7CJ.rst
@@ -0,0 +1,3 @@
+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
More information about the Python-checkins
mailing list