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

optimize rereferencing #42

Closed
wants to merge 1 commit into from
Closed
Changes from all 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
23 changes: 13 additions & 10 deletions autoregistry/_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,22 @@ def cls(self):
@cls.setter
def cls(self, new_cls: type):
if self._cls is not None:
self._rereference(self._cls, new_cls)
self._rereference(self._cls, new_cls, root=True)
self._cls = new_cls

def _rereference(self, old_cls: type, new_cls: type) -> None:
"""Recursively updates all registry references from ``old_cls`` to ``new_cls``."""
# TODO: this could be optimized by only recursively apply to recursive parents
# And not searching self at first _rereference iteration.
for k, v in self.items():
if v is old_cls:
self[k] = new_cls
def _rereference(self, old_cls: type, new_cls: type, root=False) -> None:
"""Recursively updates all registry references from ``old_cls`` to ``new_cls``.

for parent_registry in self.walk_parent_registries():
parent_registry._rereference(old_cls, new_cls)
Used when a "clone" class is created, in libraries like ``attrs``.
"""
if not root:
for k, v in self.items():
if v is old_cls:
self[k] = new_cls

if root or self.config.recursive:
for parent_registry in self.walk_parent_registries():
parent_registry._rereference(old_cls, new_cls)

def walk_parent_registries(self) -> Generator["_Registry", None, None]:
"""Iterates over immediate parenting classes and returns their ``_Registry``."""
Expand Down
Loading