11
11
12
12
import executing
13
13
14
- __version__ = "0.5.5 "
14
+ __version__ = "0.5.6 "
15
15
__all__ = [
16
16
"VarnameRetrievingError" , "varname" , "will" , "inject_varname" ,
17
- "inject" , "nameof" , "namedtuple" , "Wrapper" , "debug"
17
+ "register" , " inject" , "nameof" , "namedtuple" , "Wrapper" , "debug"
18
18
]
19
19
20
20
# To show how the desired frame is selected and other frames are skipped
@@ -181,7 +181,7 @@ def will(caller: int = 1, raise_exc: bool = True) -> Optional[str]:
181
181
# ast.Attribute
182
182
return node .attr
183
183
184
- def inject_varname (
184
+ def register (
185
185
cls : type = None , * ,
186
186
caller : int = 1 ,
187
187
multi_vars : bool = False ,
@@ -201,23 +201,24 @@ def inject_varname(
201
201
to retrieve the name.
202
202
203
203
Examples:
204
- >>> @inject_varname
204
+ >>> @varname.register
205
205
>>> class Foo: pass
206
206
>>> foo = Foo()
207
207
>>> # foo.__varname__ == 'foo'
208
208
209
209
Returns:
210
210
The wrapper function or the class itself if it is specified explictly.
211
211
"""
212
+
212
213
if cls is not None :
213
- # Used as @inject_varname directly
214
- return inject_varname (
214
+ # Used as @register directly
215
+ return register (
215
216
caller = caller ,
216
217
multi_vars = multi_vars ,
217
218
raise_exc = raise_exc
218
219
)(cls )
219
220
220
- # Used as @inject_varname (multi_vars=..., raise_exc=...)
221
+ # Used as @register (multi_vars=..., raise_exc=...)
221
222
def wrapper (cls ):
222
223
"""The wrapper function to wrap a class and inject `__varname__`"""
223
224
orig_init = cls .__init__
@@ -237,6 +238,23 @@ def wrapped_init(self, *args, **kwargs):
237
238
238
239
return wrapper
239
240
241
+ def inject_varname (
242
+ cls : type = None , * ,
243
+ caller : int = 1 ,
244
+ multi_vars : bool = False ,
245
+ raise_exc : bool = True
246
+ ) -> Union [type , Callable [[type ], type ]]:
247
+ """Alias of register. Will be deprecated"""
248
+ warnings .warn ("Decorator inject_varname will be removed in 0.6.0. "
249
+ "Use varname.register to decorate your class." ,
250
+ DeprecationWarning )
251
+ return register (
252
+ cls ,
253
+ caller = caller ,
254
+ multi_vars = multi_vars ,
255
+ raise_exc = raise_exc
256
+ )
257
+
240
258
def inject (obj : object ) -> object :
241
259
"""Inject attribute `__varname__` to an object
242
260
@@ -268,7 +286,7 @@ def inject(obj: object) -> object:
268
286
The object with __varname__ injected
269
287
"""
270
288
warnings .warn ("Function inject will be removed in 0.6.0. Use "
271
- "varname.inject_varname to decorate your class." ,
289
+ "varname.register to decorate your class." ,
272
290
DeprecationWarning )
273
291
vname = varname (caller = 0 )
274
292
try :
0 commit comments