def _validate_directive_implementation(self) -> List[str]: """ Validates that defined directives provide a proper implementation. :return: a list of errors :rtype: List[str] """ errors = [] for directive in self._directive_definitions.values(): for expected in _IMPLEMENTABLE_DIRECTIVE_FUNCTION_HOOKS: attr = getattr(directive.implementation, expected, None) if attr and not is_valid_coroutine(attr): errors.append( f"Directive {directive.name} Method " f"{expected} is not awaitable." ) for expected in _IMPLEMENTABLE_DIRECTIVE_GENERATOR_HOOKS: attr = getattr(directive.implementation, expected, None) if attr and not is_valid_async_generator(attr): errors.append( f"Directive {directive.name} Method " f"{expected} is not an Async Generator." ) return errors
def get_callables(implementation: Any) -> Dict[str, Callable]: """ Computes a dictionary of all attribute name that starts with `on_` and are linked to a callable. :param implementation: the implementation to parse :type implementation: Any :return: a dictionary of attribute name and callable :rtype: Dict[str, Callable] """ return { key: getattr(implementation, key) for key in dir(implementation) if key.startswith("on_") and ( is_valid_coroutine(getattr(implementation, key)) or is_valid_async_generator(getattr(implementation, key))) }
def __call__(self, implementation: Callable) -> Callable: """ Registers the subscription generator into the schema. :param implementation: implementation of the subscription generator :type implementation: Callable :return: the implementation of the subscription generator :rtype: Callable """ if not is_valid_async_generator(implementation): raise NonAsyncGeneratorSubscription( "The subscription < {} > given is not an awaitable " "generator.".format(repr(implementation))) SchemaRegistry.register_subscription(self._schema_name, self) self._implementation = implementation return implementation
def test_is_valid_async_generator(obj, expected): assert is_valid_async_generator(obj) == expected