anydi 0.36.0a0__tar.gz → 0.36.1a1__tar.gz

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.
Files changed (28) hide show
  1. {anydi-0.36.0a0 → anydi-0.36.1a1}/PKG-INFO +1 -1
  2. {anydi-0.36.0a0 → anydi-0.36.1a1}/anydi/_container.py +34 -43
  3. {anydi-0.36.0a0 → anydi-0.36.1a1}/pyproject.toml +1 -1
  4. {anydi-0.36.0a0 → anydi-0.36.1a1}/LICENSE +0 -0
  5. {anydi-0.36.0a0 → anydi-0.36.1a1}/README.md +0 -0
  6. {anydi-0.36.0a0 → anydi-0.36.1a1}/anydi/__init__.py +0 -0
  7. {anydi-0.36.0a0 → anydi-0.36.1a1}/anydi/_context.py +0 -0
  8. {anydi-0.36.0a0 → anydi-0.36.1a1}/anydi/_provider.py +0 -0
  9. {anydi-0.36.0a0 → anydi-0.36.1a1}/anydi/_types.py +0 -0
  10. {anydi-0.36.0a0 → anydi-0.36.1a1}/anydi/_utils.py +0 -0
  11. {anydi-0.36.0a0 → anydi-0.36.1a1}/anydi/ext/__init__.py +0 -0
  12. {anydi-0.36.0a0 → anydi-0.36.1a1}/anydi/ext/_utils.py +0 -0
  13. {anydi-0.36.0a0 → anydi-0.36.1a1}/anydi/ext/django/__init__.py +0 -0
  14. {anydi-0.36.0a0 → anydi-0.36.1a1}/anydi/ext/django/_container.py +0 -0
  15. {anydi-0.36.0a0 → anydi-0.36.1a1}/anydi/ext/django/_settings.py +0 -0
  16. {anydi-0.36.0a0 → anydi-0.36.1a1}/anydi/ext/django/_utils.py +0 -0
  17. {anydi-0.36.0a0 → anydi-0.36.1a1}/anydi/ext/django/apps.py +0 -0
  18. {anydi-0.36.0a0 → anydi-0.36.1a1}/anydi/ext/django/middleware.py +0 -0
  19. {anydi-0.36.0a0 → anydi-0.36.1a1}/anydi/ext/django/ninja/__init__.py +0 -0
  20. {anydi-0.36.0a0 → anydi-0.36.1a1}/anydi/ext/django/ninja/_operation.py +0 -0
  21. {anydi-0.36.0a0 → anydi-0.36.1a1}/anydi/ext/django/ninja/_signature.py +0 -0
  22. {anydi-0.36.0a0 → anydi-0.36.1a1}/anydi/ext/fastapi.py +0 -0
  23. {anydi-0.36.0a0 → anydi-0.36.1a1}/anydi/ext/faststream.py +0 -0
  24. {anydi-0.36.0a0 → anydi-0.36.1a1}/anydi/ext/pydantic_settings.py +0 -0
  25. {anydi-0.36.0a0 → anydi-0.36.1a1}/anydi/ext/pytest_plugin.py +0 -0
  26. {anydi-0.36.0a0 → anydi-0.36.1a1}/anydi/ext/starlette/__init__.py +0 -0
  27. {anydi-0.36.0a0 → anydi-0.36.1a1}/anydi/ext/starlette/middleware.py +0 -0
  28. {anydi-0.36.0a0 → anydi-0.36.1a1}/anydi/py.typed +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: anydi
3
- Version: 0.36.0a0
3
+ Version: 0.36.1a1
4
4
  Summary: Dependency Injection library
5
5
  Home-page: https://github.com/antonrh/anydi
6
6
  License: MIT
@@ -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
@@ -682,9 +664,7 @@ class Container:
682
664
  return defaults[parameter.name]
683
665
 
684
666
  # Get instance from overrides or context cache
685
- if parameter.annotation in self._override_instances:
686
- return self._override_instances[parameter.annotation]
687
- elif context and parameter.annotation in context:
667
+ if context and parameter.annotation in context:
688
668
  return context[parameter.annotation]
689
669
 
690
670
  # Resolve the instance
@@ -724,10 +704,16 @@ class Container:
724
704
  "or set in the scoped context."
725
705
  )
726
706
 
727
- def _patch_test_resolver(self, instance: Any) -> None:
707
+ def _patch_test_resolver(self, interface: type[Any], instance: Any) -> Any:
728
708
  """Patch the test resolver for the instance."""
709
+ if interface in self._override_instances:
710
+ return self._override_instances[interface]
711
+
729
712
  if not hasattr(instance, "__dict__"):
730
- return
713
+ return instance
714
+
715
+ if hasattr(instance, "__patched__"):
716
+ return instance
731
717
 
732
718
  wrapped = {
733
719
  name: value.interface
@@ -738,8 +724,9 @@ class Container:
738
724
  # Custom resolver function
739
725
  def _resolver(_self: Any, _name: str) -> Any:
740
726
  if _name in wrapped:
727
+ _interface = wrapped[_name]
741
728
  # Resolve the dependency if it's wrapped
742
- return self.resolve(wrapped[_name])
729
+ return self.resolve(_interface)
743
730
  # Fall back to default behavior
744
731
  return object.__getattribute__(_self, _name)
745
732
 
@@ -747,6 +734,10 @@ class Container:
747
734
  if wrapped:
748
735
  instance.__class__.__getattribute__ = _resolver
749
736
 
737
+ instance.__patched__ = True
738
+
739
+ return instance
740
+
750
741
  def is_resolved(self, interface: AnyInterface) -> bool:
751
742
  """Check if an instance by interface exists."""
752
743
  try:
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "anydi"
3
- version = "0.36.0a0"
3
+ version = "0.36.1a1"
4
4
  description = "Dependency Injection library"
5
5
  authors = ["Anton Ruhlov <antonruhlov@gmail.com>"]
6
6
  license = "MIT"
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes