anydi 0.36.0a0__py3-none-any.whl → 0.36.1a0__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
@@ -449,25 +449,18 @@ class Container:
449
449
 
450
450
  def resolve(self, interface: type[T]) -> T:
451
451
  """Resolve an instance by interface."""
452
- if interface in self._override_instances:
453
- return cast(T, self._override_instances[interface])
454
-
455
452
  provider = self._get_or_register_provider(interface, None)
456
453
  if provider.scope == "transient":
457
- instance, created = self._create_instance(provider, None), True
454
+ instance = self._create_instance(provider, None)
458
455
  else:
459
456
  context = self._get_scoped_context(provider.scope)
460
457
  if provider.scope == "singleton":
461
458
  with self._singleton_lock:
462
- instance, created = self._get_or_create_instance(
463
- provider, context=context
464
- )
459
+ instance = self._get_or_create_instance(provider, context)
465
460
  else:
466
- instance, created = self._get_or_create_instance(
467
- provider, context=context
468
- )
469
- if self.testing and created:
470
- self._patch_test_resolver(instance)
461
+ instance = self._get_or_create_instance(provider, context)
462
+ if self.testing:
463
+ instance = self._patch_test_resolver(provider.interface, instance)
471
464
  return cast(T, instance)
472
465
 
473
466
  @overload
@@ -478,25 +471,18 @@ class Container:
478
471
 
479
472
  async def aresolve(self, interface: type[T]) -> T:
480
473
  """Resolve an instance by interface asynchronously."""
481
- if interface in self._override_instances:
482
- return cast(T, self._override_instances[interface])
483
-
484
474
  provider = self._get_or_register_provider(interface, None)
485
475
  if provider.scope == "transient":
486
- instance, created = await self._acreate_instance(provider, None), True
476
+ instance = await self._acreate_instance(provider, None)
487
477
  else:
488
478
  context = self._get_scoped_context(provider.scope)
489
479
  if provider.scope == "singleton":
490
480
  async with self._singleton_async_lock:
491
- instance, created = await self._aget_or_create_instance(
492
- provider, context=context
493
- )
481
+ instance = await self._aget_or_create_instance(provider, context)
494
482
  else:
495
- instance, created = await self._aget_or_create_instance(
496
- provider, context=context
497
- )
498
- if self.testing and created:
499
- self._patch_test_resolver(instance)
483
+ instance = await self._aget_or_create_instance(provider, context)
484
+ if self.testing:
485
+ instance = self._patch_test_resolver(interface, instance)
500
486
  return cast(T, instance)
501
487
 
502
488
  def create(self, interface: type[T], **defaults: Any) -> T:
@@ -531,27 +517,25 @@ class Container:
531
517
 
532
518
  def _get_or_create_instance(
533
519
  self, provider: Provider, context: InstanceContext
534
- ) -> tuple[Any, bool]:
520
+ ) -> Any:
535
521
  """Get an instance of a dependency from the scoped context."""
536
522
  instance = context.get(provider.interface)
537
523
  if instance is None:
538
524
  instance = self._create_instance(provider, context)
539
- if not self._override_instances:
540
- context.set(provider.interface, instance)
541
- return instance, True
542
- return instance, False
525
+ context.set(provider.interface, instance)
526
+ return instance
527
+ return instance
543
528
 
544
529
  async def _aget_or_create_instance(
545
530
  self, provider: Provider, context: InstanceContext
546
- ) -> tuple[Any, bool]:
531
+ ) -> Any:
547
532
  """Get an async instance of a dependency from the scoped context."""
548
533
  instance = context.get(provider.interface)
549
534
  if instance is None:
550
535
  instance = await self._acreate_instance(provider, context)
551
- if not self._override_instances:
552
- context.set(provider.interface, instance)
553
- return instance, True
554
- return instance, False
536
+ context.set(provider.interface, instance)
537
+ return instance
538
+ return instance
555
539
 
