def may_start_with_lowercase():
     result = camel_to_snake('CamelCase')
     assert result == 'camel_case'
Esempio n. 2
0
 def __getattribute__(self, name):
     py_name = camel_to_snake(name)
     return super().__getattribute__(py_name)
Esempio n. 3
0
 def resolver_shim(func, data, info, *args, **kwargs):
     kwargs = {camel_to_snake(k): v for k, v in kwargs.items()}
     return func(data, info, *args, **kwargs)
Esempio n. 4
0
    def __init_subclass__(cls, **kwargs):
        super().__init_subclass__(**kwargs)
        if not hasattr(cls, "mutate"):
            return
        if not isinstance(cls.__dict__["mutate"], (classmethod, staticmethod)):
            raise TypeError(
                f"{cls.__name__}.mutate must be a classmethod/staticmethod")
        mutate = getattr(cls, "mutate")
        type_name(f"{cls.__name__}Payload")(cls)
        types = get_type_hints(mutate,
                               localns={cls.__name__: cls},
                               include_extras=True)
        async_mutate = is_async(mutate, types)
        fields: List[Tuple[str, AnyType, Field]] = []
        cmi_param = None
        for param_name, param in signature(mutate).parameters.items():
            if param.kind is Parameter.POSITIONAL_ONLY:
                raise TypeError("Positional only parameters are not supported")
            if param.kind in {
                    Parameter.POSITIONAL_OR_KEYWORD, Parameter.KEYWORD_ONLY
            }:
                if param_name not in types:
                    raise TypeError("Mutation parameters must be typed")
                field_type = types[param_name]
                field_ = MISSING if param.default is Parameter.empty else param.default
                if is_union_of(field_type, ClientMutationId):
                    cmi_param = param_name
                    if cls._client_mutation_id is False:
                        if field_ is MISSING:
                            raise TypeError(
                                "Cannot have a ClientMutationId parameter"
                                " when _client_mutation_id = False")
                        continue
                    elif cls._client_mutation_id is True:
                        field_ = MISSING
                    field_ = field(default=field_,
                                   metadata=alias(CLIENT_MUTATION_ID))
                fields.append((param_name, field_type, field_))
        field_names = [name for (name, _, _) in fields]
        if cmi_param is None and cls._client_mutation_id is not False:
            fields.append((
                CLIENT_MUTATION_ID,
                ClientMutationId
                if cls._client_mutation_id else Optional[ClientMutationId],
                MISSING if cls._client_mutation_id else None,
            ))
            cmi_param = CLIENT_MUTATION_ID
        input_cls = make_dataclass(f"{cls.__name__}Input", fields)

        def wrapper(input):
            return mutate(
                **{name: getattr(input, name)
                   for name in field_names})

        wrapper.__annotations__["input"] = input_cls
        wrapper.__annotations__[
            "return"] = Awaitable[cls] if async_mutate else cls
        if cls._client_mutation_id is not False:
            cls.__annotations__[
                CLIENT_MUTATION_ID] = input_cls.__annotations__[cmi_param]
            setattr(cls, CLIENT_MUTATION_ID, field(init=False))
            wrapped = wrapper

            if async_mutate:

                async def wrapper(input):
                    result = await wrapped(input)
                    setattr(result, CLIENT_MUTATION_ID,
                            getattr(input, cmi_param))
                    return result

            else:

                def wrapper(input):
                    result = wrapped(input)
                    setattr(result, CLIENT_MUTATION_ID,
                            getattr(input, cmi_param))
                    return result

            wrapper = wraps(wrapped)(wrapper)

        cls._mutation = Mutation_(
            function=wrapper,
            alias=camel_to_snake(cls.__name__),
            schema=cls._schema,
            error_handler=cls._error_handler,
        )
Esempio n. 5
0
 def converts_typical_names():
     result = camel_to_snake("CamelCase")
     assert result == "camel_case"
     result = camel_to_snake("InputObjectTypeExtensionNode")
     assert result == "input_object_type_extension_node"
Esempio n. 6
0
 def keeps_already_snake():
     result = camel_to_snake("snake_case")
     assert result == "snake_case"
Esempio n. 7
0
 def works_with_acronyms():
     result = camel_to_snake("SlowXMLParser")
     assert result == "slow_xml_parser"
     result = camel_to_snake("FastGraphQLParser")
     assert result == "fast_graph_ql_parser"
Esempio n. 8
0
 def may_start_with_lowercase():
     result = camel_to_snake("CamelCase")
     assert result == "camel_case"