python-injection 0.8.1__py3-none-any.whl → 0.8.1.post0__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.

Potentially problematic release.


This version of python-injection might be problematic. Click here for more details.

injection/core/module.py CHANGED
@@ -16,14 +16,9 @@ from collections.abc import (
16
16
  from contextlib import ContextDecorator, contextmanager, suppress
17
17
  from dataclasses import dataclass, field
18
18
  from enum import Enum, auto
19
- from functools import (
20
- partialmethod,
21
- singledispatchmethod,
22
- update_wrapper,
23
- wraps,
24
- )
19
+ from functools import partialmethod, singledispatchmethod, update_wrapper
25
20
  from inspect import Signature, isclass
26
- from types import UnionType
21
+ from types import MethodType, UnionType
27
22
  from typing import (
28
23
  Any,
29
24
  ClassVar,
@@ -635,48 +630,37 @@ class InjectedFunction(EventListener):
635
630
  __slots__ = (
636
631
  "__dict__",
637
632
  "__signature__",
633
+ "__wrapped__",
638
634
  "__dependencies",
639
635
  "__owner",
640
636
  "__setup_queue",
641
- "__wrapper",
642
637
  )
643
638
 
644
639
  def __init__(self, wrapped: Callable[..., Any], /):
645
640
  update_wrapper(self, wrapped)
646
-
647
- @wraps(wrapped)
648
- def wrapper(*args, **kwargs):
649
- self.__consume_setup_queue()
650
- args, kwargs = self.bind(args, kwargs)
651
- return wrapped(*args, **kwargs)
652
-
653
- self.__wrapper = wrapper
654
641
  self.__dependencies = Dependencies.empty()
655
642
  self.__owner = None
656
643
  self.__setup_queue = LimitedQueue[Callable[[], Any]]()
657
- self.on_setup(
658
- lambda: self.__set_signature(
659
- inspect.signature(
660
- wrapped,
661
- eval_str=True,
662
- )
663
- )
664
- )
644
+ self.on_setup(self.__set_signature)
665
645
 
666
646
  def __repr__(self) -> str:
667
- return repr(self.__wrapper)
647
+ return repr(self.wrapped)
668
648
 
669
649
  def __str__(self) -> str:
670
- return str(self.__wrapper)
650
+ return str(self.wrapped)
671
651
 
672
652
  def __call__(self, /, *args, **kwargs) -> Any:
673
- return self.__wrapper(*args, **kwargs)
653
+ for function in self.__setup_queue:
654
+ function()
655
+
656
+ args, kwargs = self.bind(args, kwargs)
657
+ return self.wrapped(*args, **kwargs)
674
658
 
675
659
  def __get__(self, instance: object = None, owner: type = None):
676
660
  if instance is None:
677
661
  return self
678
662
 
679
- return self.__wrapper.__get__(instance, owner)
663
+ return MethodType(self, instance)
680
664
 
681
665
  def __set_name__(self, owner: type, name: str):
682
666
  self.set_owner(owner)
@@ -685,6 +669,10 @@ class InjectedFunction(EventListener):
685
669
  def signature(self) -> Signature:
686
670
  return self.__signature__
687
671
 
672
+ @property
673
+ def wrapped(self) -> Callable[..., Any]:
674
+ return self.__wrapped__
675
+
688
676
  def bind(
689
677
  self,
690
678
  args: Iterable[Any] = (),
@@ -736,12 +724,6 @@ class InjectedFunction(EventListener):
736
724
  yield
737
725
  self.update(event.on_module)
738
726
 
739
- def __consume_setup_queue(self):
740
- for function in self.__setup_queue:
741
- function()
742
-
743
- return self
744
-
745
- def __set_signature(self, signature: Signature):
746
- self.__signature__ = signature
727
+ def __set_signature(self):
728
+ self.__signature__ = inspect.signature(self.wrapped, eval_str=True)
747
729
  return self
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: python-injection
3
- Version: 0.8.1
3
+ Version: 0.8.1.post0
4
4
  Summary: Fast and easy dependency injection framework.
5
5
  Home-page: https://github.com/100nm/python-injection
6
6
  License: MIT
@@ -27,7 +27,7 @@ If you wish to inject a singleton, use `singleton` decorator.
27
27
  from injection import singleton
28
28
 
29
29
  @singleton
30
- class Singleton:
30
+ class ServiceA:
31
31
  """ class implementation """
32
32
  ```
33
33
 
@@ -37,7 +37,7 @@ If you wish to inject a new instance each time, use `injectable` decorator.
37
37
  from injection import injectable
38
38
 
39
39
  @injectable
40
- class Injectable:
40
+ class ServiceB:
41
41
  """ class implementation """
42
42
  ```
43
43
 
@@ -47,7 +47,10 @@ function.
47
47
  ```python
48
48
  from injection import set_constant
49
49
 
50
- app = set_constant(Application())
50
+ class ServiceC:
51
+ """ class implementation """
52
+
53
+ service_c = set_constant(ServiceC())
51
54
  ```
52
55
 
53
56
  ## Inject an instance
@@ -59,7 +62,7 @@ _Don't forget to annotate type of parameter to inject._
59
62
  from injection import inject
60
63
 
61
64
  @inject
62
- def my_function(instance: Injectable):
65
+ def some_function(service_a: ServiceA):
63
66
  """ function implementation """
64
67
  ```
65
68
 
@@ -76,8 +79,8 @@ from injection import inject
76
79
 
77
80
  @inject
78
81
  @dataclass
79
- class DataClass:
80
- instance: Injectable = ...
82
+ class SomeDataClass:
83
+ service_a: ServiceA = ...
81
84
  ```
82
85
 
83
86
  ## Get an instance
@@ -87,7 +90,7 @@ _Example with `get_instance` function:_
87
90
  ```python
88
91
  from injection import get_instance
89
92
 
90
- instance = get_instance(Injectable)
93
+ service_a = get_instance(ServiceA)
91
94
  ```
92
95
 
93
96
  _Example with `get_lazy_instance` function:_
@@ -95,9 +98,9 @@ _Example with `get_lazy_instance` function:_
95
98
  ```python
96
99
  from injection import get_lazy_instance
97
100
 
98
- lazy_instance = get_lazy_instance(Injectable)
101
+ lazy_service_a = get_lazy_instance(ServiceA)
99
102
  # ...
100
- instance = ~lazy_instance
103
+ service_a = ~lazy_service_a
101
104
  ```
102
105
 
103
106
  ## Inheritance
@@ -111,43 +114,39 @@ classes.
111
114
  _Example with one class:_
112
115
 
113
116
  ```python
114
- from injection import singleton
115
-
116
- class A:
117
+ class AbstractService(ABC):
117
118
  ...
118
119
 
119
- @singleton(on=A)
120
- class B(A):
120
+ @injectable(on=AbstractService)
121
+ class ConcreteService(AbstractService):
121
122
  ...
122
123
  ```
123
124
 
124
125
  _Example with several classes:_
125
126
 
126
127
  ```python
127
- from injection import singleton
128
-
129
- class A:
128
+ class AbstractService(ABC):
130
129
  ...
131
130
 
132
- class B(A):
131
+ class ConcreteService(AbstractService):
133
132
  ...
134
133
 
135
- @singleton(on=(A, B))
136
- class C(B):
134
+ @injectable(on=(AbstractService, ConcreteService))
135
+ class ConcreteServiceOverload(ConcreteService):
137
136
  ...
138
137
  ```
139
138
 
140
139
  If a class is registered in a package and you want to override it, there is the `override` parameter:
141
140
 
142
141
  ```python
143
- @singleton
144
- class A:
142
+ @injectable
143
+ class InaccessibleService:
145
144
  ...
146
145
 
147
146
  # ...
148
147
 
149
- @singleton(on=A, override=True)
150
- class B(A):
148
+ @injectable(on=InaccessibleService, override=True)
149
+ class ServiceOverload(InaccessibleService):
151
150
  ...
152
151
  ```
153
152
 
@@ -157,10 +156,10 @@ A recipe is a function that tells the injector how to construct the instance to
157
156
  the return type annotation when defining the recipe.
158
157
 
159
158
  ```python
160
- from injection import singleton
159
+ from injection import injectable
161
160
 
162
- @singleton
163
- def my_recipe() -> Singleton:
161
+ @injectable
162
+ def service_d_recipe() -> ServiceD:
164
163
  """ recipe implementation """
165
164
  ```
166
165
 
@@ -10,11 +10,11 @@ injection/common/tools/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3
10
10
  injection/common/tools/threading.py,sha256=02mBkIir2kQaXt47ewMJnOvhCldXCmPI2V-BW_l_6V8,271
11
11
  injection/common/tools/type.py,sha256=-zL0dtoVZme71Mscvav7iEWxY2-JltzNTekbWOCPSFo,1276
12
12
  injection/core/__init__.py,sha256=zuf0ubI2dHnbjn1059eduhS-ACIkkROa6-dhp10krh0,22
13
- injection/core/module.py,sha256=hYnNwFNzw-1TaTjPAtmCpKgghF1JTdbkrJ9jO07KQps,19284
13
+ injection/core/module.py,sha256=YR-_XwYvL7InODB9NB9_d6oLG0hFL-AFdu-GwDe1t7w,18952
14
14
  injection/exceptions.py,sha256=nE56jW00ZB1T-Z-dvfPczPShs3CwIc7tIvdYlOXlaXA,653
15
15
  injection/integrations/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
16
16
  injection/integrations/blacksheep.py,sha256=vcLil1IccS7JtXpuVu7s2LqN5Zravfe_7xpAt5cTIU0,723
17
17
  injection/utils.py,sha256=_79aiciimZpuoUTz5lojKySUMMzpkU-e7SotiHIFTI8,676
18
- python_injection-0.8.1.dist-info/METADATA,sha256=Atf1DZFIwYCdMKnK7_xQf_muy_Ma7qQmEAnGYQr5P-M,3433
19
- python_injection-0.8.1.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
20
- python_injection-0.8.1.dist-info/RECORD,,
18
+ python_injection-0.8.1.post0.dist-info/METADATA,sha256=7oD5fbqklsTYrYRJX_fSE42rkFU80PCNgCu70MnpPmU,3679
19
+ python_injection-0.8.1.post0.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
20
+ python_injection-0.8.1.post0.dist-info/RECORD,,