Skip to content

Commit

Permalink
(partial) enable noop mode to work without an outer context too
Browse files Browse the repository at this point in the history
  • Loading branch information
amakelov committed Jan 8, 2025
1 parent 201b809 commit b82e3a9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
6 changes: 4 additions & 2 deletions mandala/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -1207,13 +1207,15 @@ def __init__(self,):
pass

def __enter__(self) -> "Storage":
assert Context.current_context is not None, "A context must be active to use `noop`"
if Context.current_context is None:
return self
storage = Context.current_context.storage
res = storage(mode='noop')
return res.__enter__()

def __exit__(self, exc_type, exc_value, traceback):
assert Context.current_context is not None, "A context must be active to use `noop`"
if Context.current_context is None:
return
storage = Context.current_context.storage
res = storage(mode='run')
return res.__exit__(exc_type, exc_value, traceback)
Expand Down
7 changes: 6 additions & 1 deletion mandala/tests/test_modes.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,9 @@ def inc(x: int) -> int:
z = inc(20)
assert storage.mode == 'run'
assert y == 22
assert z == 21
assert z == 21

# now, test it without a context
with noop():
x = inc(20)
assert x == 21

0 comments on commit b82e3a9

Please # to comment.