python-injection 0.10.8.post0__tar.gz → 0.10.9__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.9}/PKG-INFO +2 -1
  2. {python_injection-0.10.8.post0 → python_injection-0.10.9}/injection/__init__.pyi +8 -3
  3. {python_injection-0.10.8.post0 → python_injection-0.10.9}/injection/_core/hook.py +1 -1
  4. {python_injection-0.10.8.post0 → python_injection-0.10.9}/injection/_core/module.py +11 -6
  5. {python_injection-0.10.8.post0 → python_injection-0.10.9}/injection/integrations/fastapi.py +6 -4
  6. {python_injection-0.10.8.post0 → python_injection-0.10.9}/pyproject.toml +4 -4
  7. {python_injection-0.10.8.post0 → python_injection-0.10.9}/README.md +0 -0
  8. {python_injection-0.10.8.post0 → python_injection-0.10.9}/injection/__init__.py +0 -0
  9. {python_injection-0.10.8.post0 → python_injection-0.10.9}/injection/_core/__init__.py +0 -0
  10. {python_injection-0.10.8.post0 → python_injection-0.10.9}/injection/_core/common/__init__.py +0 -0
  11. {python_injection-0.10.8.post0 → python_injection-0.10.9}/injection/_core/common/event.py +0 -0
  12. {python_injection-0.10.8.post0 → python_injection-0.10.9}/injection/_core/common/invertible.py +0 -0
  13. {python_injection-0.10.8.post0 → python_injection-0.10.9}/injection/_core/common/lazy.py +0 -0
  14. {python_injection-0.10.8.post0 → python_injection-0.10.9}/injection/_core/common/threading.py +0 -0
  15. {python_injection-0.10.8.post0 → python_injection-0.10.9}/injection/_core/common/type.py +0 -0
  16. {python_injection-0.10.8.post0 → python_injection-0.10.9}/injection/exceptions.py +0 -0
  17. {python_injection-0.10.8.post0 → python_injection-0.10.9}/injection/integrations/__init__.py +0 -0
  18. {python_injection-0.10.8.post0 → python_injection-0.10.9}/injection/integrations/blacksheep.py +0 -0
  19. {python_injection-0.10.8.post0 → python_injection-0.10.9}/injection/py.typed +0 -0
  20. {python_injection-0.10.8.post0 → python_injection-0.10.9}/injection/testing/__init__.py +0 -0
  21. {python_injection-0.10.8.post0 → python_injection-0.10.9}/injection/testing/__init__.pyi +0 -0
  22. {python_injection-0.10.8.post0 → python_injection-0.10.9}/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.9
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, TypeAliasType
4
4
 
5
5
  from injection import Module, mod
6
6
  from injection.exceptions import InjectionError
@@ -33,8 +33,10 @@ class InjectionDependency[T]:
33
33
  __call__: Callable[[], T]
34
34
  __class: type[T] | TypeAliasType | GenericAlias
35
35
 
36
+ __sentinel: ClassVar[object] = object()
37
+
36
38
  def __init__(self, cls: type[T] | TypeAliasType | GenericAlias, module: Module):
37
- lazy_instance = module.get_lazy_instance(cls)
39
+ lazy_instance = module.get_lazy_instance(cls, default=self.__sentinel)
38
40
  self.__call__ = lambda: self.__ensure(~lazy_instance)
39
41
  self.__class = cls
40
42
 
@@ -47,8 +49,8 @@ class InjectionDependency[T]:
47
49
  def __hash__(self) -> int:
48
50
  return hash((self.__class,))
49
51
 
50
- def __ensure(self, instance: T | None) -> T:
51
- if instance is None:
52
+ def __ensure(self, instance: T | Any) -> T:
53
+ if instance is self.__sentinel:
52
54
  raise InjectionError(f"`{self.__class}` is an unknown dependency.")
53
55
 
54
56
  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.9"
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 = "*"
@@ -64,12 +64,12 @@ check_untyped_defs = true
64
64
  disallow_any_generics = true
65
65
  disallow_subclassing_any = true
66
66
  disallow_untyped_defs = true
67
- enable_incomplete_feature = ["NewGenericSyntax"]
68
67
  exclude = [
69
68
  "documentation/example/",
70
69
  "tests/",
71
70
  "bench.py",
72
71
  "conftest.py",
72
+ "injection/integrations/blacksheep.py",
73
73
  ]
74
74
  follow_imports = "silent"
75
75
  no_implicit_reexport = true
@@ -84,7 +84,7 @@ warn_required_dynamic_aliases = true
84
84
 
85
85
  [tool.pytest.ini_options]
86
86
  python_files = "test_*.py"
87
- addopts = "--tb short --cov ./ --cov-report term-missing:skip-covered"
87
+ addopts = "--tb short --cov ./ --cov-report term-missing:skip-covered --ignore tests/integrations/test_blacksheep.py"
88
88
  asyncio_default_fixture_loop_scope = "session"
89
89
  asyncio_mode = "auto"
90
90
  testpaths = "**/tests/"