Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
@staticmethod
@dumped()
def method3(c1):
self_.assertEqual(c1['x'], 'c1')
return C1('res3')
self_ = self
res1 = C2().method1(C1('c1'))
res2 = C2().method2(C1('c1'))
res3 = C2().method3(C1('c1'))
self.assertEqual(res1['x'], 'res1')
self.assertEqual(res2['x'], 'res2')
self.assertEqual(res3['x'], 'res3')
with self.assertRaises(InvalidDecorationError):
class Clazz:
@dumped()
@staticmethod
def method4(c1):
pass # This won't work; you need to swap the decorators.
def _validate_decoration(decorated, fork_inst):
if isinstance(decorated, staticmethod):
fork_inst._warn('You cannot decorate a static- or classmethod. '
'You can still obtain the desired behavior by '
'decorating your method first and then place '
'@staticmethod/@classmethod on top (switching the '
'order).')
raise InvalidDecorationError(
'Cannot decorate a static- or classmethod.')
if isinstance(decorated, type):
raise InvalidDecorationError('Cannot decorate a class.')
def _validate_decoration(decorated, fork_inst):
if isinstance(decorated, staticmethod):
fork_inst._warn('You cannot decorate a static- or classmethod. '
'You can still obtain the desired behavior by '
'decorating your method first and then place '
'@staticmethod/@classmethod on top (switching the '
'order).')
raise InvalidDecorationError(
'Cannot decorate a static- or classmethod.')
if isinstance(decorated, type):
raise InvalidDecorationError('Cannot decorate a class.')
>>> type(res).__name__
'datetime'
:param parameters: determines whether parameters should be taken into
account.
:param returnvalue: determines whether the return value should be taken
into account.
:param fork_inst: if given, it uses this fork of ``JsonSerializable``.
:param kwargs: any keyword arguments that should be passed on to
`jsons.load`
:param loader: the load function which must be one of (``load``,
``loads``, ``loadb``)
:return: a decorator that can be placed on a function.
"""
if loader not in (load, loads, loadb):
raise InvalidDecorationError("The 'loader' argument must be one of: "
"jsons.load, jsons.loads, jsons.loadb")
return _get_decorator(parameters, returnvalue, fork_inst, loader, kwargs)
>>> type(res).__name__
'str'
:param parameters: determines whether parameters should be taken into
account.
:param returnvalue: determines whether the return value should be taken
into account.
:param fork_inst: if given, it uses this fork of ``JsonSerializable``.
:param kwargs: any keyword arguments that should be passed on to
`jsons.dump`
:param dumper: the dump function which must be one of (``dump``,
``dumps``, ``dumpb``)
:return: a decorator that can be placed on a function.
"""
if dumper not in (dump, dumps, dumpb):
raise InvalidDecorationError("The 'dumper' argument must be one of: "
"jsons.dump, jsons.dumps, jsons.dumpb")
return _get_decorator(parameters, returnvalue, fork_inst, dumper, kwargs)