1
1
from __future__ import annotations
2
2
3
- from typing import (
4
- Any ,
5
- Callable ,
6
- Literal ,
7
- TypeVar ,
8
- overload ,
9
- )
3
+ from typing import TYPE_CHECKING , Callable , Literal , Mapping , TypeVar , overload
10
4
11
5
from psygnal ._group_descriptor import SignalGroupDescriptor
12
6
7
+ if TYPE_CHECKING :
8
+ from psygnal ._group_descriptor import EqOperator , FieldAliasFunc
9
+
13
10
__all__ = ["evented" ]
14
11
15
12
T = TypeVar ("T" , bound = type )
16
13
17
- EqOperator = Callable [[Any , Any ], bool ]
18
-
19
14
20
15
@overload
21
16
def evented (
@@ -25,6 +20,7 @@ def evented(
25
20
equality_operators : dict [str , EqOperator ] | None = None ,
26
21
warn_on_no_fields : bool = ...,
27
22
cache_on_instance : bool = ...,
23
+ signal_aliases : Mapping [str , str | None ] | FieldAliasFunc | None = ...,
28
24
) -> T : ...
29
25
30
26
@@ -36,6 +32,7 @@ def evented(
36
32
equality_operators : dict [str , EqOperator ] | None = None ,
37
33
warn_on_no_fields : bool = ...,
38
34
cache_on_instance : bool = ...,
35
+ signal_aliases : Mapping [str , str | None ] | FieldAliasFunc | None = ...,
39
36
) -> Callable [[T ], T ]: ...
40
37
41
38
@@ -46,6 +43,7 @@ def evented(
46
43
equality_operators : dict [str , EqOperator ] | None = None ,
47
44
warn_on_no_fields : bool = True ,
48
45
cache_on_instance : bool = True ,
46
+ signal_aliases : Mapping [str , str | None ] | FieldAliasFunc | None = None ,
49
47
) -> Callable [[T ], T ] | T :
50
48
"""A decorator to add events to a dataclass.
51
49
@@ -85,6 +83,14 @@ def evented(
85
83
access, but means that the owner instance will no longer be pickleable. If
86
84
`False`, the SignalGroup instance will *still* be cached, but not on the
87
85
instance itself.
86
+ signal_aliases: Mapping[str, str | None] | Callable[[str], str | None] | None
87
+ If defined, a mapping between field name and signal name. Field names that are
88
+ not `signal_aliases` keys are not aliased (the signal name is the field name).
89
+ If the dict value is None, do not create a signal associated with this field.
90
+ If a callable, the signal name is the output of the function applied to the
91
+ field name. If the output is None, no signal is created for this field.
92
+ If None, defaults to an empty dict, no aliases.
93
+ Default to None
88
94
89
95
Returns
90
96
-------
@@ -122,6 +128,7 @@ def _decorate(cls: T) -> T:
122
128
equality_operators = equality_operators ,
123
129
warn_on_no_fields = warn_on_no_fields ,
124
130
cache_on_instance = cache_on_instance ,
131
+ signal_aliases = signal_aliases ,
125
132
)
126
133
# as a decorator, this will have already been called
127
134
descriptor .__set_name__ (cls , events_namespace )
0 commit comments