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

gh-102578: Optimise setting and deleting mutable attributes on non-dataclass subclasses of frozen dataclasses #102573

Merged
merged 15 commits into from
Mar 11, 2023
Merged
Show file tree
Hide file tree
Changes from 14 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
10 changes: 4 additions & 6 deletions Lib/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -616,21 +616,19 @@ def _repr_fn(fields, globals):
def _frozen_get_del_attr(cls, fields, globals):
locals = {'cls': cls,
'FrozenInstanceError': FrozenInstanceError}
condition = 'type(self) is cls'
if fields:
fields_str = '(' + ','.join(repr(f.name) for f in fields) + ',)'
else:
# Special case for the zero-length tuple.
fields_str = '()'
condition += ' or name in {' + ', '.join(repr(f.name) for f in fields) + '}'
return (_create_fn('__setattr__',
('self', 'name', 'value'),
(f'if type(self) is cls or name in {fields_str}:',
(f'if {condition}:',
' raise FrozenInstanceError(f"cannot assign to field {name!r}")',
f'super(cls, self).__setattr__(name, value)'),
locals=locals,
globals=globals),
_create_fn('__delattr__',
('self', 'name'),
(f'if type(self) is cls or name in {fields_str}:',
(f'if {condition}:',
' raise FrozenInstanceError(f"cannot delete field {name!r}")',
f'super(cls, self).__delattr__(name)'),
locals=locals,
Expand Down
42 changes: 42 additions & 0 deletions Lib/test/test_dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -2767,6 +2767,19 @@ class C:
c.i = 5
self.assertEqual(c.i, 10)

def test_frozen_empty(self):
@dataclass(frozen=True)
class C:
pass

c = C()
self.assertFalse(hasattr(c, 'i'))
with self.assertRaises(FrozenInstanceError):
c.i = 5
XuehaiPan marked this conversation as resolved.
Show resolved Hide resolved
self.assertFalse(hasattr(c, 'i'))
with self.assertRaises(FrozenInstanceError):
del c.i

def test_inherit(self):
@dataclass(frozen=True)
class C:
Expand Down Expand Up @@ -2890,6 +2903,35 @@ class S(D):
self.assertEqual(s.y, 10)
self.assertEqual(s.cached, True)

with self.assertRaises(FrozenInstanceError):
del s.x
self.assertEqual(s.x, 3)
with self.assertRaises(FrozenInstanceError):
del s.y
self.assertEqual(s.y, 10)
del s.cached
self.assertFalse(hasattr(s, 'cached'))
with self.assertRaises(AttributeError):
del s.cached
XuehaiPan marked this conversation as resolved.
Show resolved Hide resolved

def test_non_frozen_normal_derived_from_empty_frozen(self):
@dataclass(frozen=True)
class D:
pass

class S(D):
pass

s = S()
self.assertFalse(hasattr(s, 'x'))
s.x = 5
self.assertEqual(s.x, 5)

del s.x
self.assertFalse(hasattr(s, 'x'))
with self.assertRaises(AttributeError):
del s.x

def test_overwriting_frozen(self):
# frozen uses __setattr__ and __delattr__.
with self.assertRaisesRegex(TypeError,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Speed up setting or deleting mutable attributes on non-dataclass subclasses of
frozen dataclasses. Due to the implementation of ``__setattr__`` and
``__delattr__`` for frozen dataclasses, this previously had a time complexity
of ``O(n)``. It now has a time complexity of ``O(1)``.