@@ -196,9 +196,11 @@ def __repr__(self):
196
196
return out
197
197
198
198
199
- def lightning_get_all_attr_holders (model , attribute ):
200
- """ Special attribute finding for lightning. Gets all of the objects or dicts that holds attribute.
201
- Checks for attribute in model namespace, the old hparams namespace/dict, and the datamodule. """
199
+ def _lightning_get_all_attr_holders (model , attribute ):
200
+ """
201
+ Special attribute finding for Lightning. Gets all of the objects or dicts that holds attribute.
202
+ Checks for attribute in model namespace, the old hparams namespace/dict, and the datamodule.
203
+ """
202
204
trainer = getattr (model , 'trainer' , None )
203
205
204
206
holders = []
@@ -219,31 +221,40 @@ def lightning_get_all_attr_holders(model, attribute):
219
221
return holders
220
222
221
223
222
- def lightning_get_first_attr_holder (model , attribute ):
224
+ def _lightning_get_first_attr_holder (model , attribute ):
225
+ """
226
+ Special attribute finding for Lightning. Gets the object or dict that holds attribute, or None.
227
+ Checks for attribute in model namespace, the old hparams namespace/dict, and the datamodule,
228
+ returns the last one that has it.
223
229
"""
224
- Special attribute finding for lightning. Gets the object or dict that holds attribute, or None.
225
- Checks for attribute in model namespace, the old hparams namespace/dict, and the datamodule,
226
- returns the last one that has it.
227
- """
228
- holders = lightning_get_all_attr_holders (model , attribute )
230
+ holders = _lightning_get_all_attr_holders (model , attribute )
229
231
if len (holders ) == 0 :
230
232
return None
231
233
# using the last holder to preserve backwards compatibility
232
234
return holders [- 1 ]
233
235
234
236
235
237
def lightning_hasattr (model , attribute ):
236
- """ Special hasattr for lightning. Checks for attribute in model namespace,
237
- the old hparams namespace/dict, and the datamodule. """
238
- return lightning_get_first_attr_holder (model , attribute ) is not None
238
+ """
239
+ Special hasattr for Lightning. Checks for attribute in model namespace,
240
+ the old hparams namespace/dict, and the datamodule.
241
+ """
242
+ return _lightning_get_first_attr_holder (model , attribute ) is not None
239
243
240
244
241
245
def lightning_getattr (model , attribute ):
242
- """ Special getattr for lightning. Checks for attribute in model namespace,
243
- the old hparams namespace/dict, and the datamodule. """
244
- holder = lightning_get_first_attr_holder (model , attribute )
246
+ """
247
+ Special getattr for Lightning. Checks for attribute in model namespace,
248
+ the old hparams namespace/dict, and the datamodule.
249
+
250
+ Raises:
251
+ AttributeError:
252
+ If ``model`` doesn't have ``attribute`` in any of
253
+ model namespace, the hparams namespace/dict, and the datamodule.
254
+ """
255
+ holder = _lightning_get_first_attr_holder (model , attribute )
245
256
if holder is None :
246
- raise ValueError (
257
+ raise AttributeError (
247
258
f'{ attribute } is neither stored in the model namespace'
248
259
' nor the `hparams` namespace/dict, nor the datamodule.'
249
260
)
@@ -254,13 +265,19 @@ def lightning_getattr(model, attribute):
254
265
255
266
256
267
def lightning_setattr (model , attribute , value ):
257
- """ Special setattr for lightning. Checks for attribute in model namespace
258
- and the old hparams namespace/dict.
259
- Will also set the attribute on datamodule, if it exists.
260
268
"""
261
- holders = lightning_get_all_attr_holders (model , attribute )
269
+ Special setattr for Lightning. Checks for attribute in model namespace
270
+ and the old hparams namespace/dict.
271
+ Will also set the attribute on datamodule, if it exists.
272
+
273
+ Raises:
274
+ AttributeError:
275
+ If ``model`` doesn't have ``attribute`` in any of
276
+ model namespace, the hparams namespace/dict, and the datamodule.
277
+ """
278
+ holders = _lightning_get_all_attr_holders (model , attribute )
262
279
if len (holders ) == 0 :
263
- raise ValueError (
280
+ raise AttributeError (
264
281
f'{ attribute } is neither stored in the model namespace'
265
282
' nor the `hparams` namespace/dict, nor the datamodule.'
266
283
)
0 commit comments