Skip to content

Commit

Permalink
change _util.deep_replace_property return value
Browse files Browse the repository at this point in the history
This patch changes the definition of the function decorated by
_util.deep_replace_property, from returning None if no replacement is made, to
returning the object itself. The new definition is a more natural choice for a
recursive procedure (recursion stops when a trivial loop is reached) and
results in better readable code for the casual observer.
  • Loading branch information
gertjanvanzwieten authored and Gertjan van Zwieten committed Mar 22, 2024
1 parent 1b70d73 commit 013eb10
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
4 changes: 2 additions & 2 deletions nutils/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,7 @@ class deep_replace_property:
Args
----
func
Callable which maps an object onto a new object, or ``None`` if no
Callable which maps an object onto a new object, or onto itself if no
replacement is made. It must have precisely one positional argument for
the object.
'''
Expand Down Expand Up @@ -983,7 +983,7 @@ def __get__(self, obj, objtype=None):
if isinstance(obj, self.recreate): # recreate object from rstack
f, nargs = obj
r = f(*[rstack.pop() for _ in range(nargs)])
if isinstance(r, self.owner) and (newr := self.func(r)) is not None:
if isinstance(r, self.owner) and (newr := self.func(r)) is not r:
fstack.append(newr) # recursion
else:
rstack.append(r)
Expand Down
8 changes: 6 additions & 2 deletions nutils/evaluable.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,9 @@ def _format_stack(self, values, e):
@util.deep_replace_property
def simplified(obj):
retval = obj._simplified()
if retval is not None and isinstance(obj, Array):
if retval is None:
return obj
if isinstance(obj, Array):
assert isinstance(retval, Array) and equalshape(retval.shape, obj.shape) and retval.dtype == obj.dtype, '{} --simplify--> {}'.format(obj, retval)
return retval

Expand All @@ -310,7 +312,9 @@ def optimized_for_numpy(self):
@util.deep_replace_property
def _optimized_for_numpy1(obj):
retval = obj._simplified() or obj._optimized_for_numpy()
if retval is not None and isinstance(obj, Array):
if retval is None:
return obj
if isinstance(obj, Array):
assert isinstance(retval, Array) and equalshape(retval.shape, obj.shape), '{0}._optimized_for_numpy or {0}._simplified resulted in shape change'.format(type(obj).__name__)
return retval

Expand Down
4 changes: 3 additions & 1 deletion tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,8 +540,10 @@ def simple(self):
self.called = True
if isinstance(self, replace.Ten):
return replace.Intermediate() # to test recursion
if isinstance(self, replace.Intermediate):
elif isinstance(self, replace.Intermediate):
return 10
else:
return self

class Ten(Base): pass
class Intermediate(Base): pass
Expand Down

0 comments on commit 013eb10

Please sign in to comment.