@@ -59,13 +59,27 @@ class NonVariableArgumentError(Exception):
59
59
"""When vars_only is True but try to retrieve name of
60
60
a non-variable argument"""
61
61
62
+ class ImproperUseError (VarnameRetrievingError ):
63
+ """When varname() is improperly used"""
64
+ def __init__ (self , message : str ) -> None :
65
+ message = (
66
+ f"{ message } \n \n "
67
+ "(Warning: `ImproperUseError` is a subclass of "
68
+ "`VarnameRetrievingError` for now for backward compatibility, "
69
+ "so you can still use `VarnameRetrievingError` to catch it. "
70
+ "But it will be independent of `VarnameRetrievingError` "
71
+ "in the future.)"
72
+ )
73
+ super ().__init__ (message )
74
+
62
75
class MaybeDecoratedFunctionWarning (Warning ):
63
76
"""When a suspecious decorated function used as ignore function directly"""
64
77
65
78
class MultiTargetAssignmentWarning (Warning ):
66
79
"""When varname tries to retrieve variable name in
67
80
a multi-target assignment"""
68
81
82
+
69
83
@lru_cache ()
70
84
def cached_getmodule (codeobj : CodeType ):
71
85
"""Cached version of inspect.getmodule"""
@@ -125,7 +139,7 @@ def lookfor_parent_assign(node: ast.AST) -> Optional[ast.Assign]:
125
139
def node_name (node : ast .AST ) -> Optional [Union [str , Tuple [Union [str , tuple ]]]]:
126
140
"""Get the node node name.
127
141
128
- Raises VarnameRetrievingError when failed
142
+ Raises ImproperUseError when failed
129
143
"""
130
144
if isinstance (node , ast .Name ):
131
145
return node .id
@@ -134,7 +148,7 @@ def node_name(node: ast.AST) -> Optional[Union[str, Tuple[Union[str, tuple]]]]:
134
148
if isinstance (node , (ast .List , ast .Tuple )):
135
149
return tuple (node_name (elem ) for elem in node .elts )
136
150
137
- raise VarnameRetrievingError (
151
+ raise ImproperUseError (
138
152
f"Can only get name of a variable or attribute, "
139
153
f"not { ast .dump (node )} "
140
154
)
0 commit comments