Skip to content

Commit 16d7623

Browse files
committed
override __getattr__, not __getattribute__.. fixes attr proxy
1 parent 689130b commit 16d7623

File tree

2 files changed

+19
-32
lines changed

2 files changed

+19
-32
lines changed

flopy4/__init__.py

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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

242241
def 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

test/test_component.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,14 @@ def test_registry():
1313

1414

1515
def test_sim(benchmark):
16-
gwf = None
16+
sim = Sim()
17+
tdis = Tdis(sim, nper=1, perioddata=[Tdis.PeriodData()])
18+
gwf = Gwf(sim)
19+
dis = Dis(gwf)
20+
ic = Ic(gwf, strt=1.0)
21+
oc = Oc(gwf, saverecord=[Oc.Steps_("all")])
22+
npf = Npf(gwf, icelltype=0, k=1.0)
1723

18-
def make_sim():
19-
sim = Sim()
20-
tdis = Tdis(sim, nper=1, perioddata=[Tdis.PeriodData()])
21-
gwf = Gwf(sim)
22-
dis = Dis(gwf)
23-
ic = Ic(gwf, strt=1.0)
24-
oc = Oc(gwf, saverecord=[Oc.Steps_("all")])
25-
npf = Npf(gwf, icelltype=0, k=1.0)
26-
return sim
27-
28-
sim = benchmark(make_sim)
2924
assert isinstance(sim.data, DataTree)
3025
sim.data # view the tree
3126

@@ -39,6 +34,6 @@ def make_sim():
3934
assert np.array_equal(
4035
sim.data.children["gwf"].children["npf"].k, np.ones((4))
4136
)
42-
37+
assert np.array_equal(npf.k, npf.data.k)
4338
# TODO: figure out how to deduplicate trees. components proxy root?
4439
# assert gwf.parent.data.children["gwf"].children["npf"] is npf.data

0 commit comments

Comments
 (0)