Skip to content

Support multiple variables on LHS for varname.varname #28

@EngHabu

Description

@EngHabu

The code in here

python-varname/varname.py

Lines 243 to 266 in 6a1e8bd

ret = []
for arg in node.args:
if not full or isinstance(arg, ast.Name):
ret.append(_node_name(arg))
else:
# traverse the node to get the full name: nameof(a.b.c)
# arg:
# Attribute(value=Attribute(value=Name(id='a', ctx=Load()),
# attr='b',
# ctx=Load()),
# attr='c',
# ctx=Load())
full_name = []
while not isinstance(arg, ast.Name):
if not isinstance(arg, ast.Attribute):
raise VarnameRetrievingError(
'Can only retrieve full names of '
'(chained) attribute calls by nameof.'
)
full_name.append(arg.attr)
arg = arg.value
# now it is an ast.Name
full_name.append(arg.id)
ret.append('.'.join(reversed(full_name)))

assumes the Node is a function call. There are other AST Node types. For example, the code below:

    class MyType(object):
        def __eq__(self, other):
            print(varname.nameof(self, caller=2))
            return super().__eq__(other)

    a = MyType()
    print(a == 3)

Throws an exception because The Node in this case is a Compare Node that doesn't have args. It does, however, have n.left.id to access the variable name we are calling __eq__() on.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions