@@ -216,3 +216,74 @@ def test_func(
216216 assert id (value [0 ]) != id (value [1 ])
217217 assert isinstance (value [0 ], TestClass )
218218 assert isinstance (value [1 ], TestClass )
219+
220+
221+ def test_skip_not_decorated_managers () -> None :
222+ """
223+ Test that synct context skip context managers.
224+
225+ Tests that even is class implements a context manager,
226+ it won't be called during the context resolution,
227+ because it's not annotated with contextmanager decorator.
228+ """
229+
230+ class TestCM :
231+ def __init__ (self ) -> None :
232+ self .opened = False
233+
234+ def __enter__ (self ) -> None :
235+ self .opened = True
236+
237+ def __exit__ (self , * args : object ) -> None :
238+ pass
239+
240+ test_cm = TestCM ()
241+
242+ def get_test_cm () -> TestCM :
243+ nonlocal test_cm
244+ return test_cm
245+
246+ def target (cm : Annotated [TestCM , Depends (get_test_cm )]) -> None :
247+ pass
248+
249+ graph = DependencyGraph (target = target )
250+ with graph .sync_ctx () as ctx :
251+ kwargs = ctx .resolve_kwargs ()
252+ assert kwargs ["cm" ] == test_cm
253+ assert not test_cm .opened
254+
255+
256+ @pytest .mark .anyio
257+ async def test_skip_not_decorated_async_managers () -> None :
258+ """
259+ Test that synct context skip context managers.
260+
261+ Tests that even is class implements a context manager,
262+ it won't be called during the context resolution,
263+ because it's not annotated with contextmanager decorator.
264+ """
265+
266+ class TestACM :
267+ def __init__ (self ) -> None :
268+ self .opened = False
269+
270+ async def __aenter__ (self ) -> None :
271+ self .opened = True
272+
273+ async def __aexit__ (self , * args : object ) -> None :
274+ pass
275+
276+ test_acm = TestACM ()
277+
278+ def get_test_acm () -> TestACM :
279+ nonlocal test_acm
280+ return test_acm
281+
282+ def target (acm : Annotated [TestACM , Depends (get_test_acm )]) -> None :
283+ pass
284+
285+ graph = DependencyGraph (target = target )
286+ async with graph .async_ctx () as ctx :
287+ kwargs = await ctx .resolve_kwargs ()
288+ assert kwargs ["acm" ] == test_acm
289+ assert not test_acm .opened
0 commit comments