Skip to content

How to write a wrapper around these functions? #111

@oliversheridanmethven

Description

@oliversheridanmethven

jsobj returns a dictionary. If I wanted to write a decorator which returns the .items() of whatever it is decorating (presumably a function which returns dictionaries), how would I do this, as my naive implementation didn't work. Can the functions in this module even be decorated in "the usual" manner?

My attempt:

from varname.helpers import jsobj
from functools import wraps
import unittest

def return_dict_items(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        return func(*args, **kwargs).items()
    return wrapper

class TestWrapper(unittest.TestCase):
    def test_wrapper(self):
        foo = return_dict_items(jsobj)
        a = 1
        self.assertEqual([("a", 1)], list(foo(a)))


if __name__ == '__main__':
    unittest.main()

However:

Expected :[('a', 1)]
Actual   :[(<ast.Starred object at 0x10b96f850>, 1)]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions