python-cq 0.21.0__tar.gz → 0.22.0__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.
Files changed (35) hide show
  1. {python_cq-0.21.0 → python_cq-0.22.0}/PKG-INFO +2 -2
  2. {python_cq-0.21.0 → python_cq-0.22.0}/cq/_core/cq.py +1 -3
  3. {python_cq-0.21.0 → python_cq-0.22.0}/cq/_core/di.py +14 -23
  4. {python_cq-0.21.0 → python_cq-0.22.0}/cq/_core/dispatchers/bus.py +1 -1
  5. {python_cq-0.21.0 → python_cq-0.22.0}/cq/_core/pipetools.py +0 -3
  6. {python_cq-0.21.0 → python_cq-0.22.0}/cq/_core/related_events.py +1 -1
  7. {python_cq-0.21.0 → python_cq-0.22.0}/cq/ext/injection.py +7 -14
  8. python_cq-0.22.0/cq/middlewares/contextlib.py +27 -0
  9. {python_cq-0.21.0 → python_cq-0.22.0}/pyproject.toml +2 -2
  10. python_cq-0.21.0/cq/_core/middlewares/scope.py +0 -14
  11. python_cq-0.21.0/cq/middlewares/__init__.py +0 -0
  12. {python_cq-0.21.0 → python_cq-0.22.0}/.gitignore +0 -0
  13. {python_cq-0.21.0 → python_cq-0.22.0}/LICENSE +0 -0
  14. {python_cq-0.21.0 → python_cq-0.22.0}/cq/__init__.py +0 -0
  15. {python_cq-0.21.0 → python_cq-0.22.0}/cq/_core/__init__.py +0 -0
  16. {python_cq-0.21.0 → python_cq-0.22.0}/cq/_core/common/__init__.py +0 -0
  17. {python_cq-0.21.0 → python_cq-0.22.0}/cq/_core/common/typing.py +0 -0
  18. {python_cq-0.21.0 → python_cq-0.22.0}/cq/_core/dispatchers/__init__.py +0 -0
  19. {python_cq-0.21.0 → python_cq-0.22.0}/cq/_core/dispatchers/abc.py +0 -0
  20. {python_cq-0.21.0 → python_cq-0.22.0}/cq/_core/dispatchers/lazy.py +0 -0
  21. {python_cq-0.21.0 → python_cq-0.22.0}/cq/_core/dispatchers/pipe.py +0 -0
  22. {python_cq-0.21.0 → python_cq-0.22.0}/cq/_core/handler.py +0 -0
  23. {python_cq-0.21.0 → python_cq-0.22.0}/cq/_core/message.py +0 -0
  24. {python_cq-0.21.0 → python_cq-0.22.0}/cq/_core/middleware.py +0 -0
  25. {python_cq-0.21.0 → python_cq-0.22.0}/cq/_core/pump.py +0 -0
  26. {python_cq-0.21.0/cq/_core/middlewares → python_cq-0.22.0/cq/_core/queues}/__init__.py +0 -0
  27. {python_cq-0.21.0 → python_cq-0.22.0}/cq/_core/queues/abc.py +0 -0
  28. {python_cq-0.21.0 → python_cq-0.22.0}/cq/_core/queues/memory.py +0 -0
  29. {python_cq-0.21.0 → python_cq-0.22.0}/cq/exceptions.py +0 -0
  30. {python_cq-0.21.0/cq/_core/queues → python_cq-0.22.0/cq/ext}/__init__.py +0 -0
  31. {python_cq-0.21.0/cq/ext → python_cq-0.22.0/cq/middlewares}/__init__.py +0 -0
  32. {python_cq-0.21.0 → python_cq-0.22.0}/cq/middlewares/exc.py +0 -0
  33. {python_cq-0.21.0 → python_cq-0.22.0}/cq/middlewares/retry.py +0 -0
  34. {python_cq-0.21.0 → python_cq-0.22.0}/cq/py.typed +0 -0
  35. {python_cq-0.21.0 → python_cq-0.22.0}/docs/index.md +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: python-cq
