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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ supports the following:
- Fully typed and tested
- Integrations with `FastAPI`, `FastStream` and `LiteStar`

Usage examples:

- with LiteStar - [litestar-sqlalchemy-template](https://github.com/modern-python/litestar-sqlalchemy-template)
- with FastAPI - [fastapi-sqlalchemy-template](https://github.com/modern-python/fastapi-sqlalchemy-template)

## 📚 [Documentation](https://modern-di.readthedocs.io)

## 📦 [PyPi](https://pypi.org/project/modern-di)
Expand Down
3 changes: 3 additions & 0 deletions packages/modern-di/modern_di/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ def reset_override(
self.cache_registry.clear_kwargs()
return self.providers_registry.reset_override(dependency_name=dependency_name, dependency_type=dependency_type)

def set_context(self, context_type: type[types.T], obj: types.T) -> None:
self.context_registry.set_context(context_type, obj)

def __deepcopy__(self, *_: object, **__: object) -> "typing_extensions.Self":
"""Prevent cloning object."""
return self
Expand Down
10 changes: 6 additions & 4 deletions packages/modern-di/modern_di/registries/context_registry.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import dataclasses
import typing


T = typing.TypeVar("T")
from modern_di import types


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

def find_context(self, context_type: type[T]) -> T | None:
def find_context(self, context_type: type[types.T]) -> types.T | None:
if context_type and (context := self.context.get(context_type)):
return typing.cast(T, context)
return typing.cast(types.T, context)

return None

def set_context(self, context_type: type[types.T], obj: types.T) -> None:
self.context[context_type] = obj
1 change: 1 addition & 0 deletions packages/modern-di/modern_di/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@


T_co = typing.TypeVar("T_co", covariant=True)
T = typing.TypeVar("T")
P = typing.ParamSpec("P")
UNSET = object()
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ def test_context_provider() -> None:
assert instance1 is instance2 is now


def test_context_provider_set_context_after_creation() -> None:
now = datetime.datetime.now(tz=datetime.timezone.utc)
app_container = Container()
app_container.set_context(datetime.datetime, now)
instance1 = app_container.resolve_provider(context_provider)
instance2 = app_container.resolve_provider(context_provider)
assert instance1 is instance2 is now


def test_context_provider_not_found() -> None:
app_container = Container()
assert app_container.resolve_provider(context_provider) is None
Expand Down