RegistryObjects are their own parent registries #1251

Open
opened 2025-03-31 13:32:59 +02:00 by magicfelix · 2 comments
Member

When a subclass is initialized, the following happens:

parent_registry = getattr(cls, "_registry", None)

if parent_registry is None or is_registry: # new subclass is a registry
    if getattr(cls, "_parent_registries", None) is None: # only the case for direct children of DAVResource
        cls._parent_registries = []

    if parent_registry is not None:
        cls._parent_registries.append(parent_registry) # see below

For e.g. CalendarEventMixin, a subclass of DAVResource, cls._parent_registries is not None, because it was initialized with [] for DAVResource. Therefore, parent_registry, which is DAVResource._registry, is appended to CalendarEventMixin._parent_registries (which is intended), but this list is the same object as DAVResource._parent_registries. In the end, this results in DAVResource._parent_registries containing DAVResource._registry, hence the issue title, being their own parent.

My suggestion is to always (of course only if „new subclass is a registry”) initialize cls._parent_registries with [], to make the behaviour more consistent. Maybe this requires to adjust the _register method to also register in parent-parent (recursive) registry, if that is the desired behaviour.

When a subclass is initialized, the following happens: ```python parent_registry = getattr(cls, "_registry", None) if parent_registry is None or is_registry: # new subclass is a registry if getattr(cls, "_parent_registries", None) is None: # only the case for direct children of DAVResource cls._parent_registries = [] if parent_registry is not None: cls._parent_registries.append(parent_registry) # see below ``` For e.g. `CalendarEventMixin`, a subclass of `DAVResource`, `cls._parent_registries` is not None, because it was initialized with `[]` for `DAVResource`. Therefore, `parent_registry`, which is `DAVResource._registry`, is appended to `CalendarEventMixin._parent_registries` (which is intended), but this list is the same object as `DAVResource._parent_registries`. In the end, this results in `DAVResource._parent_registries` containing `DAVResource._registry`, hence the issue title, being their own parent. My suggestion is to always (of course only if „new subclass is a registry”) initialize `cls._parent_registries` with `[]`, to make the behaviour more consistent. Maybe this requires to adjust the `_register` method to also register in parent-parent (recursive) registry, if that is the desired behaviour.
Author
Member

created branch 1251-registryobjects-are-their-own-parent-registries to address this issue

created branch [`1251-registryobjects-are-their-own-parent-registries`](/AlekSIS/official/AlekSIS-Core/-/compare/master...1251-registryobjects-are-their-own-parent-registries) to address this issue
Author
Member

mentioned in merge request !3414

mentioned in merge request !3414
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
aleksis/AlekSIS-Core#1251
No description provided.