python-injection 0.10.8.post0__tar.gz → 0.10.10__tar.gz

Sign up to get free protection for your applications and to get access to all the features.
Files changed (22) hide show
  1. {python_injection-0.10.8.post0 → python_injection-0.10.10}/PKG-INFO +2 -1
  2. {python_injection-0.10.8.post0 → python_injection-0.10.10}/injection/__init__.pyi +8 -3
  3. {python_injection-0.10.8.post0 → python_injection-0.10.10}/injection/_core/hook.py +1 -1
  4. {python_injection-0.10.8.post0 → python_injection-0.10.10}/injection/_core/module.py +11 -6
  5. {python_injection-0.10.8.post0 → python_injection-0.10.10}/injection/integrations/fastapi.py +17 -7
  6. {python_injection-0.10.8.post0 → python_injection-0.10.10}/pyproject.toml +5 -4
  7. {python_injection-0.10.8.post0 → python_injection-0.10.10}/README.md +0 -0
  8. {python_injection-0.10.8.post0 → python_injection-0.10.10}/injection/__init__.py +0 -0
  9. {python_injection-0.10.8.post0 → python_injection-0.10.10}/injection/_core/__init__.py +0 -0
  10. {python_injection-0.10.8.post0 → python_injection-0.10.10}/injection/_core/common/__init__.py +0 -0
  11. {python_injection-0.10.8.post0 → python_injection-0.10.10}/injection/_core/common/event.py +0 -0
  12. {python_injection-0.10.8.post0 → python_injection-0.10.10}/injection/_core/common/invertible.py +0 -0
  13. {python_injection-0.10.8.post0 → python_injection-0.10.10}/injection/_core/common/lazy.py +0 -0
  14. {python_injection-0.10.8.post0 → python_injection-0.10.10}/injection/_core/common/threading.py +0 -0
  15. {python_injection-0.10.8.post0 → python_injection-0.10.10}/injection/_core/common/type.py +0 -0
  16. {python_injection-0.10.8.post0 → python_injection-0.10.10}/injection/exceptions.py +0 -0
  17. {python_injection-0.10.8.post0 → python_injection-0.10.10}/injection/integrations/__init__.py +0 -0
  18. {python_injection-0.10.8.post0 → python_injection-0.10.10}/injection/integrations/blacksheep.py +0 -0
  19. {python_injection-0.10.8.post0 → python_injection-0.10.10}/injection/py.typed +0 -0
  20. {python_injection-0.10.8.post0 → python_injection-0.10.10}/injection/testing/__init__.py +0 -0
  21. {python_injection-0.10.8.post0 → python_injection-0.10.10}/injection/testing/__init__.pyi +0 -0
  22. {python_injection-0.10.8.post0 → python_injection-0.10.10}/injection/utils.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: python-injection
3
- Version: 0.10.8.post0
3
+ Version: 0.10.10
4
4
  Summary: Fast and easy dependency injection framework.
5
5
  Home-page: https://github.com/100nm/python-injection
6
6
  License: MIT
@@ -15,6 +15,7 @@ Classifier: Operating System :: OS Independent
15
15
  Classifier: Programming Language :: Python
16
16
  Classifier: Programming Language :: Python :: 3
17
17
  Classifier: Programming Language :: Python :: 3.12
18
+ Classifier: Programming Language :: Python :: 3.13
18
19
  Classifier: Programming Language :: Python :: 3 :: Only
19
20
  Classifier: Topic :: Software Development :: Libraries
20
21
  Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
@@ -136,18 +136,23 @@ class Module:
136
136
  parameter or an exception will be raised.
137
137
  """
138
138
 
139
- def get_instance[T](self, cls: _InputType[T]) -> T | None:
139
+ def get_instance[T, Default](
140
+ self,
141
+ cls: _InputType[T],
142
+ default: Default | None = ...,
143
+ ) -> T | Default | None:
140
144
  """
141
145
  Function used to retrieve an instance associated with the type passed in
142
146
  parameter or return `None`.
143
147
  """
144
148
 
145
- def get_lazy_instance[T](
149
+ def get_lazy_instance[T, Default](
146
150
  self,
147
151
  cls: _InputType[T],
152
+ default: Default | None = ...,
148
153
  *,
149
154
  cache: bool = ...,
150
- ) -> _Invertible[T | None]:
155
+ ) -> _Invertible[T | Default | None]:
151
156
  """
152
157
  Function used to retrieve an instance associated with the type passed in
153
158
  parameter or `None`. Return a `Invertible` object. To access the instance
@@ -34,7 +34,7 @@ class Hook[**P, T]:
34
34
  return iter(self.__functions)
35
35
 
36
36
  def add(self, *functions: HookFunction[P, T]) -> Self:
37
- self.__functions.extend(functions)
37
+ self.__functions.extend(reversed(functions))
38
38
  return self
39
39
 
40
40
  @classmethod
@@ -572,22 +572,27 @@ class Module(Broker, EventListener):
572
572
  injectable = self[cls]
573
573
  return injectable.get_instance()
574
574
 
