-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #583 from mit-ll-responsible-ai/contextvar-support
Add `zen(..., run_in_context: bool)`
- Loading branch information
Showing
5 changed files
with
166 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
# Copyright (c) 2023 Massachusetts Institute of Technology | ||
# SPDX-License-Identifier: MIT | ||
|
||
import random | ||
from contextvars import ContextVar | ||
from typing import Any, Dict, Optional | ||
|
||
import pytest | ||
|
||
from hydra_zen import zen | ||
from hydra_zen.errors import HydraZenValidationError | ||
|
||
config: ContextVar[Optional[Dict[str, Any]]] = ContextVar("config", default=None) | ||
var: ContextVar[Dict[str, Any]] = ContextVar("var", default=dict()) | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def clean_context_vars(): | ||
assert config.get() is None | ||
assert var.get() == {} | ||
yield | ||
config.set(None) | ||
var.set({}) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"run_in_context", | ||
[ | ||
True, | ||
pytest.param(False, marks=pytest.mark.xfail), | ||
], | ||
) | ||
def test_context_isolation(run_in_context: bool): | ||
def foo(x: str, zen_cfg): | ||
config.set(zen_cfg) | ||
conf = var.get().copy() | ||
conf[str(random.randint(1, 100))] = random.randint(1, 100) | ||
var.set(conf) | ||
assert len(conf) == 1 | ||
|
||
zfoo = zen(foo, run_in_context=run_in_context) | ||
|
||
for letter in "ab": | ||
zfoo(dict(x=letter)) | ||
assert config.get() is None | ||
assert var.get() == dict() | ||
|
||
|
||
def test_async_func_run_in_context_not_supported(): | ||
async def foo(): | ||
... | ||
|
||
with pytest.raises(TypeError, match="not supported"): | ||
zen(foo, run_in_context=True) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"run_in_context", | ||
[ | ||
True, | ||
pytest.param(False, marks=pytest.mark.xfail), | ||
], | ||
) | ||
def test_pre_call_shares_context_with_wrapped_func(run_in_context: bool): | ||
assert var.get() == {} | ||
|
||
def pre_call(cfg): | ||
var.set({"swagger": 22}) | ||
|
||
def func(): | ||
assert var.get() == {"swagger": 22} | ||
|
||
zen(func, pre_call=pre_call, run_in_context=run_in_context)({}) | ||
assert var.get() == {} | ||
|
||
|
||
def test_pre_call_run_in_its_own_context_is_forbidden(): | ||
def f(x): | ||
... | ||
|
||
with pytest.raises(HydraZenValidationError): | ||
zen(f, pre_call=zen(f, run_in_context=True), run_in_context=True) | ||
|
||
|
||
def test_validation(): | ||
with pytest.raises(TypeError, match="must be type"): | ||
zen(lambda x: x, run_in_context=None) # type: ignore |