python-injection 0.9.1__py3-none-any.whl → 0.9.2__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.

@@ -15,7 +15,7 @@ class Invertible[T](Protocol):
15
15
 
16
16
  @dataclass(repr=False, eq=False, frozen=True, slots=True)
17
17
  class SimpleInvertible[T](Invertible[T]):
18
- callable: Callable[[], T]
18
+ callable: Callable[..., T]
19
19
 
20
20
  def __invert__(self) -> T:
21
21
  return self.callable()
injection/common/lazy.py CHANGED
@@ -9,7 +9,7 @@ __all__ = ("Lazy", "LazyMapping")
9
9
  class Lazy[T](Invertible[T]):
10
10
  __slots__ = ("__cache", "__is_set")
11
11
 
12
- def __init__(self, factory: Callable[[], T]):
12
+ def __init__(self, factory: Callable[..., T]):
13
13
  self.__setup_cache(factory)
14
14
 
15
15
  def __invert__(self) -> T:
@@ -19,7 +19,7 @@ class Lazy[T](Invertible[T]):
19
19
  def is_set(self) -> bool:
20
20
  return self.__is_set
21
21
 
22
- def __setup_cache(self, factory: Callable[[], T]):
22
+ def __setup_cache(self, factory: Callable[..., T]):
23
23
  def cache_generator() -> Iterator[T]:
24
24
  nonlocal factory
25
25
  cached = factory()
injection/core/module.py CHANGED
@@ -17,7 +17,7 @@ from dataclasses import dataclass, field
17
17
  from enum import StrEnum
18
18
  from functools import partialmethod, singledispatchmethod, update_wrapper
19
19
  from inspect import Signature, isclass
20
- from queue import Queue
20
+ from queue import Empty, Queue
21
21
  from types import MethodType, UnionType
22
22
  from typing import (
23
23
  Any,
@@ -161,7 +161,7 @@ class Injectable[T](Protocol):
161
161
 
162
162
  @dataclass(repr=False, frozen=True, slots=True)
163
163
  class BaseInjectable[T](Injectable[T], ABC):
164
- factory: Callable[[], T]
164
+ factory: Callable[..., T]
165
165
 
166
166
 
167
167
  class NewInjectable[T](BaseInjectable[T]):
@@ -476,7 +476,7 @@ class Module(EventListener, Broker):
476
476
 
477
477
  function = InjectedFunction(wp)
478
478
 
479
- @function.on_setup(block=False)
479
+ @function.on_setup
480
480
  def listen():
481
481
  function.update(self)
482
482
  self.add_listener(function)
@@ -723,10 +723,8 @@ class InjectedFunction(EventListener):
723
723
  update_wrapper(self, wrapped, updated=())
724
724
  self.__dependencies = Dependencies.empty()
725
725
  self.__owner = None
726
-
727
- queue = Queue[Callable[[], Any]]()
728
- queue.put_nowait(self.__set_signature)
729
- self.__setup_queue = queue
726
+ self.__setup_queue = Queue[Callable[..., Any]](maxsize=2)
727
+ self.on_setup(self.__set_signature)
730
728
 
731
729
  def __repr__(self) -> str: # pragma: no cover
732
730
  return repr(self.wrapped)
@@ -735,13 +733,7 @@ class InjectedFunction(EventListener):
735
733
  return str(self.wrapped)
736
734
 
737
735
  def __call__(self, /, *args, **kwargs) -> Any:
738
- queue = self.__setup_queue
739
-
740
- while not queue.empty():
741
- setup = queue.get()
742
- setup()
743
- queue.task_done()
744
-
736
+ self.__setup()
745
737
  arguments = self.bind(args, kwargs)
746
738
  return self.wrapped(*arguments.args, **arguments.kwargs)
747
739
 
@@ -796,15 +788,15 @@ class InjectedFunction(EventListener):
796
788
  self.__dependencies = Dependencies.resolve(self.signature, module, self.__owner)
797
789
  return self
798
790
 
799
- def on_setup(self, wrapped: Callable[[], Any] = None, /, *, block: bool = True):
791
+ def on_setup(self, wrapped: Callable[..., Any] = None, /):
800
792
  def decorator(wp):
801
- self.__setup_queue.put(wp, block=block)
793
+ self.__setup_queue.put_nowait(wp)
802
794
  return wp
803
795
 
804
796
  return decorator(wrapped) if wrapped else decorator
805
797
 
806
798
  @singledispatchmethod
807
- def on_event(self, event: Event, /) -> None: # type: ignore
799
+ def on_event(self, event: Event, /) -> ContextManager | None: # type: ignore
808
800
  return None
809
801
 
810
802
  @on_event.register
@@ -813,6 +805,20 @@ class InjectedFunction(EventListener):
813
805
  yield
814
806
  self.update(event.on_module)
815
807
 
808
+ def __setup(self):
809
+ queue = self.__setup_queue
810
+
811
+ while True:
812
+ try:
813
+ task = queue.get_nowait()
814
+ except Empty:
815
+ break
816
+
817
+ task()
818
+ queue.task_done()
819
+
820
+ queue.join()
821
+
816
822
  def __set_signature(self) -> Self:
817
823
  self.__signature__ = inspect.signature(self.wrapped, eval_str=True)
818
824
  return self
@@ -33,6 +33,8 @@ def use_test_injectables(*, on: Module = None, test_module: Module = None):
33
33
  for module in (on, test_module):
34
34
  module.unlock()
35
35
 
36
+ del module
37
+
36
38
  with on.use_temporarily(test_module, priority=ModulePriority.HIGH):
37
39
  yield
38
40
  on.unlock()
injection/utils.py CHANGED
@@ -1,3 +1,4 @@
1
+ from collections.abc import Callable
1
2
  from importlib import import_module
2
3
  from pkgutil import walk_packages
3
4
  from types import ModuleType
@@ -5,9 +6,10 @@ from types import ModuleType
5
6
  __all__ = ("load_package",)
6
7
 
7
8
 
8
- def load_package(package: ModuleType | str):
9
+ def load_package(package: ModuleType | str, predicate: Callable[[str], bool] = None):
9
10
  """
10
11
  Function for importing all modules in a Python package.
12
+ Pass the `predicate` parameter if you want to filter the modules to be imported.
11
13
  """
12
14
 
13
15
  if isinstance(package, str):
@@ -20,8 +22,15 @@ def load_package(package: ModuleType | str):
20
22
  "Package has no `__path__` attribute, as it's probably a module."
21
23
  ) from exc
