FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-102578: Optimise setting and deleting mutable attributes on non-dataclass subclasses of frozen dataclasses by XuehaiPan · Pull Request #102573 · python/cpython · GitHub

Repository navigation

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension .rst  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
Prev Previous commit
Next Next commit
Update news entry
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
  • Loading branch information
XuehaiPan and AlexWaygood authored Mar 10, 2023
commit 0bbf3a8344a8ed0bbad230bca7f41bca45da23fc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,24 +1,4 @@
Speed up sanity check for ``__setattr__`` and ``__delattr__`` for frozen
``dataclass``\es by using ``set``-based name lookup rather than ``tuple``\s.

The sanity check is always performed, even if the ``dataclass`` is inherited.
For example:

.. code-block:: python

@dataclass(frozen=True)
class FrozenBase:
x: int
y: int
... # N_FIELDS

class Foo(FrozenBase):
def __init__(self, x, y, somevalue, someothervalue):
super().__init__(x, y)
self.somevalue = somevalue # takes O(N_FIELDS)
self.someothervalue = someothervalue # takes O(N_FIELDS) time again

foo = Foo(1, 2, 3, 4)
foo.extravalue = extravalue # takes O(N_FIELDS) time again

This change makes the sanity check :math:`O(1)` in the number of fields.
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)``.

Back | FazBrowse Home | New Git URL