1111
1212__version__ = "0.3.0"
1313
14- VARNAME_INDEX = [- 1 ]
15-
16- class MultipleTargetAssignmentWarning (Warning ):
17- """When multiple-target assignment found, i.e. y = x = func()"""
18-
19- class VarnameRetrievingWarning (Warning ):
20- """When varname retrieving failed for whatever reason"""
21-
2214class VarnameRetrievingError (Exception ):
2315 """When failed to retrieve the varname"""
2416
25- def varname (caller : int = 1 , raise_exc : bool = False ) -> str :
17+ def varname (caller : int = 1 , raise_exc : bool = True ) -> str :
2618 """Get the variable name that assigned by function/class calls
2719
2820 Args:
@@ -32,9 +24,7 @@ def varname(caller: int = 1, raise_exc: bool = False) -> str:
3224 to retrieve the name.
3325
3426 Returns:
35- str|None: The variable name, or
36- `var_<index>` (will be deprecated in `v0.4.0`) if failed.
37- It returns `None` when `raise_exc` is `False` and
27+ str|None: The variable name, or `None` when `raise_exc` is `False` and
3828 we failed to retrieve the variable name.
3929
4030 Raises:
@@ -44,44 +34,34 @@ def varname(caller: int = 1, raise_exc: bool = False) -> str:
4434 is set to `True`.
4535
4636 Warns:
47- MultipleTargetAssignmentWarning : When there are multiple target
37+ UserWarning : When there are multiple target
4838 in the assign node. (e.g: `a = b = func()`, in such a case,
4939 `b == 'a'`, may not be the case you want)
50- VarnameRetrievingWarning: When `var_0` and alike returned.
51- This will be deprecated in `v0.4.0`
5240 """
5341 node = _get_node (caller )
5442 if not node :
5543 if raise_exc :
5644 raise VarnameRetrievingError ("Unable to retrieve the ast node." )
57- VARNAME_INDEX [0 ] += 1
58- warnings .warn (f"var_{ VARNAME_INDEX [0 ]} used, "
59- "which will be deprecated in v0.4.0" ,
60- VarnameRetrievingWarning )
61- return f"var_{ VARNAME_INDEX [0 ]} "
45+ return None
6246
6347 node = _lookfor_parent_assign (node )
6448 if not node :
6549 if raise_exc :
6650 raise VarnameRetrievingError (
6751 'Failed to retrieve the variable name.'
6852 )
69- VARNAME_INDEX [0 ] += 1
70- warnings .warn (f"var_{ VARNAME_INDEX [0 ]} used, "
71- "which will be deprecated in v0.4.0" ,
72- VarnameRetrievingWarning )
73- return f"var_{ VARNAME_INDEX [0 ]} "
53+ return None
7454
7555 # Need to actually check that there's just one
7656 # give warnings if: a = b = func()
7757 if len (node .targets ) > 1 :
7858 warnings .warn ("Multiple targets in assignment, variable name "
7959 "on the very left will be used." ,
80- MultipleTargetAssignmentWarning )
60+ UserWarning )
8161 target = node .targets [0 ]
8262 return _node_name (target )
8363
84- def will (caller : int = 1 , raise_exc : bool = False ) -> str :
64+ def will (caller : int = 1 , raise_exc : bool = True ) -> str :
8565 """Detect the attribute name right immediately after a function call.
8666
8767 Examples:
@@ -259,7 +239,7 @@ class Wrapper:
259239 value (any): The value this wrapper wraps
260240 """
261241
262- def __init__ (self , value : any , raise_exc : bool = False ):
242+ def __init__ (self , value : any , raise_exc : bool = True ):
263243 self .name : str = varname (raise_exc = raise_exc )
264244 self .value : any = value
265245
0 commit comments