3
- Version: 0.21.0
3
+ Version: 0.22.0
4
4
  Summary: CQRS library for async Python projects.
5
5
  Project-URL: Documentation, https://python-cq.remimd.dev
6
6
  Project-URL: Repository, https://github.com/100nm/python-cq
@@ -22,7 +22,7 @@ Classifier: Topic :: Software Development :: Libraries
22
22
  Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
23
23
  Classifier: Topic :: Software Development :: Libraries :: Python Modules
24
24
  Classifier: Typing :: Typed
25
- Requires-Python: <3.15,>=3.12
25
+ Requires-Python: <3.16,>=3.12
26
26
  Requires-Dist: anyio
27
27
  Requires-Dist: type-analyzer
28
28
  Provides-Extra: injection
@@ -10,7 +10,6 @@ from cq._core.handler import (
10
10
  SingleHandlerRegistry,
11
11
  )
12
12
  from cq._core.message import Command, Event, Query
13
- from cq._core.middlewares.scope import CommandDispatchScopeMiddleware
14
13
 
15
14
 
16
15
  class CQ:
@@ -57,8 +56,7 @@ class CQ:
57
56
 
58
57
  def new_command_bus(self) -> Bus[Command, Any]:
59
58
  bus = SimpleBus(self.__command_registry)
60
- command_middleware = CommandDispatchScopeMiddleware(self.__di)
61
- bus.add_middlewares(command_middleware)
59
+ bus.add_middlewares(self.__di.command_scope())
62
60
  return bus
63
61
 
64
62
  def new_event_bus(self) -> Bus[Event, None]:
@@ -3,10 +3,12 @@ from __future__ import annotations
3
3
  from abc import abstractmethod
4
4
  from collections.abc import Awaitable, Callable
5
5
  from contextlib import nullcontext
6
- from typing import TYPE_CHECKING, Any, AsyncContextManager, Protocol, runtime_checkable
6
+ from typing import TYPE_CHECKING, Any, Protocol, runtime_checkable
7
+
8
+ from cq.middlewares.contextlib import AsyncContextManagerMiddleware
7
9
 
8
10
  if TYPE_CHECKING: # pragma: no cover
9
- from cq import CommandBus, EventBus, QueryBus
11
+ from cq import Command, CommandBus, EventBus, Middleware, QueryBus
10
12
 
11
13
 
12
14
  @runtime_checkable
@@ -22,30 +24,19 @@ class DIAdapter(Protocol):
22
24
  __slots__ = ()
23
25
 
24
26
  @abstractmethod
25
- def command_scope(self) -> AsyncContextManager[None]:
27
+ def command_scope(self) -> Middleware[[Command], Any]:
26
28
  """
27
- Return an async context manager that delimits the lifetime of a
28
- command dispatch.
29
+ Return a middleware that wraps each command dispatch.
29
30
 
30
31
  **Responsibilities**
31
32
 
32
- The scope must at minimum manage the lifecycle of a ``RelatedEvents``
33
- instance and register it so that it is resolvable via injection for
34
- the duration of the scope.
35
-
36
- **Nested calls**
37
-
38
- ``command_scope`` is entered in two distinct situations:
39
-
40
- 1. Around a standard command dispatch (via
41
- ``CommandDispatchScopeMiddleware``).
42
- 2. Around each step of a ``ContextCommandPipeline``, which itself
43
- wraps a command dispatch.
33
+ The middleware must at minimum manage the lifecycle of a
34
+ ``RelatedEvents`` instance and register it so that it is resolvable
35
+ via injection for the duration of the dispatch.
44
36
 
45
- This means two nested calls can occur for a single logical command.
46
- Implementations must detect re-entrant activation (e.g. a scope
47
- already active on the current task) and silently ignore the inner
48
- call instead of opening a second, conflicting scope.
37
+ If you already have an async context manager for the scope, wrap it
38
+ with ``cq.middlewares.contextlib.AsyncContextManagerMiddleware``
39
+ instead of writing the middleware by hand.
49
40
  """