556
540
  def _create_instance(
557
541
  self, provider: Provider, context: InstanceContext | None, /, **defaults: Any
@@ -638,9 +622,7 @@ class Container:
638
622
  return defaults[parameter.name]
639
623
 
640
624
  # Get instance from overrides or context cache
641
- if parameter.annotation in self._override_instances:
642
- return self._override_instances[parameter.annotation]
643
- elif context and parameter.annotation in context:
625
+ if context and parameter.annotation in context:
644
626
  return context[parameter.annotation]
645
627
 
646
628
  # Resolve the instance
@@ -724,10 +706,16 @@ class Container:
724
706
  "or set in the scoped context."
725
707
  )
726
708
 
727
- def _patch_test_resolver(self, instance: Any) -> None:
709
+ def _patch_test_resolver(self, interface: type[Any], instance: Any) -> Any:
728
710
  """Patch the test resolver for the instance."""
711
+ if interface in self._override_instances:
712
+ return self._override_instances[interface]
713
+
729
714
  if not hasattr(instance, "__dict__"):
730
- return
715
+ return instance
716
+
717
+ if hasattr(instance, "__patched__"):
718
+ return instance
731
719
 
732
720
  wrapped = {
733
721
  name: value.interface
@@ -738,8 +726,9 @@ class Container:
738
726
  # Custom resolver function
739
727
  def _resolver(_self: Any, _name: str) -> Any:
740
728
  if _name in wrapped:
729
+ _interface = wrapped[_name]
741
730
  # Resolve the dependency if it's wrapped
742
- return self.resolve(wrapped[_name])
731
+ return self.resolve(_interface)
743
732
  # Fall back to default behavior
744
733
  return object.__getattribute__(_self, _name)
745
734
 
@@ -747,6 +736,10 @@ class Container:
747
736
  if wrapped:
748
737
  instance.__class__.__getattribute__ = _resolver
749
738
 
739
+ instance.__patched__ = True
740
+
741
+ return instance
742
+
750
743
  def is_resolved(self, interface: AnyInterface) -> bool:
751
744
  """Check if an instance by interface exists."""
752
745
  try:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: anydi
3
- Version: 0.36.0a0
3
+ Version: 0.36.1a0
4
4
  Summary: Dependency Injection library
5
5
  Home-page: https://github.com/antonrh/anydi
6
6
  License: MIT
@@ -1,5 +1,5 @@
1
1
  anydi/__init__.py,sha256=OfRg2EfXD65pHTGQKhfkABMwUhw5LvsuTQV_Tv4V4wk,501
2
- anydi/_container.py,sha256=2MvGilZn2qV1kyNiOkT9wiIbVAbaN5m1XvEyPUDj2ks,38028
2
+ anydi/_container.py,sha256=zqra2aBaSxWYjcQscxWIzS2dhdIaNw7O4OS45YWiYi8,37586
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
@@ -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.0a0.dist-info/LICENSE,sha256=V6rU8a8fv6o2jQ-7ODHs0XfDFimot8Q6Km6CylRIDTo,1069
26
- anydi-0.36.0a0.dist-info/METADATA,sha256=iM_-xJgNAYAACfpk7h4RHI3F1lPz7dQj_CWxEho8VHs,5066
27
- anydi-0.36.0a0.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
28
- anydi-0.36.0a0.dist-info/entry_points.txt,sha256=GmQblwzxFg42zva1HyBYJJ7TvrTIcSAGBHmyi3bvsi4,42
29
- anydi-0.36.0a0.dist-info/RECORD,,
25
+ anydi-0.36.1a0.dist-info/LICENSE,sha256=V6rU8a8fv6o2jQ-7ODHs0XfDFimot8Q6Km6CylRIDTo,1069
26
+ anydi-0.36.1a0.dist-info/METADATA,sha256=L4tC2RFRJJEaRxq67bb1-iDWZsLTDZ4BqHnrbP_7urQ,5066
27
+ anydi-0.36.1a0.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
28
+ anydi-0.36.1a0.dist-info/entry_points.txt,sha256=GmQblwzxFg42zva1HyBYJJ7TvrTIcSAGBHmyi3bvsi4,42
29
+ anydi-0.36.1a0.dist-info/RECORD,,