python-injection 0.2.1__tar.gz → 0.2.3__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.
Potentially problematic release.
This version of python-injection might be problematic. Click here for more details.
- {python_injection-0.2.1 → python_injection-0.2.3}/PKG-INFO +1 -1
- {python_injection-0.2.1 → python_injection-0.2.3}/injection/core.py +25 -15
- {python_injection-0.2.1 → python_injection-0.2.3}/pyproject.toml +1 -1
- {python_injection-0.2.1 → python_injection-0.2.3}/README.md +0 -0
- {python_injection-0.2.1 → python_injection-0.2.3}/injection/__init__.py +0 -0
- {python_injection-0.2.1 → python_injection-0.2.3}/injection/core.pyi +0 -0
- {python_injection-0.2.1 → python_injection-0.2.3}/injection/exceptions.py +0 -0
- {python_injection-0.2.1 → python_injection-0.2.3}/injection/utils.py +0 -0
- {python_injection-0.2.1 → python_injection-0.2.3}/injection/utils.pyi +0 -0
|
@@ -3,7 +3,7 @@ from abc import ABC, abstractmethod
|
|
|
3
3
|
from dataclasses import dataclass, field
|
|
4
4
|
from functools import cached_property, wraps
|
|
5
5
|
from inspect import Parameter
|
|
6
|
-
from typing import Callable, Generic, TypeVar
|
|
6
|
+
from typing import Callable, Generic, Iterable, TypeVar
|
|
7
7
|
|
|
8
8
|
from injection.exceptions import NoInjectable
|
|
9
9
|
|
|
@@ -40,21 +40,28 @@ class UniqueInjectable(Injectable[T]):
|
|
|
40
40
|
class InjectionManager:
|
|
41
41
|
__container: dict[type, Injectable] = field(default_factory=dict, init=False)
|
|
42
42
|
|
|
43
|
-
def
|
|
43
|
+
def get(self, reference: type) -> Injectable:
|
|
44
44
|
try:
|
|
45
45
|
return self.__container[reference]
|
|
46
46
|
except KeyError as exc:
|
|
47
47
|
raise NoInjectable(f"No injectable for {reference.__name__}.") from exc
|
|
48
48
|
|
|
49
|
-
def
|
|
49
|
+
def set_multiple(self, references: Iterable[type], injectable: Injectable):
|
|
50
|
+
def reference_parser():
|
|
51
|
+
for reference in references:
|
|
52
|
+
self.check_if_exists(reference)
|
|
53
|
+
yield reference, injectable
|
|
54
|
+
|
|
55
|
+
new_values = reference_parser()
|
|
56
|
+
self.__container.update(new_values)
|
|
57
|
+
|
|
58
|
+
def check_if_exists(self, reference: type):
|
|
50
59
|
if reference in self.__container:
|
|
51
60
|
raise RuntimeError(
|
|
52
61
|
f"An injectable already exists for the "
|
|
53
62
|
f"reference class `{reference.__name__}`."
|
|
54
63
|
)
|
|
55
64
|
|
|
56
|
-
self.__container[reference] = injectable
|
|
57
|
-
|
|
58
65
|
|
|
59
66
|
_manager = InjectionManager()
|
|
60
67
|
|
|
@@ -70,16 +77,19 @@ class Decorator:
|
|
|
70
77
|
|
|
71
78
|
def __call__(self, wp=None, /, **kwargs):
|
|
72
79
|
def decorator(wrapped):
|
|
73
|
-
|
|
80
|
+
def iter_references():
|
|
81
|
+
if isinstance(wrapped, type):
|
|
82
|
+
yield wrapped
|
|
74
83
|
|
|
75
|
-
|
|
76
|
-
|
|
84
|
+
if reference := kwargs.pop("reference", None):
|
|
85
|
+
yield reference
|
|
77
86
|
|
|
78
|
-
|
|
79
|
-
|
|
87
|
+
for reference in kwargs.pop("references", ()):
|
|
88
|
+
yield reference
|
|
80
89
|
|
|
81
|
-
|
|
82
|
-
|
|
90
|
+
references = iter_references()
|
|
91
|
+
injectable = self.__injectable_class(wrapped)
|
|
92
|
+
_manager.set_multiple(references, injectable)
|
|
83
93
|
|
|
84
94
|
return wrapped
|
|
85
95
|
|
|
@@ -93,7 +103,7 @@ del Decorator
|
|
|
93
103
|
|
|
94
104
|
|
|
95
105
|
def get_instance(reference: type[T]) -> T:
|
|
96
|
-
return _manager
|
|
106
|
+
return _manager.get(reference).get_instance()
|
|
97
107
|
|
|
98
108
|
|
|
99
109
|
def inject(fn=None):
|
|
@@ -107,7 +117,7 @@ def inject(fn=None):
|
|
|
107
117
|
|
|
108
118
|
for name, parameter in signature.parameters.items():
|
|
109
119
|
try:
|
|
110
|
-
value = arguments
|
|
120
|
+
value = arguments.pop(name)
|
|
111
121
|
except KeyError:
|
|
112
122
|
try:
|
|
113
123
|
value = get_instance(parameter.annotation)
|
|
@@ -120,7 +130,7 @@ def inject(fn=None):
|
|
|
120
130
|
case Parameter.VAR_POSITIONAL:
|
|
121
131
|
args.extend(value)
|
|
122
132
|
case Parameter.VAR_KEYWORD:
|
|
123
|
-
kwargs
|
|
133
|
+
kwargs.update(value)
|
|
124
134
|
case _:
|
|
125
135
|
kwargs[name] = value
|
|
126
136
|
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|