anydi 0.36.1a3__py3-none-any.whl → 0.36.1a5__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
anydi/_container.py CHANGED
@@ -36,9 +36,7 @@ from ._utils import (
36
36
  get_full_qualname,
37
37
  get_typed_parameters,
38
38
  import_string,
39
- is_async_context_manager,
40
39
  is_builtin_type,
41
- is_context_manager,
42
40
  run_async,
43
41
  )
44
42
 
@@ -555,10 +553,7 @@ class Container:
555
553
  cm = contextlib.contextmanager(provider.call)(**provider_kwargs)
556
554
  return context.enter(cm)
557
555
 
558
- instance = provider.call(**provider_kwargs)
559
- if context is not None and is_context_manager(instance):
560
- context.enter(instance)
561
- return instance
556
+ return provider.call(**provider_kwargs)
562
557
 
563
558
  async def _acreate_instance(
564
559
  self, provider: Provider, context: InstanceContext | None, /, **defaults: Any
@@ -569,10 +564,7 @@ class Container:
569
564
  )
570
565
 
571
566
  if provider.is_coroutine:
572
- instance = await provider.call(**provider_kwargs)
573
- if context is not None and is_async_context_manager(instance):
574
- await context.aenter(instance)
575
- return instance
567
+ return await provider.call(**provider_kwargs)
576
568
 
577
569
  if provider.is_async_generator:
578
570
  if context is None:
@@ -592,10 +584,7 @@ class Container:
592
584
 
593
585
  return await run_async(_create)
594
586
 
595
- instance = await run_async(provider.call, **provider_kwargs)
596
- if context is not None and is_async_context_manager(instance):
597
- await context.aenter(instance)
598
- return instance
587
+ return await run_async(provider.call, **provider_kwargs)
599
588
 
600
589
  def _get_provided_kwargs(
601
590
  self, provider: Provider, context: InstanceContext | None, /, **defaults: Any
@@ -618,25 +607,27 @@ class Container:
618
607
  **defaults: Any,
619
608
  ) -> Any:
620
609
  """Retrieve an instance of a dependency from the scoped context."""
610
+
611
+ # Try to get instance from defaults
621
612
  if parameter.name in defaults:
622
- return defaults[parameter.name]
613
+ instance = defaults[parameter.name]
623
614
 
624
- # Get instance from overrides or context cache
625
- if context and parameter.annotation in context:
626
- return context[parameter.annotation]
615
+ # Try to get instance from context
616
+ elif context and parameter.annotation in context:
617
+ instance = context[parameter.annotation]
627
618
 
628
- # Resolve the instance
629
- try:
630
- instance = self._resolve_parameter(provider, parameter)
631
- except LookupError:
632
- if parameter.default is inspect.Parameter.empty:
633
- raise
634
- instance = parameter.default
619
+ # Resolve new instance
620
+ else:
621
+ try:
622
+ instance = self._resolve_parameter(provider, parameter)
623
+ except LookupError:
624
+ if parameter.default is inspect.Parameter.empty:
625
+ raise
626
+ instance = parameter.default
635
627
 
636
628
  # Wrap the instance in a proxy for testing
637
629
  if self.testing:
638
630
  return InstanceProxy(interface=parameter.annotation, instance=instance)
639
-
640
631
  return instance
641
632
 
642
633
  async def _aget_provided_kwargs(
@@ -660,20 +651,23 @@ class Container:
660
651
  **defaults: Any,
661
652
  ) -> Any:
662
653
  """Asynchronously retrieve an instance of a dependency from the context."""
654
+
655
+ # Try to get instance from defaults
663
656
  if parameter.name in defaults:
664
- return defaults[parameter.name]
657
+ instance = defaults[parameter.name]
665
658
 
666
- # Get instance from overrides or context cache
667
- if context and parameter.annotation in context:
668
- return context[parameter.annotation]
659
+ # Try to get instance from context
660
+ elif context and parameter.annotation in context:
661
+ instance = context[parameter.annotation]
669
662
 
670
- # Resolve the instance
671
- try:
672
- instance = await self._aresolve_parameter(provider, parameter)
673
- except LookupError:
674
- if parameter.default is inspect.Parameter.empty:
675
- raise
676
- instance = parameter.default
663
+ # Resolve new instance
664
+ else:
665
+ try:
666
+ instance = await self._aresolve_parameter(provider, parameter)
667
+ except LookupError:
668
+ if parameter.default is inspect.Parameter.empty:
669
+ raise
670
+ instance = parameter.default
677
671
 
678
672
  # Wrap the instance in a proxy for testing
679
673
  if self.testing:
@@ -727,6 +721,9 @@ class Container:
727
721
  return self.resolve(_interface)
728
722
  raise LookupError
729
723
 
724
+ # Attach the resolver getter to the instance
725
+ instance.__resolver_getter__ = __resolver_getter__
726
+
730
727
  if not hasattr(instance.__class__, "__getattribute_patched__"):
731
728
 