22
24
 
25
+ if predicate is None:
26
+
27
+ def predicate(_: str) -> bool:
28
+ return True
29
+
23
30
  for info in walk_packages(path=path, prefix=f"{package.__name__}."):
24
- if info.ispkg:
31
+ name = info.name
32
+
33
+ if info.ispkg or not predicate(name):
25
34
  continue
26
35
 
27
- import_module(info.name)
36
+ import_module(name)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: python-injection
3
- Version: 0.9.1
3
+ Version: 0.9.2
4
4
  Summary: Fast and easy dependency injection framework.
5
5
  Home-page: https://github.com/100nm/python-injection
6
6
  License: MIT
@@ -2,20 +2,20 @@ injection/__init__.py,sha256=LhfwYFMBw6tJ7XA_C353w61OPt4IuQQgs3zIjrwMIz8,654
2
2
  injection/__init__.pyi,sha256=dfIQAR8L8Yr7dM-DODR4czwUd2a7zzFQHzb8mr1Q6P0,6235
3
3
  injection/common/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
4
  injection/common/event.py,sha256=5Rdb2m3vAMCic8cQAVkStJDbrDrW_lk6kav8wYwmexM,1283
5
- injection/common/invertible.py,sha256=noNcmJ96IQi0XJms0dyfrx_AvKZnQM0sZyxhc2l6qo0,527
6
- injection/common/lazy.py,sha256=FEas6ewwOGWvRR8cflmyVvSLZd_-Fxd0QeIdvAM5I9c,1313
5
+ injection/common/invertible.py,sha256=a-fht4TxMnki-oFFaZrDz52X_LWx0NU60KQlw72pzs4,528
6
+ injection/common/lazy.py,sha256=SpQhlGBpMpeD9R5R-CdIInSVDv1XZWvnkERp1J6wsus,1315
7
7
  injection/common/tools/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
8
8
  injection/common/tools/threading.py,sha256=HlvP6k_-eZaK8JbB2b9PP171IZVe_0W2oMYsw3ebdKA,187
9
9
  injection/common/tools/type.py,sha256=ThM3Z1_gHmsdT4Jp7PlZgJY0lsufc4InPO65485Ugsg,1739
10
10
  injection/core/__init__.py,sha256=zuf0ubI2dHnbjn1059eduhS-ACIkkROa6-dhp10krh0,22
11
- injection/core/module.py,sha256=3FqU4XQgTNLI-VwpX1bKcRsLOaPeKSQ0sQuVsL_27I4,21625
11
+ injection/core/module.py,sha256=5Hqdll6gxiaHQCZl9OVWKojmzj36azzOPvIgi-6PGdg,21723
12
12
  injection/exceptions.py,sha256=RsWWiWwKSMU0vxXQqQSn6CKHLMrGu4SSzYUAy9OJRXk,626
13
13
  injection/integrations/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
14
14
  injection/integrations/blacksheep.py,sha256=82P_owhF3FKIJxxalnSic3AJnoOr1mkojkWMefpO6QI,731
15
15
  injection/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
16
- injection/testing/__init__.py,sha256=xglKmIJgg2j1wt8qWJ_TtGfFyKmn7xE7b8kQhD2yEkQ,864
16
+ injection/testing/__init__.py,sha256=cr4e3VnoKbWzkc6uyC03sffT59deKgLN-SKiZjhzooI,880
17
17
  injection/testing/__init__.pyi,sha256=_u95cJVHBkt69HUvnu2bbfYWmi7GOH_1qyFyvC1PxBk,518
18
- injection/utils.py,sha256=_79aiciimZpuoUTz5lojKySUMMzpkU-e7SotiHIFTI8,676
19
- python_injection-0.9.1.dist-info/METADATA,sha256=6vDRDhsWCG-FbwiHs-pvslfia8Fpe2n7hWJPoxJJbhg,3572
20
- python_injection-0.9.1.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
21
- python_injection-0.9.1.dist-info/RECORD,,
18
+ injection/utils.py,sha256=a_H6RC6cNWbG1NQ-zvtrlgwnfS5Jfu0X6VJwIUIHlSU,974
19
+ python_injection-0.9.2.dist-info/METADATA,sha256=UkMpmL3GAAQ5nqRyf-3-j-_hB8pJrXLW7D3E9JSEsE0,3572
20
+ python_injection-0.9.2.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
21
+ python_injection-0.9.2.dist-info/RECORD,,