575
- def get_instance[T](self, cls: InputType[T]) -> T | None:
575
+ def get_instance[T, Default](
576
+ self,
577
+ cls: InputType[T],
578
+ default: Default | None = None,
579
+ ) -> T | Default | None:
576
580
  try:
577
581
  return self.find_instance(cls)
578
582
  except KeyError:
579
- return None
583
+ return default
580
584
 
581
- def get_lazy_instance[T](
585
+ def get_lazy_instance[T, Default](
582
586
  self,
583
587
  cls: InputType[T],
588
+ default: Default | None = None,
584
589
  *,
585
590
  cache: bool = False,
586
- ) -> Invertible[T | None]:
591
+ ) -> Invertible[T | Default | None]:
587
592
  if cache:
588
- return Lazy(lambda: self.get_instance(cls))
593
+ return Lazy(lambda: self.get_instance(cls, default))
589
594
 
590
- function = self.inject(lambda instance=None: instance)
595
+ function = self.inject(lambda instance=default: instance)
591
596
  function.__injected__.set_owner(cls)
592
597
  return SimpleInvertible(function)
593
598
 
@@ -1,6 +1,6 @@
1
1
  from collections.abc import Callable
2
2
  from types import GenericAlias
3
- from typing import Any, TypeAliasType
3
+ from typing import Any, ClassVar, Self, TypeAliasType
4
4
 
5
5
  from injection import Module, mod
6
6
  from injection.exceptions import InjectionError
@@ -28,27 +28,37 @@ def Inject[T]( # noqa: N802
28
28
 
29
29
 
30
30
  class InjectionDependency[T]:
31
- __slots__ = ("__call__", "__class")
31
+ __slots__ = ("__call__", "__class", "__module")
32
32
 
33
33
  __call__: Callable[[], T]
34
34
  __class: type[T] | TypeAliasType | GenericAlias
35
+ __module: Module
36
+
37
+ __sentinel: ClassVar[object] = object()
35
38
 
36
39
  def __init__(self, cls: type[T] | TypeAliasType | GenericAlias, module: Module):
37
- lazy_instance = module.get_lazy_instance(cls)
40
+ lazy_instance = module.get_lazy_instance(cls, default=self.__sentinel)
38
41
  self.__call__ = lambda: self.__ensure(~lazy_instance)
39
42
  self.__class = cls
43
+ self.__module = module
40
44
 
41
45
  def __eq__(self, other: Any) -> bool:
42
46
  if isinstance(other, type(self)):
43
- return hash(self) == hash(other)
47
+ return self.__key == other.__key
44
48
 
45
49
  return NotImplemented
46
50
 
47
51
  def __hash__(self) -> int:
48
- return hash((self.__class,))
52
+ return hash(self.__key)
53
+
54
+ @property
55
+ def __key(
56
+ self,
57
+ ) -> tuple[type[Self], type[T] | TypeAliasType | GenericAlias, Module]:
58
+ return type(self), self.__class, self.__module
49
59
 
50
- def __ensure(self, instance: T | None) -> T:
51
- if instance is None:
60
+ def __ensure(self, instance: T | Any) -> T:
61
+ if instance is self.__sentinel:
52
62
  raise InjectionError(f"`{self.__class}` is an unknown dependency.")
53
63
 
54
64
  return instance
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "python-injection"
3
- version = "0.10.8.post0"
3
+ version = "0.10.10"
4
4
  description = "Fast and easy dependency injection framework."
5
5
  license = "MIT"
6
6
  authors = ["remimd"]
@@ -31,7 +31,7 @@ mypy = "*"
31
31
  ruff = "*"
32
32
 
33
33
  [tool.poetry.group.test.dependencies]
34
- blacksheep = "*"
34
+ blacksheep = { version = "*", optional = true }
35
35
  fastapi = "*"
36
36
  httpx = "*"
37
37
  pydantic = "*"
@@ -54,6 +54,7 @@ exclude_lines = [
54
54
  "pass",
55
55
  "pragma: no cover",
56
56
  "raise NotImplementedError",
57
+ "return NotImplemented",
57
58
  ]
58
59
 
59
60
  [tool.coverage.run]
@@ -64,12 +65,12 @@ check_untyped_defs = true
64
65
  disallow_any_generics = true
65
66
  disallow_subclassing_any = true
66
67
  disallow_untyped_defs = true
67
- enable_incomplete_feature = ["NewGenericSyntax"]
68
68
  exclude = [
69
69
  "documentation/example/",
70
70
  "tests/",
71
71
  "bench.py",
72
72
  "conftest.py",
73
+ "injection/integrations/blacksheep.py",
73
74
  ]
74
75
  follow_imports = "silent"
75
76
  no_implicit_reexport = true
@@ -84,7 +85,7 @@ warn_required_dynamic_aliases = true
84
85
 
85
86
  [tool.pytest.ini_options]
86
87
  python_files = "test_*.py"
87
- addopts = "--tb short --cov ./ --cov-report term-missing:skip-covered"
88
+ addopts = "--tb short --cov ./ --cov-report term-missing:skip-covered --ignore tests/integrations/test_blacksheep.py"
88
89
  asyncio_default_fixture_loop_scope = "session"
89
90
  asyncio_mode = "auto"
90
91
  testpaths = "**/tests/"