python-injection 0.9.5__tar.gz → 0.9.6__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.

Files changed (21) hide show
  1. {python_injection-0.9.5 → python_injection-0.9.6}/PKG-INFO +1 -1
  2. {python_injection-0.9.5 → python_injection-0.9.6}/injection/__init__.py +3 -3
  3. {python_injection-0.9.5 → python_injection-0.9.6}/injection/__init__.pyi +13 -13
  4. {python_injection-0.9.5/injection → python_injection-0.9.6/injection/_core}/common/lazy.py +1 -1
  5. {python_injection-0.9.5/injection/common/tools → python_injection-0.9.6/injection/_core/common}/threading.py +2 -1
  6. {python_injection-0.9.5/injection/core → python_injection-0.9.6/injection/_core}/module.py +27 -28
  7. {python_injection-0.9.5 → python_injection-0.9.6}/injection/utils.py +8 -8
  8. {python_injection-0.9.5 → python_injection-0.9.6}/pyproject.toml +1 -1
  9. python_injection-0.9.5/injection/integrations/__init__.py +0 -0
  10. {python_injection-0.9.5 → python_injection-0.9.6}/documentation/basic-usage.md +0 -0
  11. {python_injection-0.9.5/injection/core → python_injection-0.9.6/injection/_core}/__init__.py +0 -0
  12. {python_injection-0.9.5/injection → python_injection-0.9.6/injection/_core}/common/__init__.py +0 -0
  13. {python_injection-0.9.5/injection → python_injection-0.9.6/injection/_core}/common/event.py +0 -0
  14. {python_injection-0.9.5/injection → python_injection-0.9.6/injection/_core}/common/invertible.py +0 -0
  15. {python_injection-0.9.5/injection/common/tools → python_injection-0.9.6/injection/_core/common}/type.py +0 -0
  16. {python_injection-0.9.5 → python_injection-0.9.6}/injection/exceptions.py +0 -0
  17. {python_injection-0.9.5/injection/common/tools → python_injection-0.9.6/injection/integrations}/__init__.py +0 -0
  18. {python_injection-0.9.5 → python_injection-0.9.6}/injection/integrations/blacksheep.py +0 -0
  19. {python_injection-0.9.5 → python_injection-0.9.6}/injection/py.typed +0 -0
  20. {python_injection-0.9.5 → python_injection-0.9.6}/injection/testing/__init__.py +0 -0
  21. {python_injection-0.9.5 → python_injection-0.9.6}/injection/testing/__init__.pyi +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: python-injection
3
- Version: 0.9.5
3
+ Version: 0.9.6
4
4
  Summary: Fast and easy dependency injection framework.
5
5
  Home-page: https://github.com/100nm/python-injection
6
6
  License: MIT
@@ -1,6 +1,6 @@
1
- from .core import Injectable, Module
2
- from .core import Mode as InjectableMode
3
- from .core import Priority as ModulePriority
1
+ from ._core import Injectable, Module
2
+ from ._core import Mode as InjectableMode
3
+ from ._core import Priority as ModulePriority
4
4
 
