Skip to content

Commit 755dcfb

Browse files
authored
fix: raise for missing context keys (#231)
1 parent 5969ef8 commit 755dcfb

2 files changed

Lines changed: 9 additions & 2 deletions

File tree

tests/providers/test_context_resources.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,6 +1226,12 @@ def test_fetch_context_item_raises() -> None:
12261226
with pytest.raises(KeyError):
12271227
fetch_context_item("s", raise_on_not_found=True)
12281228

1229+
with container_context(global_context={"present": None}):
1230+
assert fetch_context_item("present", default="fallback") is None
1231+
with pytest.raises(KeyError, match="Key `missing` not found"):
1232+
fetch_context_item("missing", raise_on_not_found=True)
1233+
assert fetch_context_item("missing", default="fallback") == "fallback"
1234+
12291235

12301236
def test_context_scope_hash() -> None:
12311237
assert ContextScope("TEST") == ContextScope("TEST")

that_depends/providers/context_resources.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,9 @@ def fetch_context_item(key: str, default: typing.Any = None, raise_on_not_found:
241241
```
242242
243243
"""
244-
if context := _get_container_context():
245-
return context.get(key, default)
244+
context = _get_container_context()
245+
if context is not None and key in context:
246+
return context[key]
246247
if raise_on_not_found:
247248
msg = f"Key `{key}` not found in global context."
248249
raise KeyError(msg)

0 commit comments

Comments
 (0)