37
37
from collections import abc as collections
38
38
39
39
if typing .TYPE_CHECKING :
40
+ import typing_extensions
40
41
from typing_extensions import Self
41
42
43
+ _P = typing_extensions .ParamSpec ("_P" )
42
44
_CoroT = collections .Coroutine [typing .Any , typing .Any , "_T" ]
43
45
44
46
45
47
_T = typing .TypeVar ("_T" )
48
+ _OtherT = typing .TypeVar ("_OtherT" )
46
49
_DictKeyT = typing .Union [str , int , float , bool , None ]
47
50
_DictValueT = typing .Union [
48
51
collections .Mapping [_DictKeyT , "_DictValueT" ], collections .Sequence ["_DictValueT" ], _DictKeyT
@@ -56,7 +59,7 @@ class TypeConfig(typing.Generic[_T]):
56
59
these.
57
60
"""
58
61
59
- __slots__ = ("_async_cleanup" , "_async_create" , "_cleanup" , "_create" , "_dep_type" , "_dependencies" , " _name" )
62
+ __slots__ = ("_async_cleanup" , "_async_create" , "_cleanup" , "_create" , "_dep_type" , "_name" )
60
63
61
64
@typing .overload
62
65
def __init__ (
@@ -69,7 +72,6 @@ def __init__(
69
72
async_create : collections .Callable [..., _CoroT [_T ]],
70
73
cleanup : typing .Optional [collections .Callable [[_T ], None ]] = None ,
71
74
create : typing .Optional [collections .Callable [..., _T ]] = None ,
72
- dependencies : collections .Sequence [type [typing .Any ]] = (),
73
75
) -> None : ...
74
76
75
77
@typing .overload
@@ -83,7 +85,6 @@ def __init__(
83
85
async_create : typing .Optional [collections .Callable [..., _CoroT [_T ]]] = None ,
84
86
cleanup : typing .Optional [collections .Callable [[_T ], None ]] = None ,
85
87
create : collections .Callable [..., _T ],
86
- dependencies : collections .Sequence [type [typing .Any ]] = (),
87
88
) -> None : ...
88
89
89
90
def __init__ (
@@ -96,7 +97,6 @@ def __init__(
96
97
async_create : typing .Optional [collections .Callable [..., _CoroT [_T ]]] = None ,
97
98
cleanup : typing .Optional [collections .Callable [[_T ], None ]] = None ,
98
99
create : typing .Optional [collections .Callable [..., _T ]] = None ,
99
- dependencies : collections .Sequence [type [typing .Any ]] = (),
100
100
) -> None :
101
101
"""Initialise a type config.
102
102
@@ -119,8 +119,6 @@ def __init__(
119
119
Callback used to use to destroy the dependency in a sync runtime.
120
120
create
121
121
Callback used to use to create the dependency in a sync runtime.
122
- dependencies
123
- Sequence of type dependencies that are required to create this dependency.
124
122
125
123
Raises
126
124
------
@@ -135,9 +133,56 @@ def __init__(
135
133
self ._cleanup = cleanup
136
134
self ._create = create
137
135
self ._dep_type = dep_type
138
- self ._dependencies = dependencies
139
136
self ._name = name
140
137
138
+ @classmethod
139
+ def from_create (
140
+ cls , dep_type : type [_OtherT ], name : str , /
141
+ ) -> collections .Callable [[collections .Callable [..., _OtherT ]], TypeConfig [_OtherT ]]:
142
+ """Initialise a type config by decorating a sync create callback.
143
+
144
+ Parameters
145
+ ----------
146
+ dep_type
147
+ Type of the dep this should be registered for.
148
+ name
149
+ Name used to identify this type dependency in configuration files.
150
+
151
+ Returns
152
+ -------
153
+ TypeConfig
154
+ The created type config.
155
+ """
156
+
157
+ def decorator (callback : collections .Callable [..., _OtherT ], / ) -> TypeConfig [_OtherT ]:
158
+ return cls (dep_type , name , create = callback )
159
+
160
+ return decorator
161
+
162
+ @classmethod
163
+ def from_async_create (
164
+ cls , dep_type : type [_OtherT ], name : str , /
165
+ ) -> collections .Callable [[collections .Callable [..., _CoroT [_OtherT ]]], TypeConfig [_OtherT ]]:
166
+ """Initialise a type config by decorating an async create callback.
167
+
168
+ Parameters
169
+ ----------
170
+ dep_type
171
+ Type of the dep this should be registered for.
172
+ name
173
+ Name used to identify this type dependency in configuration files.
174
+
175
+ Returns
176
+ -------
177
+ TypeConfig
178
+ The created type config.
179
+ """
180
+
181
+ def decorator (callback : collections .Callable [..., _CoroT [_OtherT ]], / ) -> TypeConfig [_OtherT ]:
182
+ return cls (dep_type , name , async_create = callback )
183
+
184
+ return decorator
185
+
141
186
@property
142
187
def async_cleanup (self ) -> typing .Optional [collections .Callable [[_T ], _CoroT [None ]]]:
143
188
"""Callback used to use to cleanup the dependency in an async runtime."""
@@ -163,16 +208,59 @@ def dep_type(self) -> type[_T]:
163
208
"""The type created values should be registered as a type dependency for."""
164
209
return self ._dep_type
165
210
166
- @property
167
- def dependencies (self ) -> collections .Sequence [type [typing .Any ]]:
168
- """Sequence of type dependencies that are required to create this dependency."""
169
- return self ._dependencies
170
-
171
211
@property
172
212
def name (self ) -> str :
173
213
"""Name used to identify this type dependency in configuration files."""
174
214
return self ._name
175
215
216
+ def with_create (self , callback : collections .Callable [_P , _T ], / ) -> collections .Callable [_P , _T ]:
217
+ """Set the synchronous create callback through a decorator call.
218
+
219
+ Parameters
220
+ ----------
221
+ callback
222
+ The callback to set as the synchronous create callback.
223
+ """
224
+ self ._create = callback
225
+ return callback
226
+
227
+ def with_async_create (
228
+ self , callback : collections .Callable [_P , _CoroT [_T ]], /
229
+ ) -> collections .Callable [_P , _CoroT [_T ]]:
230
+ """Set the asynchronous create callback through a decorator call.
231
+
232
+ Parameters
233
+ ----------
234
+ callback
235
+ The callback to set as the asynchronous create callback.
236
+ """
237
+ self ._async_create = callback
238
+ return callback
239
+
240
+ def with_cleanup (self , callback : collections .Callable [[_T ], None ], / ) -> collections .Callable [[_T ], None ]:
241
+ """Set the synchronous cleanup callback through a decorator call.
242
+
243
+ Parameters
244
+ ----------
245
+ callback
246
+ The callback to set as the asynchronous cleanup callback.
247
+ """
248
+ self ._cleanup = callback
249
+ return callback
250
+
251
+ def with_async_cleanup (
252
+ self , callback : collections .Callable [[_T ], _CoroT [None ]], /
253
+ ) -> collections .Callable [[_T ], _CoroT [None ]]:
254
+ """Set the asynchronous cleanup callback through a decorator call.
255
+
256
+ Parameters
257
+ ----------
258
+ callback
259
+ The callback to set as the synchronous cleanup callback.
260
+ """
261
+ self ._async_cleanup = callback
262
+ return callback
263
+
176
264
177
265
class PluginConfig (abc .ABC ):
178
266
"""Base class used for configuring plugins loaded via Alluka's manager.
0 commit comments