1616 cast ,
1717)
1818
19- __all__ = ["EventIterator" , "Event" ]
19+ from typing_extensions import ParamSpec
20+
21+ __all__ = ["event_iterator" , "event" ]
2022
2123_TResult = TypeVar ("_TResult" )
22- _TCallable = TypeVar ( "_TCallable" , bound = Callable [..., Any ] )
24+ _TParams = ParamSpec ( "_TParams" )
2325
2426
25- class EventResultIteratorBase (Generic [_TCallable , _TResult ]):
27+ class EventResultIteratorBase (Generic [_TParams , _TResult ]):
2628 def __init__ (self ) -> None :
2729 self ._lock = threading .RLock ()
2830
2931 self ._listeners : MutableSet [weakref .ref [Any ]] = set ()
3032
31- def add (self , callback : _TCallable ) -> None :
33+ def add (self , callback : Callable [ _TParams , _TResult ] ) -> None :
3234 def remove_listener (ref : Any ) -> None :
3335 with self ._lock :
3436 self ._listeners .remove (ref )
@@ -39,7 +41,7 @@ def remove_listener(ref: Any) -> None:
3941 else :
4042 self ._listeners .add (weakref .ref (callback , remove_listener ))
4143
42- def remove (self , callback : _TCallable ) -> None :
44+ def remove (self , callback : Callable [ _TParams , _TResult ] ) -> None :
4345 with self ._lock :
4446 try :
4547 if inspect .ismethod (callback ):
@@ -61,33 +63,37 @@ def __len__(self) -> int:
6163 def __bool__ (self ) -> bool :
6264 return len (self ._listeners ) > 0
6365
64- def __iter__ (self ) -> Iterator [_TCallable ]:
66+ def __iter__ (self ) -> Iterator [Callable [ _TParams , _TResult ] ]:
6567 for r in self ._listeners :
6668 c = r ()
6769 if c is not None :
6870 yield c
6971
70- def _notify (self , * args : Any , ** kwargs : Any ) -> Iterator [_TResult ]:
72+ def _notify (self , * __args : _TParams . args , ** __kwargs : _TParams . kwargs ) -> Iterator [_TResult ]:
7173 for method in set (self ):
72- yield method (* args , ** kwargs )
74+ yield method (* __args , ** __kwargs )
7375
7476
75- class EventIterator (EventResultIteratorBase [_TCallable , _TResult ]):
76- def __call__ (self , * args : Any , ** kwargs : Any ) -> Iterator [_TResult ]:
77- return self ._notify (* args , ** kwargs )
77+ class EventIterator (EventResultIteratorBase [_TParams , _TResult ]):
78+ def __call__ (self , * __args : _TParams . args , ** __kwargs : _TParams . kwargs ) -> Iterator [_TResult ]:
79+ return self ._notify (* __args , ** __kwargs )
7880
7981
80- class Event (EventResultIteratorBase [_TCallable , _TResult ]):
81- def __call__ (self , * args : Any , ** kwargs : Any ) -> List [_TResult ]:
82- return list (self ._notify (* args , ** kwargs ))
82+ class Event (EventResultIteratorBase [_TParams , _TResult ]):
83+ def __call__ (self , * __args : _TParams . args , ** __kwargs : _TParams . kwargs ) -> List [_TResult ]:
84+ return list (self ._notify (* __args , ** __kwargs ))
8385
8486
8587_TEvent = TypeVar ("_TEvent" )
8688
8789
88- class EventDescriptorBase (Generic [_TCallable , _TResult , _TEvent ]):
90+ class EventDescriptorBase (Generic [_TParams , _TResult , _TEvent ]):
8991 def __init__ (
90- self , _func : _TCallable , factory : Callable [..., _TEvent ], * factory_args : Any , ** factory_kwargs : Any
92+ self ,
93+ _func : Callable [_TParams , _TResult ],
94+ factory : Callable [..., _TEvent ],
95+ * factory_args : Any ,
96+ ** factory_kwargs : Any ,
9197 ) -> None :
9298 self ._func = _func
9399 self .__factory = factory
@@ -111,11 +117,11 @@ def __get__(self, obj: Any, objtype: Type[Any]) -> _TEvent:
111117 return cast ("_TEvent" , getattr (obj , name ))
112118
113119
114- class event_iterator (EventDescriptorBase [_TCallable , Any , EventIterator [_TCallable , Any ]]): # noqa: N801
115- def __init__ (self , _func : _TCallable ) -> None :
116- super ().__init__ (_func , EventIterator [_TCallable , Any ])
120+ class event_iterator (EventDescriptorBase [_TParams , _TResult , EventIterator [_TParams , _TResult ]]): # noqa: N801
121+ def __init__ (self , _func : Callable [ _TParams , _TResult ] ) -> None :
122+ super ().__init__ (_func , EventIterator [_TParams , _TResult ])
117123
118124
119- class event (EventDescriptorBase [_TCallable , Any , Event [_TCallable , Any ]]): # noqa: N801
120- def __init__ (self , _func : _TCallable ) -> None :
121- super ().__init__ (_func , Event [_TCallable , Any ])
125+ class event (EventDescriptorBase [_TParams , _TResult , Event [_TParams , _TResult ]]): # noqa: N801
126+ def __init__ (self , _func : Callable [ _TParams , _TResult ] ) -> None :
127+ super ().__init__ (_func , Event [_TParams , _TResult ])
0 commit comments