Skip to content

return None if no _parent_ref is set #2548

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

Merged
merged 2 commits into from
Oct 27, 2022
Merged
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
4 changes: 4 additions & 0 deletions flax/linen/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,9 @@ class ParentDescriptor:
logic applies in subclasses even after various dataclass transforms.
"""
def __get__(self, obj, objtype=None):
# check if obj is None, happens during %autoreload
if obj is None:
return None
parent = object.__getattribute__(obj, "_parent_ref")
return parent() if isinstance(parent, weakref.ReferenceType) else parent

Expand Down Expand Up @@ -610,6 +613,7 @@ def __init_subclass__(cls, **kwargs: Any) -> None:
cls._state = _uninitialized_module_internal_state
cls.scope: Optional[Scope] = None
# Handles weak referencing of parent Modules to prevent reference cycles.
cls._parent_ref = None
cls.parent = ParentDescriptor()

@classmethod
Expand Down