@@ -48,7 +48,9 @@ def get(
4848
4949 value = tree .get (key , None )
5050 if value is not None :
51- return value .item ()
51+ if value .shape == ():
52+ return value .item ()
53+ return value
5254 value = tree .dims .get (key , None )
5355 if value is not None :
5456 return value
@@ -228,15 +230,12 @@ def getattribute(self: Any, name: str) -> Any:
228230 """
229231 cls = type (self )
230232 spec = fields_dict (cls )
231- try :
232- tree = self .data
233- if name in spec :
234- value = get (tree , name , None )
235- if value is not None :
236- return value
237- except :
238- pass
239- return super (cls , self ).__getattribute__ (name )
233+ tree = self .data
234+ if name in spec :
235+ value = get (tree , name , None )
236+ if value is not None :
237+ return value
238+ raise AttributeError
240239
241240
242241def setattribute (self : _Component , attr : Attribute , value : Any ):
@@ -253,10 +252,7 @@ def setattribute(self: _Component, attr: Attribute, value: Any):
253252 spec = fields_dict (cls )
254253 if attr .name not in spec :
255254 raise AttributeError (f"{ cls .__name__ } has no attribute { attr .name } " )
256- if value is None :
257- return
258- data = getattr (self , "data" , None )
259- if data is None :
255+ if value is None or not hasattr (self , "data" ):
260256 return value
261257 if get_origin (attr .type ) in [list , np .ndarray ]:
262258 shape = attr .metadata ["dims" ]
@@ -287,11 +283,7 @@ def init(self, *args, **kwargs):
287283 children = kwargs .pop ("children" , None )
288284 old_init (self , ** kwargs )
289285 init_tree (self , name = name , parent = parent , children = children )
290- # `__getattribute__` now. way faster.
291- self .__getattribute__ = getattribute
286+ cls .__getattr__ = getattribute
292287
293- # don't update `__getattribute__` yet, because init'ing
294- # the tree involves a lot of attribute access. do it in
295- # the init method, after the tree has been set up.
296288 cls .__init__ = init
297289 return cls
0 commit comments