Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions modelx/core/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
EditableParent,
)
from modelx.core.space import UserSpaceImpl
from modelx.core.binding.namespace import BaseNamespace
from modelx.core.binding.namespace import BaseNamespace, NamespaceServer
from modelx.core.formula import NULL_FORMULA
from modelx.core.util import is_valid_name
from modelx.core.execution.trace import TraceManager
Expand Down Expand Up @@ -1802,10 +1802,8 @@ def new_space(
formula=None,
refs=None,
source=None,
is_derived=False,
prefix="",
doc=None,
container=None
doc=None
):
"""Create a new child space.

Expand Down Expand Up @@ -1862,18 +1860,19 @@ def new_space(
for n in nx.descendants(self._graph, node):
self._graph.get_mro(n)

if container is None:
container = parent.named_spaces

space = UserSpaceImpl(
parent,
name,
container,
container=parent.named_spaces,
formula=formula,
refs=refs,
source=source,
doc=doc
)

if isinstance(parent, NamespaceServer):
parent.on_notify(parent.named_spaces) # Fix: bug GH203

self._graph.nodes[node]["space"] = space
self._graph.nodes[node]["state"] = "created"

Expand All @@ -1886,7 +1885,7 @@ def new_space(
try:
self._instructions.execute()
except BaseException:
del container[name]
del parent.named_spaces[name]
raise

self._update_manager()
Expand Down Expand Up @@ -2030,10 +2029,8 @@ def _copy_space_recursively(
formula=source.formula,
refs={k: v.interface for k, v in source.own_refs.items()},
source=source.source,
is_derived=False,
prefix="",
doc=source.doc,
container=None
doc=source.doc
)

for cells in source.cells.values():
Expand Down
21 changes: 21 additions & 0 deletions modelx/tests/core/space/test_new_space.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import modelx as mx


def test_new_space_when_parent_has_itemspace():

# https://github.com/fumitoh/modelx/issues/203

m = mx.new_model()
space = m.new_space()
space.parameters = ('x', )
space[1] # create an itemspace

assert 1 in space.itemspaces
space.new_space('SpaceA')

assert not space.itemspaces # must be empty
assert 'SpaceA' in dir(space) # must have SpaceA in its namespace




Loading