Skip to content

Commit

Permalink
Added functools.wrap for decorators (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
gaogaotiantian authored Mar 15, 2022
1 parent b4808f0 commit dffa62c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/objprint/decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
# For details: https://github.com/gaogaotiantian/objprint/blob/master/NOTICE.txt


import functools


def add_objprint(orig_class=None, format="string", **kwargs):

from . import _objprint
Expand All @@ -19,9 +22,9 @@ def __str__(self):

if orig_class is None:
def wrapper(cls):
cls.__str__ = __str__
cls.__str__ = functools.wraps(cls.__str__)(__str__)
return cls
return wrapper
else:
orig_class.__str__ = __str__
orig_class.__str__ = functools.wraps(orig_class.__str__)(__str__)
return orig_class
6 changes: 6 additions & 0 deletions tests/test_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ def test_wrapper(self):

self.assertEqual(output, expected)

def test_signature(self):
obj = DecoratedClass()
self.assertIn("DecoratedClass", str(obj.__str__))
obj = TestWrapper()
self.assertIn("TestWrapper", str(obj.__str__))

def test_json(self):
with io.StringIO() as buf, redirect_stdout(buf):
print(DecoratedJsonCls())
Expand Down

0 comments on commit dffa62c

Please sign in to comment.