5
5
  __all__ = (
6
6
  "Injectable",
@@ -1,7 +1,7 @@
1
1
  from abc import abstractmethod
2
2
  from collections.abc import Callable
3
3
  from contextlib import ContextDecorator
4
- from enum import StrEnum
4
+ from enum import Enum
5
5
  from logging import Logger
6
6
  from types import UnionType
7
7
  from typing import (
@@ -13,11 +13,11 @@ from typing import (
13
13
  runtime_checkable,
14
14
  )
15
15
 
16
- from .common.invertible import Invertible
17
- from .common.tools.type import TypeInfo
18
- from .core import InjectableFactory
19
- from .core import ModeStr as InjectableModeStr
20
- from .core import PriorityStr as ModulePriorityStr
16
+ from ._core import InjectableFactory as _InjectableFactory
17
+ from ._core import ModeStr as InjectableModeStr
18
+ from ._core import PriorityStr as ModulePriorityStr
19
+ from ._core.common.invertible import Invertible as _Invertible
20
+ from ._core.common.type import TypeInfo as _TypeInfo
21
21
 
22
22
  _: Module = ...
23
23
 
@@ -62,9 +62,9 @@ class Module:
62
62
  wrapped: Callable[..., T] = ...,
63
63
  /,
64
64
  *,
65
- cls: InjectableFactory[T] = ...,
65
+ cls: _InjectableFactory[T] = ...,
66
66
  inject: bool = ...,
67
- on: TypeInfo[T] = ...,
67
+ on: _TypeInfo[T] = ...,
68
68
  mode: InjectableMode | InjectableModeStr = ...,
69
69
  ):
70
70
  """
@@ -79,7 +79,7 @@ class Module:
79
79
  /,
80
80
  *,
81
81
  inject: bool = ...,
82
- on: TypeInfo[T] = ...,
82
+ on: _TypeInfo[T] = ...,
83
83
  mode: InjectableMode | InjectableModeStr = ...,
84
84
  ):
85
85
  """
@@ -98,7 +98,7 @@ class Module:
98
98
  def set_constant[T](
99
99
  self,
100
100
  instance: T,
101
- on: TypeInfo[T] = ...,
101
+ on: _TypeInfo[T] = ...,
102
102
  *,
103
103
  mode: InjectableMode | InjectableModeStr = ...,
104
104
  ) -> Self:
@@ -125,7 +125,7 @@ class Module:
125
125
  cls: type[T],
126
126
  *,
127
127
  cache: bool = ...,
128
- ) -> Invertible[T | None]:
128
+ ) -> _Invertible[T | None]:
129
129
  """
130
130
  Function used to retrieve an instance associated with the type passed in
131
131
  parameter or `None`. Return a `Invertible` object. To access the instance
@@ -199,7 +199,7 @@ class Module:
199
199
  """
200
200
 
201
201
  @final
202
- class ModulePriority(StrEnum):
202
+ class ModulePriority(Enum):
203
203
  LOW = ...
204
204
  HIGH = ...
205
205
 
@@ -212,7 +212,7 @@ class Injectable[T](Protocol):
212
212
  def get_instance(self) -> T: ...
213
213
 
214
214
  @final
215
- class InjectableMode(StrEnum):
215
+ class InjectableMode(Enum):
216
216
  FALLBACK = ...
217
217
  NORMAL = ...
218
218
  OVERRIDE = ...
@@ -1,7 +1,7 @@
1
1
  from collections.abc import Callable, Iterator, Mapping
2
2
  from types import MappingProxyType
3
3
 
4
- from injection.common.invertible import Invertible
4
+ from injection._core.common.invertible import Invertible
5
5
 
6
6
  __all__ = ("Lazy", "LazyMapping")
7
7
 
@@ -1,3 +1,4 @@
1
+ from collections.abc import Iterator
1
2
  from contextlib import contextmanager
2
3
  from threading import RLock
3
4
 
@@ -5,7 +6,7 @@ __all__ = ("synchronized",)
5
6
 
6
7
 
7
8
  @contextmanager
8
- def synchronized():
9
+ def synchronized() -> Iterator[RLock]:
9
10
  lock = RLock()
10
11
 
11
12
  with lock:
@@ -30,12 +30,13 @@ from typing import (
30
30
  Self,
31
31
  runtime_checkable,
32
32
  )
33
+ from uuid import uuid4
33
34
 
