Skip to content
Snippets Groups Projects
Commit d3267205 authored by Piotr Maślanka's avatar Piotr Maślanka
Browse files

add satella.coding.wraps

parent 8d493726
No related branches found
Tags v2.4.34
No related merge requests found
# v2.4.34
* _TBA_
* added `wraps`
# v2.4.33
......
......@@ -84,3 +84,9 @@ attach_arguments
----------------
.. autofunction:: satella.coding.attach_arguments
wraps
-----
A ``functools.wraps()`` equivalent, but for classes
.. autofunction:: satella.coding.wraps
__version__ = '2.4.34a1'
__version__ = '2.4.34'
......@@ -5,7 +5,7 @@ Just useful objects to make your coding nicer every day
from .algos import merge_dicts
from .concurrent import Monitor, RMonitor
from .decorators import precondition, for_argument, PreconditionError, short_none, has_keys, \
attach_arguments
attach_arguments, wraps
from .fun_static import static_var
from .metaclasses import metaclass_maker
from .recast_exceptions import rethrow_as, silence_excs, catch_exception
......@@ -16,5 +16,5 @@ __all__ = [
'precondition', 'PreconditionError', 'attach_arguments',
'rethrow_as', 'silence_excs',
'static_var', 'metaclass_maker',
'catch_exception',
'catch_exception', 'wraps',
]
......@@ -5,7 +5,7 @@ import itertools
from ..exceptions import PreconditionError
__all__ = ['precondition', 'for_argument', 'PreconditionError', 'short_none', 'has_keys',
'attach_arguments']
'attach_arguments', 'wraps']
def _NOP(x):
......@@ -16,6 +16,22 @@ def _TRUE(x):
return True
def wraps(cls_to_wrap: tp.Type) -> tp.Callable[[tp.Type], tp.Type]:
"""
A functools.wraps() but for classes
:param cls_to_wrap: class to wrap
:return:
"""
def outer(cls: tp.Type):
cls.__doc__ = cls_to_wrap.__doc__
cls.__name__ = cls_to_wrap.__name__
cls.__module__ = cls_to_wrap.__module__
return cls
return outer
def attach_arguments(*args, **kwargs):
"""
Return a decorator that passes extra arguments to the function.
......
import unittest
import typing as tp
from satella.coding import attach_arguments
from socket import socket
from satella.coding import attach_arguments, wraps
class TestDecorators(unittest.TestCase):
......@@ -10,3 +10,12 @@ class TestDecorators(unittest.TestCase):
self.assertEqual(kwargs, {'label': 2, 'value': 4})
test_me(value=4)
def test_wraps(self):
@wraps(socket)
class MySocket(socket):
pass
self.assertEqual(MySocket.__name__, socket.__name__)
self.assertEqual(MySocket.__doc__, socket.__doc__)
self.assertEqual(MySocket.__module__, socket.__module__)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment