Skip to content

Commit 05987df

Browse files
authored
refactor: extract shared Decorator type and simplify step overloads
1 parent 51fae3b commit 05987df

7 files changed

Lines changed: 44 additions & 43 deletions

File tree

cq/_core/common/__init__.py

Whitespace-only changes.

cq/_core/common/typing.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from typing import Protocol
2+
3+
4+
class Decorator(Protocol):
5+
def __call__[T](self, wrapped: T, /) -> T: ...

cq/_core/dispatcher/pipe.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from dataclasses import dataclass, field
44
from typing import TYPE_CHECKING, Any, Protocol, Self, overload
55

6+
from cq._core.common.typing import Decorator
67
from cq._core.dispatcher.base import BaseDispatcher, Dispatcher
78
from cq._core.middleware import Middleware
89

@@ -46,13 +47,13 @@ def step[T](
4647
) -> PipeConverter[T, Any]: ...
4748

4849
@overload
49-
def step[T](
50+
def step(
5051
self,
5152
wrapped: None = ...,
5253
/,
5354
*,
54-
dispatcher: Dispatcher[T, Any] | None = ...,
55-
) -> Callable[[PipeConverter[T, Any]], PipeConverter[T, Any]]: ...
55+
dispatcher: Dispatcher[Any, Any] | None = ...,
56+
) -> Decorator: ...
5657

5758
def step[T](
5859
self,
@@ -163,13 +164,13 @@ def step[T](
163164
) -> PipeConverterMethod[T, Any]: ...
164165

165166
@overload
166-
def step[T](
167+
def step(
167168
self,
168169
wrapped: None = ...,
169170
/,
170171
*,
171-
dispatcher: Dispatcher[T, Any] | None = ...,
172-
) -> Callable[[PipeConverterMethod[T, Any]], PipeConverterMethod[T, Any]]: ...
172+
dispatcher: Dispatcher[Any, Any] | None = ...,
173+
) -> Decorator: ...
173174

174175
def step[T](
175176
self,

cq/_core/handler.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import injection
1111
from type_analyzer import MatchingTypesConfig, iter_matching_types, matching_types
1212

13+
from cq._core.common.typing import Decorator
14+
1315
type HandlerType[**P, T] = type[Handler[P, T]]
1416
type HandlerFactory[**P, T] = Callable[..., Awaitable[Handler[P, T]]]
1517

@@ -90,10 +92,6 @@ def subscribe(self, input_type: type[I], factory: HandlerFactory[[I], O]) -> Sel
9092
return self
9193

9294

93-
class _Decorator(Protocol):
94-
def __call__[T](self, wrapped: T, /) -> T: ...
95-
96-
9795
@dataclass(repr=False, eq=False, frozen=True, slots=True)
9896
class HandlerDecorator[I, O]:
9997
registry: HandlerRegistry[I, O]
@@ -108,7 +106,7 @@ def __call__(
108106
/,
109107
*,
110108
threadsafe: bool | None = ...,
111-
) -> _Decorator: ...
109+
) -> Decorator: ...
112110

113111
@overload
114112
def __call__[T](
@@ -126,7 +124,7 @@ def __call__(
126124
/,
127125
*,
128126
threadsafe: bool | None = ...,
129-
) -> _Decorator: ...
127+
) -> Decorator: ...
130128

131129
def __call__[T](
132130
self,

cq/_core/message.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
command_handler: Final[HandlerDecorator[Command, Any]] = HandlerDecorator(
2727
SingleHandlerRegistry(),
2828
)
29-
event_handler: Final[HandlerDecorator[Event, None]] = HandlerDecorator(
29+
event_handler: Final[HandlerDecorator[Event, Any]] = HandlerDecorator(
3030
MultipleHandlerRegistry(),
3131
)
3232
query_handler: Final[HandlerDecorator[Query, Any]] = HandlerDecorator(

cq/_core/pipetools.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
from typing import TYPE_CHECKING, Any, Callable, overload
1+
from typing import TYPE_CHECKING, Any, overload
22

33
import injection
44

55
from cq import Dispatcher
6+
from cq._core.common.typing import Decorator
67
from cq._core.dispatcher.lazy import LazyDispatcher
78
from cq._core.dispatcher.pipe import ContextPipeline, PipeConverterMethod
89
from cq._core.message import AnyCommandBus, Command, Query, QueryBus
@@ -52,11 +53,7 @@ def query_step[T: Query](
5253
) -> PipeConverterMethod[T, Any]: ...
5354

5455
@overload
55-
def query_step[T: Query](
56-
self,
57-
wrapped: None = ...,
58-
/,
59-
) -> Callable[[PipeConverterMethod[T, Any]], PipeConverterMethod[T, Any]]: ...
56+
def query_step(self, wrapped: None = ..., /) -> Decorator: ...
6057

6158
def query_step[T: Query](
6259
self,

uv.lock

Lines changed: 24 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)