34
- from injection.common.event import Event, EventChannel, EventListener
35
- from injection.common.invertible import Invertible, SimpleInvertible
36
- from injection.common.lazy import Lazy, LazyMapping
37
- from injection.common.tools.threading import synchronized
38
- from injection.common.tools.type import (
35
+ from injection._core.common.event import Event, EventChannel, EventListener
36
+ from injection._core.common.invertible import Invertible, SimpleInvertible
37
+ from injection._core.common.lazy import Lazy, LazyMapping
38
+ from injection._core.common.threading import synchronized
39
+ from injection._core.common.type import (
39
40
  TypeInfo,
40
41
  TypeReport,
41
42
  analyze_types,
@@ -65,12 +66,12 @@ Events
65
66
 
66
67
 
67
68
  @dataclass(frozen=True, slots=True)
68
- class ContainerEvent(Event, ABC):
69
- container: Container
69
+ class LocatorEvent(Event, ABC):
70
+ locator: Locator
70
71
 
71
72
 
72
73
  @dataclass(frozen=True, slots=True)
73
- class ContainerDependenciesUpdated(ContainerEvent):
74
+ class LocatorDependenciesUpdated(LocatorEvent):
74
75
  reports: Collection[TypeReport]
75
76
  mode: Mode
76
77
 
@@ -78,8 +79,8 @@ class ContainerDependenciesUpdated(ContainerEvent):
78
79
  length = len(self.reports)
79
80
  formatted_types = ", ".join(f"`{report.type}`" for report in self.reports)
80
81
  return (
81
- f"{length} container dependenc{'ies' if length > 1 else 'y'} have "
82
- f"been updated{f': {formatted_types}' if formatted_types else ''}."
82
+ f"{length} dependenc{"ies" if length > 1 else "y"} have been "
83
+ f"updated{f": {formatted_types}" if formatted_types else ""}."
83
84
  )
84
85
 
85
86
 
@@ -232,7 +233,7 @@ class Broker(Protocol):
232
233
 
233
234
 
234
235
  """
235
- Container
236
+ Locator
236
237
  """
237
238
 
238
239
 
@@ -259,7 +260,7 @@ class Record[T](NamedTuple):
259
260
 
260
261
 
261
262
  @dataclass(repr=False, frozen=True, slots=True)
262
- class Container(Broker):
263
+ class Locator(Broker):
263
264
  __records: dict[TypeReport, Record] = field(default_factory=dict, init=False)
264
265
  __channel: EventChannel = field(default_factory=EventChannel, init=False)
265
266
 
@@ -294,13 +295,14 @@ class Container(Broker):
294
295
  mode: Mode | ModeStr,
295
296
  ) -> Self:
296
297
  mode = Mode(mode)
298
+ record = Record(injectable, mode)
297
299
  records = {
298
- report: Record(injectable, mode)
300
+ report: record
299
301
  for report in self.__prepare_reports_for_updating(classes, mode)
300
302
  }
301
303
 
302
304
  if records:
303
- event = ContainerDependenciesUpdated(self, records.keys(), mode)
305
+ event = LocatorDependenciesUpdated(self, records.keys(), mode)
304
306
 
305
307
  with self.dispatch(event):
306
308
  self.__records.update(records)
@@ -367,25 +369,25 @@ type InjectableFactory[T] = Callable[[Callable[..., T]], Injectable[T]]
367
369
 
368
370
 
369
371
  @dataclass(eq=False, frozen=True, slots=True)
370
- class Module(EventListener, Broker):
371
- name: str | None = field(default=None)
372
+ class Module(Broker, EventListener):
373
+ name: str = field(default_factory=lambda: f"anonymous@{uuid4().hex[:7]}")
372
374
  __channel: EventChannel = field(
373
375
  default_factory=EventChannel,
374
376
  init=False,
375
377
  repr=False,
376
378
  )
377
- __container: Container = field(
378
- default_factory=Container,
379
+ __locator: Locator = field(
380
+ default_factory=Locator,
379
381
  init=False,
380
382
  repr=False,
381
383
  )
382
- __modules: OrderedDict[Module, None] = field(
383
- default_factory=OrderedDict,
384
+ __loggers: list[Logger] = field(
385
+ default_factory=lambda: [getLogger(__name__)],
384
386
  init=False,
385
387
  repr=False,
386
388
  )
387
- __loggers: list[Logger] = field(
388
- default_factory=lambda: [getLogger(__name__)],
389
+ __modules: OrderedDict[Module, None] = field(
390
+ default_factory=OrderedDict,
389
391
  init=False,
390
392
  repr=False,
391
393
  )
@@ -393,7 +395,7 @@ class Module(EventListener, Broker):
393
395
  __instances: ClassVar[dict[str, Module]] = {}
394
396
 
395
397
  def __post_init__(self):
396
- self.__container.add_listener(self)
398
+ self.__locator.add_listener(self)
397
399
 
398
400
  def __getitem__[T](self, cls: type[T] | UnionType, /) -> Injectable[T]:
399
401
  for broker in self.__brokers:
@@ -402,9 +404,6 @@ class Module(EventListener, Broker):
402
404
 
403
405
  raise NoInjectable(cls)
404
406
 
405
- def __setitem__[T](self, cls: type[T] | UnionType, injectable: Injectable[T], /):
406
- self.update((cls,), injectable)
407
-
408
407
  def __contains__(self, cls: type | UnionType, /) -> bool:
409
408
  return any(cls in broker for broker in self.__brokers)
410
409
 
@@ -415,7 +414,7 @@ class Module(EventListener, Broker):
415
414
  @property
416
415
  def __brokers(self) -> Iterator[Broker]:
417
416
  yield from tuple(self.__modules)
418
- yield self.__container
417
+ yield self.__locator
419
418
 
420
419
  def injectable[T](
421
420
  self,
@@ -517,7 +516,7 @@ class Module(EventListener, Broker):
517
516
  injectable: Injectable[T],
518
517
  mode: Mode | ModeStr = Mode.get_default(),
519
518
  ) -> Self:
520
- self.__container.update(classes, injectable, mode)
519
+ self.__locator.update(classes, injectable, mode)
521
520
  return self
522
521
 
523
522
  def init_modules(self, *modules: Module) -> Self:
@@ -1,15 +1,15 @@
1
1
  from collections.abc import Callable, Iterator
2
2
  from importlib import import_module
3
3
  from pkgutil import walk_packages
4
- from types import ModuleType
4
+ from types import ModuleType as PythonModule
5
5
 
6
6
  __all__ = ("load_package",)
7
7
 
8
8
 
9
9
  def load_package(
10
- package: ModuleType | str,
10
+ package: PythonModule | str,
11
11
  predicate: Callable[[str], bool] = lambda module_name: True,
12
- ) -> tuple[ModuleType, ...]:
12
+ ) -> dict[str, PythonModule]:
13
13
  """
14
14
  Function for importing all modules in a Python package.
15
15
  Pass the `predicate` parameter if you want to filter the modules to be imported.
@@ -18,13 +18,13 @@ def load_package(
18
18
  if isinstance(package, str):
19
19
  package = import_module(package)
20
20
 
21
- return tuple(__iter_modules(package, predicate))
21
+ return dict(__iter_modules_from(package, predicate))
22
22
 
23
23
 
24
- def __iter_modules(
25
- package: ModuleType,
24
+ def __iter_modules_from(
25
+ package: PythonModule,
26
26
  predicate: Callable[[str], bool],
27
- ) -> Iterator[ModuleType]:
27
+ ) -> Iterator[tuple[str, PythonModule]]:
28
28
  try:
29
29
  path = package.__path__
30
30
  except AttributeError as exc:
@@ -38,4 +38,4 @@ def __iter_modules(
38
38
  if info.ispkg or not predicate(name):
39
39
  continue
40
40
 
41
- yield import_module(name)
41
+ yield name, import_module(name)
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "python-injection"
3
- version = "0.9.5"
3
+ version = "0.9.6"
4
4
  description = "Fast and easy dependency injection framework."
5
5
  license = "MIT"
6
6
  authors = ["remimd"]
File without changes