@@ -59,13 +59,27 @@ class NonVariableArgumentError(Exception):
5959 """When vars_only is True but try to retrieve name of
6060 a non-variable argument"""
6161
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+
6275class MaybeDecoratedFunctionWarning (Warning ):
6376 """When a suspecious decorated function used as ignore function directly"""
6477
6578class MultiTargetAssignmentWarning (Warning ):
6679 """When varname tries to retrieve variable name in
6780 a multi-target assignment"""
6881
82+
6983@lru_cache ()
7084def cached_getmodule (codeobj : CodeType ):
7185 """Cached version of inspect.getmodule"""
@@ -125,7 +139,7 @@ def lookfor_parent_assign(node: ast.AST) -> Optional[ast.Assign]:
125139def node_name (node : ast .AST ) -> Optional [Union [str , Tuple [Union [str , tuple ]]]]:
126140 """Get the node node name.
127141
128- Raises VarnameRetrievingError when failed
142+ Raises ImproperUseError when failed
129143 """
130144 if isinstance (node , ast .Name ):
131145 return node .id
@@ -134,7 +148,7 @@ def node_name(node: ast.AST) -> Optional[Union[str, Tuple[Union[str, tuple]]]]:
134148 if isinstance (node , (ast .List , ast .Tuple )):
135149 return tuple (node_name (elem ) for elem in node .elts )
136150
137- raise VarnameRetrievingError (
151+ raise ImproperUseError (
138152 f"Can only get name of a variable or attribute, "
139153 f"not { ast .dump (node )} "
140154 )
0 commit comments