anydi 0.44.1__py3-none-any.whl → 0.45.1__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 +45 -48
- anydi/testing.py +14 -16
- {anydi-0.44.1.dist-info → anydi-0.45.1.dist-info}/METADATA +1 -1
- {anydi-0.44.1.dist-info → anydi-0.45.1.dist-info}/RECORD +7 -7
- {anydi-0.44.1.dist-info → anydi-0.45.1.dist-info}/WHEEL +0 -0
- {anydi-0.44.1.dist-info → anydi-0.45.1.dist-info}/entry_points.txt +0 -0
- {anydi-0.44.1.dist-info → anydi-0.45.1.dist-info}/licenses/LICENSE +0 -0
anydi/_container.py
CHANGED
|
@@ -399,9 +399,8 @@ class Container:
|
|
|
399
399
|
self._set_provider(provider)
|
|
400
400
|
return provider
|
|
401
401
|
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
) -> None:
|
|
402
|
+
@staticmethod
|
|
403
|
+
def _validate_provider_scope(scope: Scope, name: str, kind: ProviderKind) -> None:
|
|
405
404
|
"""Validate the provider scope."""
|
|
406
405
|
if scope not in (allowed_scopes := get_args(Scope)):
|
|
407
406
|
raise ValueError(
|
|
@@ -431,10 +430,8 @@ class Container:
|
|
|
431
430
|
) -> Provider:
|
|
432
431
|
"""Get or register a provider by interface."""
|
|
433
432
|
try:
|
|
434
|
-
return self.
|
|
435
|
-
except
|
|
436
|
-
if interface is inspect.Parameter.empty:
|
|
437
|
-
raise
|
|
433
|
+
return self._providers[interface]
|
|
434
|
+
except KeyError:
|
|
438
435
|
if inspect.isclass(interface) and not is_builtin_type(interface):
|
|
439
436
|
# Try to get defined scope
|
|
440
437
|
if is_provided(interface):
|
|
@@ -442,7 +439,12 @@ class Container:
|
|
|
442
439
|
else:
|
|
443
440
|
scope = parent_scope
|
|
444
441
|
return self._register_provider(interface, scope, interface, **defaults)
|
|
445
|
-
raise
|
|
442
|
+
raise LookupError(
|
|
443
|
+
f"The provider interface `{type_repr(interface)}` is either not "
|
|
444
|
+
"registered, not provided, or not set in the scoped context. "
|
|
445
|
+
"Please ensure that the provider interface is properly registered and "
|
|
446
|
+
"that the class is decorated with a scope before attempting to use it."
|
|
447
|
+
) from None
|
|
446
448
|
|
|
447
449
|
def _set_provider(self, provider: Provider) -> None:
|
|
448
450
|
"""Set a provider by interface."""
|
|
@@ -528,40 +530,34 @@ class Container:
|
|
|
528
530
|
del context[interface]
|
|
529
531
|
|
|
530
532
|
def _resolve_or_create(
|
|
531
|
-
self, interface:
|
|
532
|
-
) ->
|
|
533
|
+
self, interface: Any, create: bool, /, **defaults: Any
|
|
534
|
+
) -> Any:
|
|
533
535
|
"""Internal method to handle instance resolution and creation."""
|
|
534
536
|
provider = self._get_or_register_provider(interface, None, **defaults)
|
|
535
537
|
if provider.scope == "transient":
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
)
|
|
545
|
-
|
|
546
|
-
return cast(T, instance)
|
|
538
|
+
return self._create_instance(provider, None, **defaults)
|
|
539
|
+
context = self._get_instance_context(provider.scope)
|
|
540
|
+
with context.lock():
|
|
541
|
+
return (
|
|
542
|
+
self._get_or_create_instance(provider, context)
|
|
543
|
+
if not create
|
|
544
|
+
else self._create_instance(provider, context, **defaults)
|
|
545
|
+
)
|
|
547
546
|
|
|
548
547
|
async def _aresolve_or_create(
|
|
549
|
-
self, interface:
|
|
550
|
-
) ->
|
|
548
|
+
self, interface: Any, create: bool, /, **defaults: Any
|
|
549
|
+
) -> Any:
|
|
551
550
|
"""Internal method to handle instance resolution and creation asynchronously."""
|
|
552
551
|
provider = self._get_or_register_provider(interface, None, **defaults)
|
|
553
552
|
if provider.scope == "transient":
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
)
|
|
563
|
-
|
|
564
|
-
return cast(T, instance)
|
|
553
|
+
return await self._acreate_instance(provider, None, **defaults)
|
|
554
|
+
context = self._get_instance_context(provider.scope)
|
|
555
|
+
async with context.alock():
|
|
556
|
+
return (
|
|
557
|
+
await self._aget_or_create_instance(provider, context)
|
|
558
|
+
if not create
|
|
559
|
+
else await self._acreate_instance(provider, context, **defaults)
|
|
560
|
+
)
|
|
565
561
|
|
|
566
562
|
def _get_or_create_instance(
|
|
567
563
|
self, provider: Provider, context: InstanceContext
|
|
@@ -652,10 +648,9 @@ class Container:
|
|
|
652
648
|
"""Retrieve the arguments for a provider."""
|
|
653
649
|
provided_kwargs = {}
|
|
654
650
|
for parameter in provider.parameters:
|
|
655
|
-
|
|
651
|
+
provided_kwargs[parameter.name] = self._get_provider_instance(
|
|
656
652
|
provider, parameter, context, **defaults
|
|
657
653
|
)
|
|
658
|
-
provided_kwargs[parameter.name] = instance
|
|
659
654
|
return {**defaults, **provided_kwargs}
|
|
660
655
|
|
|
661
656
|
def _get_provider_instance(
|
|
@@ -665,12 +660,12 @@ class Container:
|
|
|
665
660
|
context: InstanceContext | None,
|
|
666
661
|
/,
|
|
667
662
|
**defaults: Any,
|
|
668
|
-
) ->
|
|
663
|
+
) -> Any:
|
|
669
664
|
"""Retrieve an instance of a dependency from the scoped context."""
|
|
670
665
|
|
|
671
666
|
# Try to get instance from defaults
|
|
672
667
|
if parameter.name in defaults:
|
|
673
|
-
return defaults[parameter.name]
|
|
668
|
+
return defaults[parameter.name]
|
|
674
669
|
|
|
675
670
|
# Try to get instance from context
|
|
676
671
|
elif context and parameter.annotation in context:
|
|
@@ -683,8 +678,8 @@ class Container:
|
|
|
683
678
|
except LookupError:
|
|
684
679
|
if parameter.default is inspect.Parameter.empty:
|
|
685
680
|
raise
|
|
686
|
-
return parameter.default
|
|
687
|
-
return instance
|
|
681
|
+
return parameter.default
|
|
682
|
+
return instance
|
|
688
683
|
|
|
689
684
|
async def _aget_provided_kwargs(
|
|
690
685
|
self, provider: Provider, context: InstanceContext | None, /, **defaults: Any
|
|
@@ -692,10 +687,9 @@ class Container:
|
|
|
692
687
|
"""Asynchronously retrieve the arguments for a provider."""
|
|
693
688
|
provided_kwargs = {}
|
|
694
689
|
for parameter in provider.parameters:
|
|
695
|
-
|
|
690
|
+
provided_kwargs[parameter.name] = await self._aget_provider_instance(
|
|
696
691
|
provider, parameter, context, **defaults
|
|
697
692
|
)
|
|
698
|
-
provided_kwargs[parameter.name] = instance
|
|
699
693
|
return {**defaults, **provided_kwargs}
|
|
700
694
|
|
|
701
695
|
async def _aget_provider_instance(
|
|
@@ -705,12 +699,12 @@ class Container:
|
|
|
705
699
|
context: InstanceContext | None,
|
|
706
700
|
/,
|
|
707
701
|
**defaults: Any,
|
|
708
|
-
) ->
|
|
702
|
+
) -> Any:
|
|
709
703
|
"""Asynchronously retrieve an instance of a dependency from the context."""
|
|
710
704
|
|
|
711
705
|
# Try to get instance from defaults
|
|
712
706
|
if parameter.name in defaults:
|
|
713
|
-
return defaults[parameter.name]
|
|
707
|
+
return defaults[parameter.name]
|
|
714
708
|
|
|
715
709
|
# Try to get instance from context
|
|
716
710
|
elif context and parameter.annotation in context:
|
|
@@ -723,20 +717,20 @@ class Container:
|
|
|
723
717
|
except LookupError:
|
|
724
718
|
if parameter.default is inspect.Parameter.empty:
|
|
725
719
|
raise
|
|
726
|
-
return parameter.default
|
|
727
|
-
return instance
|
|
720
|
+
return parameter.default
|
|
721
|
+
return instance
|
|
728
722
|
|
|
729
723
|
def _resolve_parameter(
|
|
730
724
|
self, provider: Provider, parameter: inspect.Parameter
|
|
731
725
|
) -> Any:
|
|
732
726
|
self._validate_resolvable_parameter(provider, parameter)
|
|
733
|
-
return self.
|
|
727
|
+
return self._resolve_or_create(parameter.annotation, False)
|
|
734
728
|
|
|
735
729
|
async def _aresolve_parameter(
|
|
736
730
|
self, provider: Provider, parameter: inspect.Parameter
|
|
737
731
|
) -> Any:
|
|
738
732
|
self._validate_resolvable_parameter(provider, parameter)
|
|
739
|
-
return await self.
|
|
733
|
+
return await self._aresolve_or_create(parameter.annotation, False)
|
|
740
734
|
|
|
741
735
|
def _validate_resolvable_parameter(
|
|
742
736
|
self, provider: Provider, parameter: inspect.Parameter
|
|
@@ -822,6 +816,9 @@ class Container:
|
|
|
822
816
|
self, call: Callable[..., Any], parameter: inspect.Parameter
|
|
823
817
|
) -> None:
|
|
824
818
|
"""Validate an injected parameter."""
|
|
819
|
+
# TODO: temporary disable until strict is enforced
|
|
820
|
+
return None
|
|
821
|
+
|
|
825
822
|
if parameter.annotation is inspect.Parameter.empty:
|
|
826
823
|
raise TypeError(
|
|
827
824
|
f"Missing `{type_repr(call)}` parameter `{parameter.name}` annotation."
|
anydi/testing.py
CHANGED
|
@@ -2,7 +2,7 @@ import contextlib
|
|
|
2
2
|
import inspect
|
|
3
3
|
import logging
|
|
4
4
|
from collections.abc import Iterable, Iterator, Sequence
|
|
5
|
-
from typing import Any, TypeVar
|
|
5
|
+
from typing import Any, TypeVar
|
|
6
6
|
|
|
7
7
|
import wrapt # type: ignore
|
|
8
8
|
from typing_extensions import Self
|
|
@@ -18,6 +18,8 @@ T = TypeVar("T")
|
|
|
18
18
|
|
|
19
19
|
|
|
20
20
|
class TestContainer(Container):
|
|
21
|
+
__test__ = False
|
|
22
|
+
|
|
21
23
|
def __init__(
|
|
22
24
|
self,
|
|
23
25
|
*,
|
|
@@ -65,18 +67,18 @@ class TestContainer(Container):
|
|
|
65
67
|
self._override_instances.pop(interface, None)
|
|
66
68
|
|
|
67
69
|
def _resolve_or_create(
|
|
68
|
-
self, interface:
|
|
69
|
-
) ->
|
|
70
|
+
self, interface: Any, create: bool, /, **defaults: Any
|
|
71
|
+
) -> Any:
|
|
70
72
|
"""Internal method to handle instance resolution and creation."""
|
|
71
73
|
instance = super()._resolve_or_create(interface, create, **defaults)
|
|
72
|
-
return
|
|
74
|
+
return self._patch_resolver(interface, instance)
|
|
73
75
|
|
|
74
76
|
async def _aresolve_or_create(
|
|
75
|
-
self, interface:
|
|
76
|
-
) ->
|
|
77
|
+
self, interface: Any, create: bool, /, **defaults: Any
|
|
78
|
+
) -> Any:
|
|
77
79
|
"""Internal method to handle instance resolution and creation asynchronously."""
|
|
78
80
|
instance = await super()._aresolve_or_create(interface, create, **defaults)
|
|
79
|
-
return
|
|
81
|
+
return self._patch_resolver(interface, instance)
|
|
80
82
|
|
|
81
83
|
def _get_provider_instance(
|
|
82
84
|
self,
|
|
@@ -87,12 +89,10 @@ class TestContainer(Container):
|
|
|
87
89
|
**defaults: Any,
|
|
88
90
|
) -> Any:
|
|
89
91
|
"""Retrieve an instance of a dependency from the scoped context."""
|
|
90
|
-
instance
|
|
92
|
+
instance = super()._get_provider_instance(
|
|
91
93
|
provider, parameter, context, **defaults
|
|
92
94
|
)
|
|
93
|
-
|
|
94
|
-
instance = InstanceProxy(instance, interface=parameter.annotation)
|
|
95
|
-
return instance, is_default
|
|
95
|
+
return InstanceProxy(instance, interface=parameter.annotation)
|
|
96
96
|
|
|
97
97
|
async def _aget_provider_instance(
|
|
98
98
|
self,
|
|
@@ -103,14 +103,12 @@ class TestContainer(Container):
|
|
|
103
103
|
**defaults: Any,
|
|
104
104
|
) -> Any:
|
|
105
105
|
"""Asynchronously retrieve an instance of a dependency from the context."""
|
|
106
|
-
instance
|
|
106
|
+
instance = await super()._aget_provider_instance(
|
|
107
107
|
provider, parameter, context, **defaults
|
|
108
108
|
)
|
|
109
|
-
|
|
110
|
-
instance = InstanceProxy(instance, interface=parameter.annotation)
|
|
111
|
-
return instance, is_default
|
|
109
|
+
return InstanceProxy(instance, interface=parameter.annotation)
|
|
112
110
|
|
|
113
|
-
def _patch_resolver(self, interface:
|
|
111
|
+
def _patch_resolver(self, interface: Any, instance: Any) -> Any:
|
|
114
112
|
"""Patch the test resolver for the instance."""
|
|
115
113
|
if interface in self._override_instances:
|
|
116
114
|
return self._override_instances[interface]
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
anydi/__init__.py,sha256=UH3N0LIw4ysoV0_EFgtDNioxEQxOmeytYbJnS8MXIXM,532
|
|
2
2
|
anydi/_async.py,sha256=KhRd3RmZFcwNDzrMm8ctA1gwrg-6m_7laECTYsZdF5k,1445
|
|
3
|
-
anydi/_container.py,sha256=
|
|
3
|
+
anydi/_container.py,sha256=pclsUVpXDPhacgdBl1_MZ9pV4AUc6a3jBVQOHLniW-4,31047
|
|
4
4
|
anydi/_context.py,sha256=_Xy8cTpRskb4cxTd-Fe-5NnIZyBe1DnovkofhdeUfmw,3020
|
|
5
5
|
anydi/_decorators.py,sha256=F3yBeGQSz1EsulZaEvYn3cd6FEjJRMoyA6u1QCbEwcs,2813
|
|
6
6
|
anydi/_module.py,sha256=QPvP27JndZkwl-FYUZWscJm6yfkNzjwoFGURyDhb6Pc,2582
|
|
@@ -9,7 +9,7 @@ anydi/_scan.py,sha256=WOdsPaoRZPYw4kBzoBXyWhkiwuxY8_UjHfa0c9QFyLc,3639
|
|
|
9
9
|
anydi/_scope.py,sha256=PFHjPb2-n0vhRo9mvD_craTFfoJBzR3y-N3_0apL5Q0,258
|
|
10
10
|
anydi/_typing.py,sha256=m7KnVenDE1E420IeYz2Ocw6dhddmSFbuBBgkOXtl9pA,3709
|
|
11
11
|
anydi/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
12
|
-
anydi/testing.py,sha256=
|
|
12
|
+
anydi/testing.py,sha256=ex9grqKpQmmJWwhIVnzq6aHaUAGLu2-7fwLyYTUuKHE,5678
|
|
13
13
|
anydi/ext/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
14
14
|
anydi/ext/_utils.py,sha256=QthoO0Nxv_nI-yQ9kGSi9jLLT5N71BxOgFNXwLQ5DQ0,2073
|
|
15
15
|
anydi/ext/fastapi.py,sha256=q7q1hF5v4dpV3YM4L1CVuD20bXC7f5S7e1apAfekkG0,2448
|
|
@@ -27,8 +27,8 @@ anydi/ext/django/ninja/_operation.py,sha256=wk5EOjLY3KVIHk9jMCGsFsja9-dQmMOpLpHX
|
|
|
27
27
|
anydi/ext/django/ninja/_signature.py,sha256=UVLmKpYvH1fNb9C7ffP50KCmh0BE6unHegfss5dvpVU,2261
|
|
28
28
|
anydi/ext/starlette/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
29
29
|
anydi/ext/starlette/middleware.py,sha256=MxnzshAs-CMvjJp0r457k52MzBL8O4KAuClnF6exBdU,803
|
|
30
|
-
anydi-0.
|
|
31
|
-
anydi-0.
|
|
32
|
-
anydi-0.
|
|
33
|
-
anydi-0.
|
|
34
|
-
anydi-0.
|
|
30
|
+
anydi-0.45.1.dist-info/METADATA,sha256=aup2n3aXxNUsWObdd6COZTXiPs8U_jXGiRvi9hB2CmM,4957
|
|
31
|
+
anydi-0.45.1.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
32
|
+
anydi-0.45.1.dist-info/entry_points.txt,sha256=Nklo9f3Oe4AkNsEgC4g43nCJ-23QDngZSVDNRMdaILI,43
|
|
33
|
+
anydi-0.45.1.dist-info/licenses/LICENSE,sha256=V6rU8a8fv6o2jQ-7ODHs0XfDFimot8Q6Km6CylRIDTo,1069
|
|
34
|
+
anydi-0.45.1.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|