add additional typing for calling events
This commit is contained in:
parent
d63b3a2bce
commit
c8df48728b
@ -16,6 +16,7 @@ from typing import (
|
|||||||
Generic,
|
Generic,
|
||||||
List,
|
List,
|
||||||
Optional,
|
Optional,
|
||||||
|
Self,
|
||||||
Tuple,
|
Tuple,
|
||||||
Type,
|
Type,
|
||||||
TypeVar,
|
TypeVar,
|
||||||
@ -1445,13 +1446,8 @@ class LiteralEventChainVar(ArgsFunctionOperation, LiteralVar, EventChainVar):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
G = ParamSpec("G")
|
|
||||||
|
|
||||||
IndividualEventType = Union[EventSpec, EventHandler, Callable[G, Any], Var[Any]]
|
|
||||||
|
|
||||||
EventType = Union[IndividualEventType[G], List[IndividualEventType[G]]]
|
|
||||||
|
|
||||||
P = ParamSpec("P")
|
P = ParamSpec("P")
|
||||||
|
Q = ParamSpec("Q")
|
||||||
T = TypeVar("T")
|
T = TypeVar("T")
|
||||||
V = TypeVar("V")
|
V = TypeVar("V")
|
||||||
V2 = TypeVar("V2")
|
V2 = TypeVar("V2")
|
||||||
@ -1474,49 +1470,47 @@ if sys.version_info >= (3, 10):
|
|||||||
self.func = func
|
self.func = func
|
||||||
|
|
||||||
@overload
|
@overload
|
||||||
def __get__(
|
def __call__(
|
||||||
self: EventCallback[[V], T], instance: None, owner
|
self: EventCallback[Concatenate[V, Q], T], value: V | Var[V]
|
||||||
) -> Callable[[Union[Var[V], V]], EventSpec]: ...
|
) -> EventCallback[Q, T]: ...
|
||||||
|
|
||||||
@overload
|
@overload
|
||||||
def __get__(
|
def __call__(
|
||||||
self: EventCallback[[V, V2], T], instance: None, owner
|
self: EventCallback[Concatenate[V, V2, Q], T],
|
||||||
) -> Callable[[Union[Var[V], V], Union[Var[V2], V2]], EventSpec]: ...
|
value: V | Var[V],
|
||||||
|
value2: V2 | Var[V2],
|
||||||
|
) -> EventCallback[Q, T]: ...
|
||||||
|
|
||||||
@overload
|
@overload
|
||||||
def __get__(
|
def __call__(
|
||||||
self: EventCallback[[V, V2, V3], T], instance: None, owner
|
self: EventCallback[Concatenate[V, V2, V3, Q], T],
|
||||||
) -> Callable[
|
value: V | Var[V],
|
||||||
[Union[Var[V], V], Union[Var[V2], V2], Union[Var[V3], V3]],
|
value2: V2 | Var[V2],
|
||||||
EventSpec,
|
value3: V3 | Var[V3],
|
||||||
]: ...
|
) -> EventCallback[Q, T]: ...
|
||||||
|
|
||||||
@overload
|
@overload
|
||||||
def __get__(
|
def __call__(
|
||||||
self: EventCallback[[V, V2, V3, V4], T], instance: None, owner
|
self: EventCallback[Concatenate[V, V2, V3, V4, Q], T],
|
||||||
) -> Callable[
|
value: V | Var[V],
|
||||||
[
|
value2: V2 | Var[V2],
|
||||||
Union[Var[V], V],
|
value3: V3 | Var[V3],
|
||||||
Union[Var[V2], V2],
|
value4: V4 | Var[V4],
|
||||||
Union[Var[V3], V3],
|
) -> EventCallback[Q, T]: ...
|
||||||
Union[Var[V4], V4],
|
|
||||||
],
|
def __call__(self, *values) -> EventCallback: # type: ignore
|
||||||
EventSpec,
|
"""Call the function with the values.
|
||||||
]: ...
|
|
||||||
|
Args:
|
||||||
|
*values: The values to call the function with.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
The function with the values.
|
||||||
|
"""
|
||||||
|
return self.func(*values) # type: ignore
|
||||||
|
|
||||||
@overload
|
@overload
|
||||||
def __get__(
|
def __get__(self, instance: None, owner) -> Self: ...
|
||||||
self: EventCallback[[V, V2, V3, V4, V5], T], instance: None, owner
|
|
||||||
) -> Callable[
|
|
||||||
[
|
|
||||||
Union[Var[V], V],
|
|
||||||
Union[Var[V2], V2],
|
|
||||||
Union[Var[V3], V3],
|
|
||||||
Union[Var[V4], V4],
|
|
||||||
Union[Var[V5], V5],
|
|
||||||
],
|
|
||||||
EventSpec,
|
|
||||||
]: ...
|
|
||||||
|
|
||||||
@overload
|
@overload
|
||||||
def __get__(self, instance, owner) -> Callable[P, T]: ...
|
def __get__(self, instance, owner) -> Callable[P, T]: ...
|
||||||
@ -1548,6 +1542,9 @@ if sys.version_info >= (3, 10):
|
|||||||
return func # type: ignore
|
return func # type: ignore
|
||||||
else:
|
else:
|
||||||
|
|
||||||
|
class EventCallback(Generic[P, T]):
|
||||||
|
"""A descriptor that wraps a function to be used as an event."""
|
||||||
|
|
||||||
def event_handler(func: Callable[P, T]) -> Callable[P, T]:
|
def event_handler(func: Callable[P, T]) -> Callable[P, T]:
|
||||||
"""Wrap a function to be used as an event.
|
"""Wrap a function to be used as an event.
|
||||||
|
|
||||||
@ -1560,6 +1557,15 @@ else:
|
|||||||
return func
|
return func
|
||||||
|
|
||||||
|
|
||||||
|
G = ParamSpec("G")
|
||||||
|
|
||||||
|
IndividualEventType = Union[
|
||||||
|
EventSpec, EventHandler, Callable[G, Any], EventCallback[G, Any], Var[Any]
|
||||||
|
]
|
||||||
|
|
||||||
|
EventType = Union[IndividualEventType[G], List[IndividualEventType[G]]]
|
||||||
|
|
||||||
|
|
||||||
class EventNamespace(types.SimpleNamespace):
|
class EventNamespace(types.SimpleNamespace):
|
||||||
"""A namespace for event related classes."""
|
"""A namespace for event related classes."""
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user