@@ -123,28 +123,31 @@ def _module_meta_call(cls: type[M], *args, **kwargs) -> M:
123
123
parent = None
124
124
module : M
125
125
126
- if parent_ctx is not None and parent_ctx .in_compact :
127
- if 'parent' in kwargs :
128
- parent = kwargs .pop ('parent' )
129
- if parent is not None :
130
- raise ValueError (
131
- f"'parent' can only be set to None, got { type (parent ).__name__ } "
132
- )
133
- name = None
134
- else :
135
- type_index = parent_ctx .type_counter [cls ]
136
- parent_ctx .type_counter [cls ] += 1
126
+ name = None
127
+ if parent_ctx is not None :
128
+ if not parent_ctx .in_compact and 'name' in kwargs :
129
+ raise ValueError (
130
+ f"'name' can only be set in @compact functions. If in setup(), "
131
+ "use parent's `self.<attr_name> to set the submodule name." )
137
132
138
- if 'name' in kwargs :
139
- name = kwargs .pop ('name' )
140
- if not isinstance (name , str ):
141
- raise ValueError (f"'name' must be a 'str', got { type (name ).__name__ } " )
133
+ if parent_ctx .in_compact :
134
+ if 'parent' in kwargs :
135
+ parent = kwargs .pop ('parent' )
136
+ if parent is not None :
137
+ raise ValueError (
138
+ f"'parent' can only be set to None, got { type (parent ).__name__ } "
139
+ )
142
140
else :
143
- name = f'{ cls .__name__ } _{ type_index } '
144
-
145
- parent = parent_ctx .module
146
- else :
147
- name = None
141
+ type_index = parent_ctx .type_counter [cls ]
142
+ parent_ctx .type_counter [cls ] += 1
143
+
144
+ if 'name' in kwargs :
145
+ name = kwargs .pop ('name' )
146
+ if not isinstance (name , str ):
147
+ raise ValueError (f"'name' must be a 'str', got { type (name ).__name__ } " )
148
+ else :
149
+ name = f'{ cls .__name__ } _{ type_index } '
150
+ parent = parent_ctx .module
148
151
149
152
module = nnx_module .ModuleMeta .__call__ (cls , * args , ** kwargs )
150
153
module .scope = None
0 commit comments