Skip to content

Commit ca5555a

Browse files
authored
Merge pull request #127 from modern-python/set-context
allow setting context after container creation
2 parents 3ad8276 + a151623 commit ca5555a

5 files changed

Lines changed: 24 additions & 4 deletions

File tree

‎README.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ supports the following:
1717
- Fully typed and tested
1818
- Integrations with `FastAPI`, `FastStream` and `LiteStar`
1919

20+
Usage examples:
21+
22+
- with LiteStar - [litestar-sqlalchemy-template](https://github.com/modern-python/litestar-sqlalchemy-template)
23+
- with FastAPI - [fastapi-sqlalchemy-template](https://github.com/modern-python/fastapi-sqlalchemy-template)
24+
2025
## 📚 [Documentation](https://modern-di.readthedocs.io)
2126

2227
## 📦 [PyPi](https://pypi.org/project/modern-di)

‎packages/modern-di/modern_di/container.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ def reset_override(
122122
self.cache_registry.clear_kwargs()
123123
return self.providers_registry.reset_override(dependency_name=dependency_name, dependency_type=dependency_type)
124124

125+
def set_context(self, context_type: type[types.T], obj: types.T) -> None:
126+
self.context_registry.set_context(context_type, obj)
127+
125128
def __deepcopy__(self, *_: object, **__: object) -> "typing_extensions.Self":
126129
"""Prevent cloning object."""
127130
return self
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
import dataclasses
22
import typing
33

4-
5-
T = typing.TypeVar("T")
4+
from modern_di import types
65

76

87
@dataclasses.dataclass(kw_only=True, slots=True, frozen=True)
98
class ContextRegistry:
109
context: dict[type[typing.Any], typing.Any]
1110

12-
def find_context(self, context_type: type[T]) -> T | None:
11+
def find_context(self, context_type: type[types.T]) -> types.T | None:
1312
if context_type and (context := self.context.get(context_type)):
14-
return typing.cast(T, context)
13+
return typing.cast(types.T, context)
1514

1615
return None
16+
17+
def set_context(self, context_type: type[types.T], obj: types.T) -> None:
18+
self.context[context_type] = obj

‎packages/modern-di/modern_di/types.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22

33

44
T_co = typing.TypeVar("T_co", covariant=True)
5+
T = typing.TypeVar("T")
56
P = typing.ParamSpec("P")
67
UNSET = object()

‎packages/modern-di/tests_core/providers/test_context_provider.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ def test_context_provider() -> None:
1515
assert instance1 is instance2 is now
1616

1717

18+
def test_context_provider_set_context_after_creation() -> None:
19+
now = datetime.datetime.now(tz=datetime.timezone.utc)
20+
app_container = Container()
21+
app_container.set_context(datetime.datetime, now)
22+
instance1 = app_container.resolve_provider(context_provider)
23+
instance2 = app_container.resolve_provider(context_provider)
24+
assert instance1 is instance2 is now
25+
26+
1827
def test_context_provider_not_found() -> None:
1928
app_container = Container()
2029
assert app_container.resolve_provider(context_provider) is None

0 commit comments

Comments
 (0)