summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEric V. Smith <ericvsmith@users.noreply.github.com>2022-05-02 16:36:39 (GMT)
committerGitHub <noreply@github.com>2022-05-02 16:36:39 (GMT)
commit5f9c0f5ddf441dedeb085b0d9f9c9488ca6bd44d (patch)
tree7cd849376052fc7278742ebeb799e77e35cd4793
parent958f21c5cdb3bbbd16fec87164785cff3dacce96 (diff)
downloadcpython-5f9c0f5ddf441dedeb085b0d9f9c9488ca6bd44d.zip
cpython-5f9c0f5ddf441dedeb085b0d9f9c9488ca6bd44d.tar.gz
cpython-5f9c0f5ddf441dedeb085b0d9f9c9488ca6bd44d.tar.bz2
Add weakref_slot to dataclass decorator, to allow instances with slots to be weakref-able. (#92160)
-rw-r--r--Doc/library/dataclasses.rst17
-rw-r--r--Lib/dataclasses.py27
-rw-r--r--Lib/test/test_dataclasses.py72
-rw-r--r--Misc/NEWS.d/next/Library/2022-05-02-09-09-47.gh-issue-91215.l1p7CJ.rst3
4 files changed, 106 insertions, 13 deletions
diff --git a/Doc/library/dataclasses.rst b/Doc/library/dataclasses.rst
index 08568da..ec50696 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 1acb712..4645ebf 100644
--- a/Lib/dataclasses.py
+++ b/Lib/dataclasses.py
@@ -883,7 +883,7 @@ _hash_action = {(False, False, False, False): None,
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 make_dataclass(cls_name, fields, *, bases=(), namespace=None, init=True,
# 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 847bcd4..6a36da1 100644
--- a/Lib/test/test_dataclasses.py
+++ b/Lib/test/test_dataclasses.py
@@ -9,6 +9,7 @@ import pickle
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 TestSlots(unittest.TestCase):
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 0000000..3a9897c
--- /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