anydi 0.36.0a0__tar.gz → 0.36.1a0__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.
- {anydi-0.36.0a0 → anydi-0.36.1a0}/PKG-INFO +1 -1
- {anydi-0.36.0a0 → anydi-0.36.1a0}/anydi/_container.py +33 -40
- {anydi-0.36.0a0 → anydi-0.36.1a0}/pyproject.toml +1 -1
- {anydi-0.36.0a0 → anydi-0.36.1a0}/LICENSE +0 -0
- {anydi-0.36.0a0 → anydi-0.36.1a0}/README.md +0 -0
- {anydi-0.36.0a0 → anydi-0.36.1a0}/anydi/__init__.py +0 -0
- {anydi-0.36.0a0 → anydi-0.36.1a0}/anydi/_context.py +0 -0
- {anydi-0.36.0a0 → anydi-0.36.1a0}/anydi/_provider.py +0 -0
- {anydi-0.36.0a0 → anydi-0.36.1a0}/anydi/_types.py +0 -0
- {anydi-0.36.0a0 → anydi-0.36.1a0}/anydi/_utils.py +0 -0
- {anydi-0.36.0a0 → anydi-0.36.1a0}/anydi/ext/__init__.py +0 -0
- {anydi-0.36.0a0 → anydi-0.36.1a0}/anydi/ext/_utils.py +0 -0
- {anydi-0.36.0a0 → anydi-0.36.1a0}/anydi/ext/django/__init__.py +0 -0
- {anydi-0.36.0a0 → anydi-0.36.1a0}/anydi/ext/django/_container.py +0 -0
- {anydi-0.36.0a0 → anydi-0.36.1a0}/anydi/ext/django/_settings.py +0 -0
- {anydi-0.36.0a0 → anydi-0.36.1a0}/anydi/ext/django/_utils.py +0 -0
- {anydi-0.36.0a0 → anydi-0.36.1a0}/anydi/ext/django/apps.py +0 -0
- {anydi-0.36.0a0 → anydi-0.36.1a0}/anydi/ext/django/middleware.py +0 -0
- {anydi-0.36.0a0 → anydi-0.36.1a0}/anydi/ext/django/ninja/__init__.py +0 -0
- {anydi-0.36.0a0 → anydi-0.36.1a0}/anydi/ext/django/ninja/_operation.py +0 -0
- {anydi-0.36.0a0 → anydi-0.36.1a0}/anydi/ext/django/ninja/_signature.py +0 -0
- {anydi-0.36.0a0 → anydi-0.36.1a0}/anydi/ext/fastapi.py +0 -0
- {anydi-0.36.0a0 → anydi-0.36.1a0}/anydi/ext/faststream.py +0 -0
- {anydi-0.36.0a0 → anydi-0.36.1a0}/anydi/ext/pydantic_settings.py +0 -0
- {anydi-0.36.0a0 → anydi-0.36.1a0}/anydi/ext/pytest_plugin.py +0 -0
- {anydi-0.36.0a0 → anydi-0.36.1a0}/anydi/ext/starlette/__init__.py +0 -0
- {anydi-0.36.0a0 → anydi-0.36.1a0}/anydi/ext/starlette/middleware.py +0 -0
- {anydi-0.36.0a0 → anydi-0.36.1a0}/anydi/py.typed +0 -0
|
@@ -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
|
|
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
|
|
463
|
-
provider, context=context
|
|
464
|
-
)
|
|
459
|
+
instance = self._get_or_create_instance(provider, context)
|
|
465
460
|
else:
|
|
466
|
-
instance
|
|
467
|
-
|
|
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
|
|
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
|
|
492
|
-
provider, context=context
|
|
493
|
-
)
|
|
481
|
+
instance = await self._aget_or_create_instance(provider, context)
|
|
494
482
|
else:
|
|
495
|
-
instance
|
|
496
|
-
|
|
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
|
-
) ->
|
|
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
|
-
|
|
540
|
-
|
|
541
|
-
|
|
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
|
-
) ->
|
|
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
|
-
|
|
552
|
-
|
|
553
|
-
|
|
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
|
|
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) ->
|
|
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(
|
|
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:
|
|
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
|
|
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
|
|
File without changes
|
|
File without changes
|
|
File without changes
|