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

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy