Exemplo n.º 1
0
def print_type(gtype, nonnull=True, except_types=()):
    if isinstance(gtype, except_types):
        raise ValidationError(f'{gtype} is not a valid type')
    literal = None
    if is_union(gtype):
        if is_optional(gtype):
            return f'{print_type(gtype.__args__[0], nonnull=False, except_types=except_types)}'  # noqa
        else:
            raise ValidationError(
                f'Native Union type is not supported except Optional')
    elif is_list(gtype):
        literal = f'[{print_type(gtype.__args__[0], except_types=except_types)}]'  # noqa
    elif isinstance(gtype, types.ObjectType):
        literal = f'{gtype.__name__}'
    elif gtype in VALID_BASIC_TYPES:
        literal = VALID_BASIC_TYPES[gtype]
    elif gtype is None or gtype == type(None):  # noqa
        return 'null'
    elif isinstance(gtype, types.UnionType):
        literal = f'{gtype.__name__}'
    elif isinstance(gtype, types.EnumType):
        literal = f'{gtype.__name__}'
    elif isinstance(gtype, types.InputType):
        literal = f'{gtype.__name__}'
    elif isinstance(gtype, types.InterfaceType):
        literal = f'{gtype.__name__}'
    else:
        raise ValidationError(f'Can not convert type {gtype} to GraphQL type')

    if nonnull:
        literal += '!'
    return literal
Exemplo n.º 2
0
    def register_types(cls, types):
        for ptype in types:
            if ptype in cls.validated_type:
                continue
            cls.validated_type.append(ptype)

            if isinstance(ptype, ObjectType):
                cls.registered_type.append(ptype)
                cls.register_fields_type(ptype.__fields__.values())
            elif is_union(ptype) or is_list(ptype):
                cls.register_types(ptype.__args__)
            elif isinstance(ptype, UnionType):
                cls.registered_type.append(ptype)
                cls.register_types(ptype.members)
            elif isinstance(ptype, InputType):
                cls.registered_type.append(ptype)
                cls.register_fields_type(ptype.__fields__.values())
            elif isinstance(ptype, InterfaceType):
                cls.registered_type.append(ptype)
                cls.register_fields_type(ptype.__fields__.values())
                cls.register_types(ptype.__subclasses__())
            elif isinstance(ptype, EnumType):
                cls.registered_type.append(ptype)
            else:
                # Other basic types, do not need be handled
                pass
Exemplo n.º 3
0
 def replace_forwarded_type(self, ptype):
     if hasattr(ptype, '__args__'):
         args = [self.replace_forwarded_type(t) for t in ptype.__args__]
         if is_union(ptype):
             return PyUnion[tuple(args)]
         elif is_list(ptype):
             return List[tuple(args)]
     elif isinstance(ptype, (str, ForwardRef)):
         return self.get_type(ptype)
     return ptype
Exemplo n.º 4
0
def load_variable(variable, ptype):
    if isinstance(ptype, types.InputType):
        data = {}
        keys = ptype.__dataclass_fields__.keys()
        for key, value in variable.items():
            snake_cases = to_snake_case(key)
            data[snake_cases if key not in keys else key] = load_variable(
                value, ptype.__fields__[snake_cases].ftype)
        return ptype(**data)
    elif is_list(ptype):
        return [load_variable(i, ptype.__args__[0]) for i in variable]
    elif is_optional(ptype):
        if variable is None:
            return None
        return load_variable(variable, ptype.__args__[0])
    elif isinstance(ptype, types.EnumType):
        return getattr(ptype, variable)
    else:
        return variable
Exemplo n.º 5
0
    def __check_return_type__(cls, return_type, result):
        if is_optional(return_type):
            if result is None:
                return True
            return cls.__check_return_type__(return_type.__args__[0], result)
        elif is_list(return_type):
            if len(result) == 0:
                return True
            for item in result:
                if not cls.__check_return_type__(return_type.__args__[0],
                                                 item):
                    return False
                return True
        elif isinstance(return_type, types.UnionType):
            for member in return_type.members:
                if cls.__check_return_type__(member, result):
                    return True
        elif isinstance(result, return_type):
            return True

        return False
Exemplo n.º 6
0
def test_is_list():
    assert is_list(typing.List[str]) is True
    assert is_list(typing.Union[str, int, None]) is False