From 88a08af7fb50d3522a3df0d73f1cd2e392313ded Mon Sep 17 00:00:00 2001 From: remimd Date: Fri, 27 Feb 2026 11:40:59 +0100 Subject: [PATCH] fix: expose context return value to middleware in ContextPipeline --- cq/_core/dispatcher/pipe.py | 41 +++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/cq/_core/dispatcher/pipe.py b/cq/_core/dispatcher/pipe.py index f634877..3ca7757 100644 --- a/cq/_core/dispatcher/pipe.py +++ b/cq/_core/dispatcher/pipe.py @@ -152,25 +152,30 @@ def __init__(self, dispatcher: Dispatcher[Any, Any]) -> None: if TYPE_CHECKING: # pragma: no cover @overload - def __get__[O](self, instance: None, owner: type[O], /) -> Dispatcher[I, O]: ... + def __get__[Context]( + self, + instance: None, + owner: type[Context], + /, + ) -> Dispatcher[I, Context]: ... @overload - def __get__[O]( + def __get__[Context]( self, - instance: O, - owner: type[O] | None = ..., + instance: Context, + owner: type[Context] | None = ..., /, - ) -> Dispatcher[I, O]: ... + ) -> Dispatcher[I, Context]: ... @overload def __get__(self, instance: None = ..., owner: None = ..., /) -> Self: ... - def __get__[O]( + def __get__[Context]( self, - instance: O | None = None, - owner: type[O] | None = None, + instance: Context | None = None, + owner: type[Context] | None = None, /, - ) -> Self | Dispatcher[I, O]: + ) -> Self | Dispatcher[I, Context]: if instance is None: if owner is None: return self @@ -231,19 +236,19 @@ def decorator(wp: ConvertMethod[T, Any]) -> ConvertMethod[T, Any]: return decorator(wrapped) if wrapped else decorator - async def __execute[O]( + async def __execute[Context]( self, input_value: I, /, *, - context: O, - context_type: type[O] | None, - ) -> O: - await self.__middleware_group.invoke( - lambda i: self.__steps.execute(i, context, context_type), - input_value, - ) - return context + context: Context, + context_type: type[Context] | None, + ) -> Context: + async def handler(i: I, /) -> Context: + await self.__steps.execute(i, context, context_type) + return context + + return await self.__middleware_group.invoke(handler, input_value) @dataclass(repr=False, eq=False, frozen=True, slots=True)