varname
package
varname
Dark magics about variable names in python
Some internal utilities for varname
Classes
config— Global configurations for varname</>VarnameException— Root exception for all varname exceptions</>VarnameRetrievingError— When failed to retrieve the varname</>QualnameNonUniqueError— When a qualified name is used as an ignore element but references tomultiple objects in a module </>ImproperUseError— When varname() is improperly used</>VarnameWarning— Root warning for all varname warnings</>MaybeDecoratedFunctionWarning— When a suspecious decorated function used as ignore function directly</>MultiTargetAssignmentWarning— When varname tries to retrieve variable name ina multi-target assignment </>UsingExecWarning— When exec is used to retrieve function name forargname()</>
Functions
argnode_source(source,node,vars_only)(Union) — Get the source of an argument node</>attach_ignore_id_to_module(module)— Attach the ignore id to module</>bytecode_nameof(code,offset)(str) — Cached Bytecode version of nameof</>cached_getmodule(codeobj)— Cached version of inspect.getmodule</>check_qualname_by_source(source,modname,qualname)— Check if a qualname in module is unique</>debug_ignore_frame(msg,frameinfo)— Print the debug message for a given frame info object</>frame_matches_module_by_ignore_id(frame,module)(bool) — Check if the frame is from the module by ignore id</>get_argument_sources(source,node,func,vars_only)(Mapping) — Get the sources for argument from an ast.Call node</>get_function_called_argname(frame,node)(Callable) — Get the function who called argname</>get_node(frame,ignore,raise_exc,ignore_lambda)(AST) — Try to get node from the executing object.</>get_node_by_frame(frame,raise_exc)(AST) — Get the node by frame, raise errors if possible</>lookfor_parent_assign(node,strict)(Union) — Look for an ast.Assign node in the parents</>node_name(node,subscript_slice)(Union) — Get the node node name.</>reconstruct_func_node(node)(Call) — Reconstruct the ast.Call node from</>rich_exc_message(msg,node,context_lines)(str) — Attach the source code from the node to message toget a rich message for exceptions </>
The frame ignoring system for varname
There 4 mechanisms to ignore intermediate frames to determine the desired one so that a variable name should be retrieved at that frame.
- Ignore frames by a given module. Any calls inside it and inside its
submodules will be ignored. A filename (path) to a module is also acceptable
and recommended when code is executed by
execwithout module available. - Ignore frames by a given pair of module and a qualified name (qualname). See 1) for acceptable modules. The qualname should be unique in that module.
- Ignore frames by a (non-decorated) function.
- Ignore frames by a decorated function. In this case, you can specified a tuple with the function and the number of decorators of it. The decorators on the wrapper function inside the decorators should also be counted.
Any frames in varname, standard libraries, and frames of any expressions like
Classes
IgnoreElem()— An element of the ignore list</>IgnoreModule— Ignore calls from a module or its submodules</>IgnoreFilename— Ignore calls from a module by matching its filename</>IgnoreDirname— Ignore calls from modules inside a directory</>IgnoreStdlib— Ignore standard libraries in sysconfig.get_python_lib(standard_lib=True)</>IgnoreFunction— Ignore a non-decorated function</>IgnoreDecorated— Ignore a decorated function</>IgnoreModuleQualname— Ignore calls by qualified name in the module</>IgnoreFilenameQualname— Ignore calls with given qualname in the module with the filename</>IgnoreOnlyQualname— Ignore calls that match the given qualname, across all frames.</>IgnoreList— The ignore list to match the frames to see if they should be ignored</>
Functions
create_ignore_elem(ignore_elem)(IgnoreElem) — Create an ignore element according to the type</>
Some helper functions builtin based upon core features
Functions
debug(var,*more_vars,prefix,merge,repr,sep,vars_only)— Print variable names and values.</>exec_code(code,globals,locals,sourcefile,frame,ignore,**kwargs)— Execute code where source code is visible at runtime.</>jsobj(*args,vars_only,frame,**kwargs)(Dict) — A wrapper to create a JavaScript-like object</>register(cls_or_func,frame,ignore,multi_vars,raise_exc,strict)(Union) — A decorator to register varname to a class or function</>
Provide core features for varname
Functions
argname(arg,*more_args,func,dispatch,frame,ignore,vars_only)(Union) — Get the names/sources of arguments passed to a function.</>nameof(var,*more_vars,frame,vars_only)(Union) — Get the names of the variables passed in</>varname(frame,ignore,multi_vars,raise_exc,strict)(Union) — Get the name of the variable(s) that assigned by function call orclass instantiation. </>will(frame,raise_exc)(str) — Detect the attribute name right immediately after a function call.</>