1111
1212import executing
1313
14- __version__ = "0.5.5 "
14+ __version__ = "0.5.6 "
1515__all__ = [
1616 "VarnameRetrievingError" , "varname" , "will" , "inject_varname" ,
17- "inject" , "nameof" , "namedtuple" , "Wrapper" , "debug"
17+ "register" , " inject" , "nameof" , "namedtuple" , "Wrapper" , "debug"
1818]
1919
2020# 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]:
181181 # ast.Attribute
182182 return node .attr
183183
184- def inject_varname (
184+ def register (
185185 cls : type = None , * ,
186186 caller : int = 1 ,
187187 multi_vars : bool = False ,
@@ -201,23 +201,24 @@ def inject_varname(
201201 to retrieve the name.
202202
203203 Examples:
204- >>> @inject_varname
204+ >>> @varname.register
205205 >>> class Foo: pass
206206 >>> foo = Foo()
207207 >>> # foo.__varname__ == 'foo'
208208
209209 Returns:
210210 The wrapper function or the class itself if it is specified explictly.
211211 """
212+
212213 if cls is not None :
213- # Used as @inject_varname directly
214- return inject_varname (
214+ # Used as @register directly
215+ return register (
215216 caller = caller ,
216217 multi_vars = multi_vars ,
217218 raise_exc = raise_exc
218219 )(cls )
219220
220- # Used as @inject_varname (multi_vars=..., raise_exc=...)
221+ # Used as @register (multi_vars=..., raise_exc=...)
221222 def wrapper (cls ):
222223 """The wrapper function to wrap a class and inject `__varname__`"""
223224 orig_init = cls .__init__
@@ -237,6 +238,23 @@ def wrapped_init(self, *args, **kwargs):
237238
238239 return wrapper
239240
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+
240258def inject (obj : object ) -> object :
241259 """Inject attribute `__varname__` to an object
242260
@@ -268,7 +286,7 @@ def inject(obj: object) -> object:
268286 The object with __varname__ injected
269287 """
270288 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." ,
272290 DeprecationWarning )
273291 vname = varname (caller = 0 )
274292 try :
0 commit comments