Esempio n. 1
0
    def _decorate_function(self, func, swagger_view_attrs):
        try:
            inspect.signature(func)
        except Exception:
            return

        name = func.__name__
        func: datafunction = datafunction(func)

        @functools.wraps(func)
        def wrapper(*args, **kwargs):
            return self.call_method(func, *args, **kwargs)

        self.dispatcher.add_method(wrapper)

        @dataclass
        class _Success:
            id: int
            jsonrpc: str

        class Success(class_schema(_Success)):
            result = func.return_schemas.schema_class._declared_fields[
                "_return"]

        Success.__name__ = f"{name}_success"

        self._add_view(
            swagger_view_attrs or {},
            func.params_schemas.schema_class,
            Success,
            self.path + name,
            type(self).__name__ + "_" + name,
            func.__doc__,
            method=name,
        )
def test_bound_method():
    class A:
        def foo(self, x: int) -> int:
            return x * 2

    method = A().foo
    decorated = datafunction(method)
    assert decorated("3") == 6
    def request(self, method_name, *args, **kwargs):
        method = getattr(self._original_methods, method_name)
        method = datafunction(method)

        # Here we use datafunction in reverse
        data = method.dump_arguments(*args, **kwargs)
        response = self.client.request(method_name, **data)
        return method.load_result(response.data.result)
def test_caching():
    def foo(x: int) -> int:
        return x

    assert datafunction(foo) is datafunction(foo)