File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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 )
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 11import dataclasses
22import 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 )
98class 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
Original file line number Diff line number Diff line change 22
33
44T_co = typing .TypeVar ("T_co" , covariant = True )
5+ T = typing .TypeVar ("T" )
56P = typing .ParamSpec ("P" )
67UNSET = object ()
Original file line number Diff line number Diff 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+
1827def test_context_provider_not_found () -> None :
1928 app_container = Container ()
2029 assert app_container .resolve_provider (context_provider ) is None
You can’t perform that action at this time.
0 commit comments