diff --git a/modelx/core/model.py b/modelx/core/model.py index 8c0e8ec..beea8bf 100644 --- a/modelx/core/model.py +++ b/modelx/core/model.py @@ -1437,8 +1437,9 @@ def change_cells_formula(self, cells, func): define = True for space in self._get_subs(cells.parent, skip_self=False): c = space.cells[cells.name] - if c is not cells and c.is_defined(): - break # Stop when sub cells is defined + if (c is not cells and c.is_defined() and + self.get_deriv_bases(c, defined_only=True)[0] is cells): + continue # Skip when c's base is not cells space.clear_subs_rootitems() space.cells[cells.name].on_change_formula(func, define) define = False # Do not define derived cells diff --git a/modelx/tests/core/cells/test_formula.py b/modelx/tests/core/cells/test_formula.py index 9ba60b8..3a630bd 100644 --- a/modelx/tests/core/cells/test_formula.py +++ b/modelx/tests/core/cells/test_formula.py @@ -105,4 +105,29 @@ def test_set_formula_with_defined_sub(sample_for_rename_and_formula): assert not len(foo) # assert len(sub1.Child1.Foo) # Not Cleared - # assert sub1.Child1.Foo(1) == 3 # Not Changed \ No newline at end of file + # assert sub1.Child1.Foo(1) == 3 # Not Changed + + +def test_set_base_formula_with_defined_sub(): + """Setting the formula of a base cells not affecting its sub + + https://github.com/fumitoh/modelx/issues/141 + + Base----foo + +----Sub1---foo(defined) + +----Sub2---foo(derived) + """ + m = mx.new_model() + base = m.new_space('Base') + sub1 = m.new_space('Sub1', bases=base) + sub2 = m.new_space('Sub2', bases=base) + + sub1.new_cells('foo') + base.new_cells('foo') + assert 'foo' in sub2 + + @mx.defcells(space=base) + def foo(): + return "base" + + assert sub2.foo() == "base" \ No newline at end of file