Python promise.is_thenable() Examples

The following are 11 code examples of promise.is_thenable(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module promise , or try the search function .
Example #1
Source File: test_decorators.py    From django-graphql-social-auth with MIT License 5 votes vote down vote up
def test_social_auth_thenable(self, *args):

        @decorators.social_auth
        def wrapped(cls, root, info, provider, *args):
            return Promise()

        result = wrapped(TestCase, None, self.info(), 'google-oauth2', 'token')

        self.assertTrue(is_thenable(result)) 
Example #2
Source File: decorators.py    From django-graphql-social-auth with MIT License 5 votes vote down vote up
def social_auth(f):
    @psa
    @wraps(f)
    def wrapper(cls, root, info, social, **kwargs):
        def on_resolve(payload):
            payload.social = social
            return payload

        result = f(cls, root, info, social, **kwargs)

        if is_thenable(result):
            return Promise.resolve(result).then(on_resolve)
        return on_resolve(result)
    return wrapper 
Example #3
Source File: __init__.py    From graphene with MIT License 5 votes vote down vote up
def execute(self, *args, **kwargs):
        executed = self.schema.execute(*args, **dict(self.execute_options, **kwargs))
        if is_thenable(executed):
            return Promise.resolve(executed).then(self.format_result)

        return self.format_result(executed) 
Example #4
Source File: fields.py    From graphene-sqlalchemy with MIT License 5 votes vote down vote up
def connection_resolver(cls, resolver, connection_type, model, root, info, **args):
        resolved = resolver(root, info, **args)

        on_resolve = partial(cls.resolve_connection, connection_type, model, info, args)
        if is_thenable(resolved):
            return Promise.resolve(resolved).then(on_resolve)

        return on_resolve(resolved) 
Example #5
Source File: test_awaitable_35.py    From promise with MIT License 5 votes vote down vote up
def test_coroutine_is_thenable():
    async def my_coroutine():
        await sleep(.01)
        return True

    assert is_thenable(my_coroutine()) 
Example #6
Source File: test_benchmark.py    From promise with MIT License 5 votes vote down vote up
def test_benchmark_is_thenable_basic_type(benchmark):
    def create_promise():
        return is_thenable(True)

    result = benchmark(create_promise)
    assert result == False 
Example #7
Source File: test_benchmark.py    From promise with MIT License 5 votes vote down vote up
def test_benchmark_is_thenable_custom_type(benchmark):
    class MyType(object):
        pass

    my_type_instance = MyType()

    def create_promise():
        return is_thenable(my_type_instance)

    result = benchmark(create_promise)
    assert result == False 
Example #8
Source File: executor.py    From graphql-core-legacy with MIT License 5 votes vote down vote up
def execute_fields(
    exe_context,  # type: ExecutionContext
    parent_type,  # type: GraphQLObjectType
    source_value,  # type: Any
    fields,  # type: DefaultOrderedDict
    path,  # type: List[Union[int, str]]
    info,  # type: Optional[ResolveInfo]
):
    # type: (...) -> Union[Dict, Promise[Dict]]
    contains_promise = False

    final_results = OrderedDict()

    for response_name, field_asts in fields.items():
        result = resolve_field(
            exe_context,
            parent_type,
            source_value,
            field_asts,
            info,
            path + [response_name],
        )
        if result is Undefined:
            continue

        final_results[response_name] = result
        if is_thenable(result):
            contains_promise = True

    if not contains_promise:
        return final_results

    return promise_for_dict(final_results) 
Example #9
Source File: executor.py    From graphql-core-legacy with MIT License 5 votes vote down vote up
def complete_value_catching_error(
    exe_context,  # type: ExecutionContext
    return_type,  # type: Any
    field_asts,  # type: List[Field]
    info,  # type: ResolveInfo
    path,  # type: List[Union[int, str]]
    result,  # type: Any
):
    # type: (...) -> Any
    # If the field type is non-nullable, then it is resolved without any
    # protection from errors.
    if isinstance(return_type, GraphQLNonNull):
        return complete_value(exe_context, return_type, field_asts, info, path, result)

    # Otherwise, error protection is applied, logging the error and
    # resolving a null value for this field if one is encountered.
    try:
        completed = complete_value(
            exe_context, return_type, field_asts, info, path, result
        )
        if is_thenable(completed):

            def handle_error(error):
                # type: (Union[GraphQLError, GraphQLLocatedError]) -> Optional[Any]
                traceback = completed._traceback  # type: ignore
                exe_context.report_error(error, traceback)
                return None

            return completed.catch(handle_error)

        return completed
    except Exception as e:
        traceback = sys.exc_info()[2]
        exe_context.report_error(e, traceback)
        return None 
Example #10
Source File: executor.py    From graphql-core-legacy with MIT License 5 votes vote down vote up
def complete_list_value(
    exe_context,  # type: ExecutionContext
    return_type,  # type: GraphQLList
    field_asts,  # type: List[Field]
    info,  # type: ResolveInfo
    path,  # type: List[Union[int, str]]
    result,  # type: Any
):
    # type: (...) -> List[Any]
    """
    Complete a list value by completing each item in the list with the inner type
    """
    assert isinstance(result, Iterable), (
        "User Error: expected iterable, but did not find one " + "for field {}.{}."
    ).format(info.parent_type, info.field_name)

    item_type = return_type.of_type
    completed_results = []
    contains_promise = False

    index = 0
    for item in result:
        completed_item = complete_value_catching_error(
            exe_context, item_type, field_asts, info, path + [index], item
        )
        if not contains_promise and is_thenable(completed_item):
            contains_promise = True

        completed_results.append(completed_item)
        index += 1

    return (
        Promise.all(completed_results)  # type: ignore
        if contains_promise
        else completed_results
    ) 
Example #11
Source File: executor.py    From graphql-core-legacy with MIT License 4 votes vote down vote up
def execute_fields_serially(
    exe_context,  # type: ExecutionContext
    parent_type,  # type: GraphQLObjectType
    source_value,  # type: Any
    path,  # type: List
    fields,  # type: DefaultOrderedDict
):
    # type: (...) -> Promise
    def execute_field_callback(results, response_name):
        # type: (Dict, str) -> Union[Dict, Promise[Dict]]
        field_asts = fields[response_name]
        result = resolve_field(
            exe_context,
            parent_type,
            source_value,
            field_asts,
            None,
            path + [response_name],
        )
        if result is Undefined:
            return results

        if is_thenable(result):

            def collect_result(resolved_result):
                # type: (Dict) -> Dict
                results[response_name] = resolved_result
                return results

            return result.then(collect_result, None)

        results[response_name] = result
        return results

    def execute_field(prev_promise, response_name):
        # type: (Promise, str) -> Promise
        return prev_promise.then(
            lambda results: execute_field_callback(results, response_name)
        )

    return functools.reduce(
        execute_field, fields.keys(), Promise.resolve(collections.OrderedDict())
    )