50
41
 
51
42
  raise NotImplementedError
@@ -97,8 +88,8 @@ class DIAdapter(Protocol):
97
88
  class NoDI(DIAdapter):
98
89
  __slots__ = ()
99
90
 
100
- def command_scope(self) -> AsyncContextManager[None]:
101
- return nullcontext()
91
+ def command_scope(self) -> Middleware[[Command], Any]:
92
+ return AsyncContextManagerMiddleware(nullcontext())
102
93
 
103
94
  def lazy[T](self, tp: type[T], /) -> Callable[[], Awaitable[T]]:
104
95
  tp_str = getattr(tp, "__name__", str(tp))
@@ -69,7 +69,7 @@ class BaseBus[I, O](BaseDispatcher[I, O], Bus[I, O], ABC):
69
69
 
70
70
  def _trigger_listeners(self, message: I, /, task_group: TaskGroup) -> None:
71
71
  for listener in self.__listeners:
72
- task_group.start_soon(listener, message)
72
+ task_group.start_soon(listener, message) # type: ignore[arg-type]
73
73
 
74
74
 
75
75
  class SimpleBus[I, O](BaseBus[I, O]):
@@ -11,7 +11,6 @@ from cq._core.dispatchers.pipe import (
11
11
  ConvertMethodSync,
12
12
  )
13
13
  from cq._core.message import Command, CommandBus, Query, QueryBus
14
- from cq._core.middlewares.scope import CommandDispatchScopeMiddleware
15
14
 
16
15
 
17
16
  class ContextCommandPipeline[C: Command](ContextPipeline[C]):
@@ -22,8 +21,6 @@ class ContextCommandPipeline[C: Command](ContextPipeline[C]):
22
21
  def __init__(self, di: DIAdapter) -> None:
23
22
  super().__init__(LazyDispatcher(CommandBus, di))
24
23
  self.__query_dispatcher = LazyDispatcher(QueryBus, di)
25
- command_middleware = CommandDispatchScopeMiddleware(di)
26
- self.add_middlewares(command_middleware)
27
24
 
28
25
  def add_static_query_step[Q: Query](self, query: Q, /) -> Self:
29
26
  return self.add_static_step(query, dispatcher=self.__query_dispatcher)
@@ -43,4 +43,4 @@ class AnyIORelatedEvents(RelatedEvents):
43
43
  def add(self, *events: Event) -> None:
44
44
  self.history.extend(events)
45
45
  for event in events:
46
- self.task_group.start_soon(self.emit, event)
46
+ self.task_group.start_soon(self.emit, event) # type: ignore[arg-type]
@@ -1,15 +1,15 @@
1
1
  from collections.abc import AsyncIterator
2
- from contextlib import AsyncExitStack, asynccontextmanager
2
+ from contextlib import AsyncExitStack
3
3
  from dataclasses import dataclass, field
4
4
  from enum import StrEnum
5
- from typing import Any, AsyncContextManager, Awaitable, Callable
5
+ from typing import Any, Awaitable, Callable
6
6
 
7
7
  from injection import Module, adefine_scope, mod
8
8
  from injection.exceptions import ScopeAlreadyDefinedError
9
9
 
10
10
  from cq._core.di import DIAdapter
11
- from cq._core.message import CommandBus, EventBus, QueryBus
12
- from cq._core.middleware import MiddlewareResult
11
+ from cq._core.message import Command, CommandBus, EventBus, QueryBus
12
+ from cq._core.middleware import Middleware, MiddlewareResult
13
13
  from cq._core.related_events import AnyIORelatedEvents, RelatedEvents
14
14
 