732
729
  def __getattribute__(_self: Any, name: str) -> Any:
@@ -747,9 +744,6 @@ class Container:
747
744
  instance.__class__.__getattribute__ = __getattribute__
748
745
  instance.__class__.__getattribute_patched__ = True
749
746
 
750
- # Attach the resolver getter to the instance
751
- instance.__resolver_getter__ = __resolver_getter__
752
-
753
747
  return instance
754
748
 
755
749
  def is_resolved(self, interface: AnyInterface) -> bool:
anydi/_utils.py CHANGED
@@ -44,16 +44,6 @@ def is_builtin_type(tp: type[Any]) -> bool:
44
44
  return tp.__module__ == builtins.__name__
45
45
 
46
46
 
47
- def is_context_manager(obj: Any) -> bool:
48
- """Check if the given object is a context manager."""
49
- return hasattr(obj, "__enter__") and hasattr(obj, "__exit__")
50
-
51
-
52
- def is_async_context_manager(obj: Any) -> bool:
53
- """Check if the given object is an async context manager."""
54
- return hasattr(obj, "__aenter__") and hasattr(obj, "__aexit__")
55
-
56
-
57
47
  def get_typed_annotation(
58
48
  annotation: Any, globalns: dict[str, Any], module: Any = None
59
49
  ) -> Any:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: anydi
3
- Version: 0.36.1a3
3
+ Version: 0.36.1a5
4
4
  Summary: Dependency Injection library
5
5
  Home-page: https://github.com/antonrh/anydi
6
6
  License: MIT
@@ -1,9 +1,9 @@
1
1
  anydi/__init__.py,sha256=OfRg2EfXD65pHTGQKhfkABMwUhw5LvsuTQV_Tv4V4wk,501
2
- anydi/_container.py,sha256=iJy5GoGDvs7CmV7NsTtzfnl1k53wWky6IvT0vCLAD2k,38090
2
+ anydi/_container.py,sha256=7Qwreyqnc95M0mgDus6vQmyuIF5qLFTMaBoA7GoWELg,37772
3
3
  anydi/_context.py,sha256=7LV_SL4QWkJeiG7_4D9PZ5lmU-MPzhofxC95zCgY9Gc,2651
4
4
  anydi/_provider.py,sha256=W42y8wbsnWbb9B9gI-pnEa-lsz68nK0VIm55CJW3pWg,7457
5
5
  anydi/_types.py,sha256=Vttj9GTp9g0KKpK-uqolLfVZJPIM7f7_YL8bPlablcQ,1539
6
- anydi/_utils.py,sha256=INI0jNIXrJ6LS4zqJymMO2yUEobpxmBGASf4G_vR6AU,4378
6
+ anydi/_utils.py,sha256=gU5ccCC2JZpKOY84ZCJ3PHyaEGhTW82PkGAhS6_R_qI,4027
7
7
  anydi/ext/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
8
8
  anydi/ext/_utils.py,sha256=U6sRqWzccWUu7eMhbXX1NrwcaxitQF9cO1KxnKF37gw,2566
9
9
  anydi/ext/django/__init__.py,sha256=QI1IABCVgSDTUoh7M9WMECKXwB3xvh04HfQ9TOWw1Mk,223
@@ -22,8 +22,8 @@ anydi/ext/pytest_plugin.py,sha256=ShGhiZnP1KyMHhnc9Ci1RKAuHVhw628OTS2P2BLEOfc,50
22
22
  anydi/ext/starlette/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
23
23
  anydi/ext/starlette/middleware.py,sha256=9CQtGg5ZzUz2gFSzJr8U4BWzwNjK8XMctm3n52M77Z0,792
24
24
  anydi/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
25
- anydi-0.36.1a3.dist-info/LICENSE,sha256=V6rU8a8fv6o2jQ-7ODHs0XfDFimot8Q6Km6CylRIDTo,1069
26
- anydi-0.36.1a3.dist-info/METADATA,sha256=KacqHtuQbewO_xqZOchuioqBcgXuHcLiGjHUsp8hDDM,5066
27
- anydi-0.36.1a3.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
28
- anydi-0.36.1a3.dist-info/entry_points.txt,sha256=GmQblwzxFg42zva1HyBYJJ7TvrTIcSAGBHmyi3bvsi4,42
29
- anydi-0.36.1a3.dist-info/RECORD,,
25
+ anydi-0.36.1a5.dist-info/LICENSE,sha256=V6rU8a8fv6o2jQ-7ODHs0XfDFimot8Q6Km6CylRIDTo,1069
26
+ anydi-0.36.1a5.dist-info/METADATA,sha256=pmhLHtLYS6Hi90rx0etN5ViIkIJYGMeOopTfyKPCHhY,5066
27
+ anydi-0.36.1a5.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
28
+ anydi-0.36.1a5.dist-info/entry_points.txt,sha256=GmQblwzxFg42zva1HyBYJJ7TvrTIcSAGBHmyi3bvsi4,42
29
+ anydi-0.36.1a5.dist-info/RECORD,,