python-varname

Dark magics about variable names in python

MIT License

Downloads
252.1K
Stars
298
Committers
5

Bot releases are hidden (Show)

python-varname -

Published by pwwang over 3 years ago

  • Add ImproperUseError to distinguish node retrieving error from improper varname use #49
python-varname -

Published by pwwang over 3 years ago

  • Fix standard library ignoring ignores 3rd-party libraries under site-packages/
  • Allow pathlib.Path object to be used in ignore items
python-varname -

Published by pwwang over 3 years ago

  • Remove argument full for nameof, use vars_only instead. When vars_only=False, source of the argument returned.
    # before:
    nameof(a.b, full=True) # 'a.b'
    nameof(x[0], full=True) # unable to fetch
    # after (requires asttoken):
    nameof(a.b, vars_only=False) # 'a.b'
    nameof(x[0], vars_only=False) # 'x[0]'
    
  • Add argument frame to argname, so that it can be wrapped.
    def argname2(arg, *more_args):
        return argname(arg, *more_args, frame=2)
    
  • Allow argname to fetch the source of variable keyword arguments (**kwargs), which will be an empty dict ({}) when no keyword arguments passed.
    def func(a, **kwargs):
        return argname(a, kwargs)
    # before:
    func(x) # raises error
    # after:
    func(x) # returns ('x', {})
    
  • Add argument pos_only to argname to only match the positional arguments
    # before
    def func(a, b=1):
      return argname(a)
    func(x) # 'x'
    func(x, b=2) # error since 2 is not ast.Name
    
    # after
    def func(a, b=1):
      return argname(a, pos_only=True)
    func(x) # 'x'
    func(x, b=2) # 'x'
    
  • Parse the arguments only if needed
    # before
    def func(a, b):
      return argname(a)
    func(x, 1) # NonVariableArgumentError
    
    # after
    func(x, 1) # 'x'
    
  • Allow variable positional arguments for argname so that argname(*args) is allowed
    # before
    def func(arg, *args):
      return argname(arg, args) # *args not allowed
    x = y = 1
    func(x, y) # ('x', ('y', 1))
    
    # after
    def func(arg, *args):
      return argname(arg, *args)
    x = y = 1
    func(x, y) # ('x', 'y')
    
  • Add vars_only (defaults to False) argument to helpers.debug so source of expression becomes available
    a=1
    debug(a+a) # DEBUG: a+a=2
    
python-varname -

Published by pwwang over 3 years ago

  • Add argname to retrieve argument names/sources passed to a function
python-varname -

Published by pwwang over 3 years ago

  • Changed:
    • Wrapper, register and debug moved to varname.helpers
    • Argument caller changed to frame across all APIs
    • ignore accepting module, filename, function, (function, num_decorators), (module, qualname) and (filename, qualname)
  • Removed:
    • inject (Use helpers.regiester instead)
    • inject_varname (Use helpers.regiester instead)
    • namedtuple
  • Added:
    • Arguments frame and ignore to Wrapper
    • helpers.register as a decorator for functions
python-varname -

Published by pwwang almost 4 years ago

  • Add ignore argument to varname to ignore frames that will be ignored by caller
  • Deprecate inject_varname, use register instead
python-varname -

Published by pwwang almost 4 years ago

  • Deprecate inject and use inject_varname to decorate classes instead.
python-varname -

Published by pwwang almost 4 years ago

  • Allow varname.varname to receive multiple variables on the left-hand side
python-varname -

Published by pwwang almost 4 years ago

  • Add debug function
  • Deprecate namedtuple (will be removed in 0.6.0)
python-varname -

Published by pwwang almost 4 years ago

  • Move messaging of weird nameof calls from _bytecode_nameof to nameof.
  • Disallow full to be used when _bytecode_nameof needs to be invoked.
python-varname -

Published by pwwang almost 4 years ago

  • Add better messaging for weird nameof calls
python-varname -

Published by pwwang almost 4 years ago

  • Allow nameof to retrieve full name of chained attribute calls
  • Add __all__ to the module so that only desired APIs are exposed when from varname import *
  • Give more hints on nameof being called in a weird way when no soucecode available.