15
15
  __all__ = ("CQScope", "InjectionAdapter", "InjectionScopeMiddleware")
@@ -24,12 +24,11 @@ class InjectionAdapter(DIAdapter):
24
24
  module: Module = field(default_factory=mod)
25
25
  threadsafe: bool | None = field(default=None)
26
26
 
27
- def command_scope(self) -> AsyncContextManager[None]:
28
- return InjectionScopeMiddleware( # type: ignore[return-value]
27
+ def command_scope(self) -> Middleware[[Command], Any]:
28
+ return InjectionScopeMiddleware(
29
29
  CQScope.COMMAND_DISPATCH,
30
- exist_ok=True,
31
30
  threadsafe=self.threadsafe,
32
- )._cm
31
+ )
33
32
 
34
33
  def lazy[T](self, tp: type[T], /) -> Callable[[], Awaitable[T]]:
35
34
  awaitable = self.module.aget_lazy_instance(tp, threadsafe=self.threadsafe)
@@ -83,12 +82,6 @@ class InjectionScopeMiddleware:
83
82
  threadsafe: bool | None = field(default=None, kw_only=True)
84
83
 
85
84
  async def __call__(self, /, *args: Any, **kwargs: Any) -> MiddlewareResult[Any]:
86
- async with self._cm: # type: ignore[attr-defined]
87
- yield
88
-
89
- @property
90
- @asynccontextmanager
91
- async def _cm(self) -> AsyncIterator[None]:
92
85
  async with AsyncExitStack() as stack:
93
86
  try:
94
87
  await stack.enter_async_context(
@@ -0,0 +1,27 @@
1
+ from __future__ import annotations
2
+
3
+ from dataclasses import dataclass
4
+ from typing import TYPE_CHECKING, Any, AsyncContextManager, ContextManager
5
+
6
+ if TYPE_CHECKING: # pragma: no cover
7
+ from cq import MiddlewareResult
8
+
9
+ __all__ = ("AsyncContextManagerMiddleware", "ContextManagerMiddleware")
10
+
11
+
12
+ @dataclass(repr=False, eq=False, frozen=True, slots=True)
13
+ class AsyncContextManagerMiddleware:
14
+ context: AsyncContextManager[Any]
15
+
16
+ async def __call__(self, /, *args: Any, **kwargs: Any) -> MiddlewareResult[Any]:
17
+ async with self.context:
18
+ yield
19
+
20
+
21
+ @dataclass(repr=False, eq=False, frozen=True, slots=True)
22
+ class ContextManagerMiddleware:
23
+ context: ContextManager[Any]
24
+
25
+ async def __call__(self, /, *args: Any, **kwargs: Any) -> MiddlewareResult[Any]:
26
+ with self.context:
27
+ yield
@@ -20,12 +20,12 @@ test = [
20
20
 
21
21
  [project]
22
22
  name = "python-cq"
23
- version = "0.21.0"
23
+ version = "0.22.0"
24
24
  description = "CQRS library for async Python projects."
25
25
  license = "MIT"
26
26
  license-files = ["LICENSE"]
27
27
  readme = "docs/index.md"
28
- requires-python = ">=3.12, <3.15"
28
+ requires-python = ">=3.12, <3.16"
29
29
  authors = [{ name = "remimd" }]
30
30
  keywords = ["cqrs"]
31
31
  classifiers = [
@@ -1,14 +0,0 @@
1
- from dataclasses import dataclass
2
- from typing import Any
3
-
4
- from cq._core.di import DIAdapter
5
- from cq._core.middleware import MiddlewareResult
6
-
7
-
8
- @dataclass(repr=False, eq=False, frozen=True, slots=True)
9
- class CommandDispatchScopeMiddleware:
10
- di: DIAdapter
11
-
12
- async def __call__(self, /, *args: Any, **kwargs: Any) -> MiddlewareResult[Any]:
13
- async with self.di.command_scope():
14
- yield
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes