@@ -143,37 +143,34 @@ def _module_meta_call(cls: type[M], *args, **kwargs) -> M:
143
143
144
144
name = None
145
145
if parent_ctx is not None :
146
- want_name = 'name' in kwargs and 'name' in inspect .get_annotations (cls )
147
- if not want_name and not parent_ctx .in_compact and 'name' in kwargs :
148
- raise ValueError (
149
- f"'name' can only be set in @compact functions. If in setup(), "
150
- "use parent's `self.<attr_name> to set the submodule name." )
146
+ if 'parent' in kwargs :
147
+ parent = kwargs .pop ('parent' )
148
+ if parent_ctx .in_compact and parent is not None :
149
+ raise ValueError (
150
+ f"'parent' can only be set to None, got { type (parent ).__name__ } "
151
+ )
152
+ else :
153
+ parent = parent_ctx .module
151
154
152
- if parent_ctx .in_compact :
153
- if 'parent' in kwargs :
154
- parent = kwargs .pop ('parent' )
155
- if parent is not None :
156
- raise ValueError (
157
- f"'parent' can only be set to None, got { type (parent ).__name__ } "
158
- )
159
- else :
160
- if 'name' in kwargs :
161
- name = kwargs ['name' ] if want_name else kwargs .pop ('name' )
162
- if not isinstance (name , str ):
163
- raise ValueError (f"'name' must be a 'str', got { type (name ).__name__ } " )
164
- else :
165
- name = _auto_submodule_name (parent_ctx , cls )
166
- parent = parent_ctx .module
155
+ if 'name' in kwargs :
156
+ name = kwargs ['name' ]
157
+ if not 'name' in inspect .get_annotations (cls ):
158
+ kwargs .pop ('name' )
159
+ if not isinstance (name , str ):
160
+ raise ValueError (f"'name' must be a 'str', got { type (name ).__name__ } " )
161
+ elif parent_ctx .in_compact :
162
+ name = _auto_submodule_name (parent_ctx , cls )
167
163
168
164
module = nnx_module .ModuleMeta .__call__ (cls , * args , ** kwargs )
169
165
module .scope = None
170
166
module .attr_priorities = {}
171
167
172
- # compact behavior
173
168
if parent is not None :
174
169
assert parent .scope is not None
175
- assert name is not None
176
- setattr (parent , name , module )
170
+ # compact, or setup if `name` exists
171
+ if name is not None :
172
+ setattr (parent , name , module )
173
+ parent .set_attr_priority (name , AttrPriority .INIT_PARENT )
177
174
178
175
return module # type: ignore
179
176
@@ -182,9 +179,10 @@ def _module_meta_call(cls: type[M], *args, **kwargs) -> M:
182
179
ModuleMeta .__call__ = _module_meta_call # type: ignore
183
180
184
181
class AttrPriority (enum .IntEnum ):
185
- HIGH = 1
186
- DEFAULT = 2
187
- LOW = 3
182
+ HIGH = 0
183
+ INIT_PARENT = 20
184
+ DEFAULT = 50
185
+ LOW = 100
188
186
189
187
190
188
class PriorityStr (str ):
0 commit comments