Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
@wrapt.decorator
def _decorator(wrapped, instance, args, kwargs):
return wrapped(*args, **kwargs)
class Class(object):
@_decorator
@classmethod
def function(cls, *args, **kwargs):
return args, kwargs
self.assertTrue(isinstance(function, wrapt.FunctionWrapper))
self.assertTrue(isinstance(function, wrapt.ObjectProxy))
instance = Class()
self.assertFalse(isinstance(instance.function, wrapt.FunctionWrapper))
self.assertTrue(isinstance(instance.function, wrapt.ObjectProxy))
def test_update_qualname_modified_on_original(self):
def function():
pass
def wrapper(wrapped, instance, args, kwargs):
return wrapped(*args, **kwargs)
instance = wrapt.FunctionWrapper(function, wrapper)
if six.PY3:
method = self.test_update_qualname_modified_on_original
self.assertEqual(instance.__qualname__,
(method.__qualname__ + '..function'))
instance.__qualname__ = 'override_qualname'
self.assertEqual(function.__qualname__, 'override_qualname')
self.assertEqual(instance.__qualname__, 'override_qualname')
def test_re_bind_after_none(self):
def function():
pass
def wrapper(wrapped, instance, args, kwargs):
return wrapped(*args, **kwargs)
_wrapper = wrapt.FunctionWrapper(function, wrapper)
self.assertTrue(isinstance(_wrapper, wrapt.FunctionWrapper))
instance = object()
_bound_wrapper_1 = _wrapper.__get__(None, type(instance))
self.assertTrue(_bound_wrapper_1._self_parent is _wrapper)
self.assertTrue(isinstance(_bound_wrapper_1,
wrapt.BoundFunctionWrapper))
self.assertEqual(_bound_wrapper_1._self_instance, None)
_bound_wrapper_2 = _bound_wrapper_1.__get__(instance, type(instance))
self.assertTrue(_bound_wrapper_2._self_parent is _wrapper)
def test_update_doc_modified_on_original(self):
def function():
"""documentation"""
pass
def wrapper(wrapped, instance, args, kwargs):
return wrapped(*args, **kwargs)
instance = wrapt.FunctionWrapper(function, wrapper)
self.assertEqual(instance.__doc__, "documentation")
instance.__doc__ = 'override_doc'
self.assertEqual(function.__doc__, 'override_doc')
self.assertEqual(instance.__doc__, 'override_doc')
def test_update_annotations_modified_on_original(self):
def function():
pass
def wrapper(wrapped, instance, args, kwargs):
return wrapped(*args, **kwargs)
instance = wrapt.FunctionWrapper(function, wrapper)
if six.PY3:
self.assertEqual(instance.__annotations__, {})
else:
def run(*args):
instance.__annotations__
self.assertRaises(AttributeError, run, ())
override_annotations = { 'override_annotations': '' }
instance.__annotations__ = override_annotations
self.assertEqual(function.__annotations__, override_annotations)
self.assertEqual(instance.__annotations__, override_annotations)
def test_unpatch(self):
gql = graphql.graphql
unpatch()
assert gql == graphql.graphql
assert not isinstance(graphql.graphql, FunctionWrapper)
patch()
assert isinstance(graphql.graphql, FunctionWrapper)
tracer, schema = get_traced_schema()
graphql.graphql(schema, '{ hello }')
span = tracer.writer.pop()[0]
unpatch()
assert gql == graphql.graphql
cb_args = {}
def test_cb(**kwargs):
cb_args.update(kwargs)
patch(span_callback=test_cb)
assert isinstance(graphql.graphql, FunctionWrapper)
def _inject(f):
return wrapt.FunctionWrapper(
wrapped=f,
wrapper=Injector(
container=container or get_global_container(),
blueprint=_generate_injection_blueprint(
func=f,
arg_map=arg_map,
use_names=use_names,
use_type_hints=use_type_hints
)
def apply_transformation(function, counter, data, output_dir, save_strategy, follow):
# A `function` can be a list of functions
if (isinstance(function, Iterable)
and not isinstance(function, wrapt.FunctionWrapper)):
for obj in function:
data = apply_transformation(obj, counter, data,
output_dir, save_strategy)
return data
else:
metadata = get_function_meta(function)
index = next(counter)
metadata.update(
index=index,
type="function start",
count=len(data),
)
logger.info(metadata)
print("Applying transformation {}".format(metadata['name']))
data = function(data)
logging.warning(
'%s: `%s.%s` (in file: %s) is deprecated and will be removed %s.\n'
'Instructions for updating: %s\n' % (
"Class" if inspect.isclass(wrapped) else "Function", wrapped.__module__, class_or_func_name,
wrapped.__code__.co_filename, 'in a future version' if date is None else
('after %s' % date), instructions
)
)
return wrapped(*args, **kwargs)
decorated = wrapper(wrapped)
if sys.version_info > (3, 0): # docstring can only be edited with Python 3
wrapt.FunctionWrapper.__setattr__(
decorated, "__doc__", add_deprecation_notice_to_docstring(wrapped.__doc__, date, instructions)
)
return decorated