33from collections .abc import Awaitable , Callable , Iterator
44from dataclasses import dataclass , field
55from functools import partial
6- from inspect import Parameter , isclass
6+ from inspect import Parameter , isclass , unwrap
77from inspect import signature as inspect_signature
88from typing import TYPE_CHECKING , Any , Protocol , Self , overload , runtime_checkable
99
@@ -27,14 +27,23 @@ async def handle(self, /, *args: P.args, **kwargs: P.kwargs) -> T:
2727
2828@dataclass (repr = False , eq = False , frozen = True , slots = True )
2929class HandleFunction [** P , T ]:
30- handler_factory : HandlerFactory [P , T ]
31- handler_type : HandlerType [P , T ] | None = field ( default = None )
32- fail_silently : bool = field ( default = False )
30+ factory : HandlerFactory [P , T ]
31+ source : HandlerType [P , T ] | Any
32+ fail_silently : bool
3333
3434 async def __call__ (self , / , * args : P .args , ** kwargs : P .kwargs ) -> T :
35- handler = await self .handler_factory ()
35+ handler = await self .factory ()
3636 return await handler .handle (* args , ** kwargs )
3737
38+ @classmethod
39+ def create (
40+ cls ,
41+ factory : HandlerFactory [P , T ],
42+ source : HandlerType [P , T ] | None = None ,
43+ fail_silently : bool = False ,
44+ ) -> Self :
45+ return cls (factory , source or unwrap (factory ), fail_silently )
46+
3847
3948@runtime_checkable
4049class HandlerRegistry [I , O ](Protocol ):
@@ -73,7 +82,7 @@ def subscribe(
7382 handler_type : HandlerType [[I ], O ] | None = None ,
7483 fail_silently : bool = False ,
7584 ) -> Self :
76- function = HandleFunction (handler_factory , handler_type , fail_silently )
85+ function = HandleFunction . create (handler_factory , handler_type , fail_silently )
7786
7887 for key_type in _build_key_types (input_type ):
7988 self .__values [key_type ].append (function )
@@ -101,7 +110,7 @@ def subscribe(
101110 handler_type : HandlerType [[I ], O ] | None = None ,
102111 fail_silently : bool = False ,
103112 ) -> Self :
104- function = HandleFunction (handler_factory , handler_type , fail_silently )
113+ function = HandleFunction . create (handler_factory , handler_type , fail_silently )
105114 entries = {key_type : function for key_type in _build_key_types (input_type )}
106115
107116 for key_type in entries :
0 commit comments