minor tweaks

This commit is contained in:
Kacppian 2018-07-09 14:44:30 +05:30
parent c2e0a41617
commit e88b050694
18 changed files with 78 additions and 92 deletions

View File

@ -45,13 +45,9 @@ def get_complete_version(version=None):
from graphene import VERSION as version
else:
if len(version) is not 5:
raise AssertionError(
"Version needs to be 5"
)
if version[3] not in ('alpha', 'beta', 'rc', 'final'):
raise AssertionError(
"Release version is unkown"
)
raise AssertionError("Version needs to be 5")
if version[3] not in ("alpha", "beta", "rc", "final"):
raise AssertionError("Release version is unkown")
return version

View File

@ -48,14 +48,14 @@ class Connection(ObjectType):
_meta = ConnectionOptions(cls)
if not node:
raise AssertionError(
"You have to provide a node in {}.Meta"
.format(cls.__name__)
"You have to provide a node in {}.Meta".format(cls.__name__)
)
if not issubclass(node, (Scalar, Enum, ObjectType, Interface, Union, NonNull)):
raise AssertionError(
'Received incompatible node "{}" for Connection {}.'
.format(node, cls.__name__)
'Received incompatible node "{}" for Connection {}.'.format(
node, cls.__name__
)
)
base_name = re.sub("Connection$", "", name or cls.__name__) or node._meta.name
@ -114,8 +114,9 @@ class IterableConnectionField(Field):
if not issubclass(connection_type, Connection):
raise AssertionError(
'{} type have to be a subclass of Connection. Received "{}".'
.format(self.__class__.__name__, connection_type)
'{} type have to be a subclass of Connection. Received "{}".'.format(
self.__class__.__name__, connection_type
)
)
return type
@ -126,11 +127,12 @@ class IterableConnectionField(Field):
if not isinstance(resolved, Iterable):
raise AssertionError(
'''
"""
Resolved value from the connection field have to be iterable or instance of {}.
Received "{}"
'''
.format(connection_type, resolved)
""".format(
connection_type, resolved
)
)
connection = connection_from_list(
resolved,

View File

@ -47,8 +47,9 @@ class ClientIDMutation(Mutation):
if cls.mutate and cls.mutate.__func__ == ClientIDMutation.mutate.__func__:
if not mutate_and_get_payload:
raise AssertionError(
"{name}.mutate_and_get_payload method is required in a ClientIDMutation."
.format(name=name or cls.__name__)
"{name}.mutate_and_get_payload method is required in a ClientIDMutation.".format(
name=name or cls.__name__
)
)
if not name:

View File

@ -48,11 +48,9 @@ class GlobalID(Field):
class NodeField(Field):
def __init__(self, node, type=False, deprecation_reason=None,
name=None, **kwargs):
def __init__(self, node, type=False, deprecation_reason=None, name=None, **kwargs):
if not issubclass(node, Node):
raise AssertionError('NodeField can only operate in Nodes')
raise AssertionError("NodeField can only operate in Nodes")
self.node_type = node
self.field_type = type
@ -103,8 +101,7 @@ class Node(AbstractNode):
if only_type:
if graphene_type != only_type:
raise AssertionError(
'Must receive a {} id.'
.format(only_type._meta.name)
"Must receive a {} id.".format(only_type._meta.name)
)
# We make sure the ObjectType implements the "Node" interface

View File

@ -75,8 +75,7 @@ def to_arguments(args, extra_args=None):
arg_name = default_name or arg.name
if arg_name in arguments:
raise AssertionError(
'More than one Argument have same name "{}".'
.format(arg_name)
'More than one Argument have same name "{}".'.format(arg_name)
)
arguments[arg_name] = arg

View File

@ -20,10 +20,7 @@ class Date(Scalar):
if isinstance(date, datetime.datetime):
date = date.date()
if not isinstance(date, datetime.date):
raise AssertionError(
'Received not compatible date "{}"'
.format(repr(date))
)
raise AssertionError('Received not compatible date "{}"'.format(repr(date)))
return date.isoformat()
@classmethod
@ -50,8 +47,7 @@ class DateTime(Scalar):
def serialize(dt):
if not isinstance(dt, (datetime.datetime, datetime.date)):
raise AssertionError(
'Received not compatible datetime "{}"'
.format(repr(dt))
'Received not compatible datetime "{}"'.format(repr(dt))
)
return dt.isoformat()
@ -78,10 +74,7 @@ class Time(Scalar):
@staticmethod
def serialize(time):
if not isinstance(time, datetime.time):
raise AssertionError(
'Received not compatible time "{}"'
.format(repr(time))
)
raise AssertionError('Received not compatible time "{}"'.format(repr(time)))
return time.isoformat()
@classmethod

View File

@ -14,7 +14,7 @@ class Dynamic(MountedType):
super(Dynamic, self).__init__(_creation_counter=_creation_counter)
if not (inspect.isfunction(type) or isinstance(type, partial)):
raise AssertionError(
'type is expected to be a function or an instance of partial'
"type is expected to be a function or an instance of partial"
)
self.type = type
self.with_schema = with_schema

View File

@ -37,17 +37,17 @@ class Field(MountedType):
if args and not isinstance(args, Mapping):
raise AssertionError(
'Arguments in a field have to be a mapping, received "{}".'
.format(args)
'Arguments in a field have to be a mapping, received "{}".'.format(args)
)
if source and resolver:
raise AssertionError(
'A Field cannot have a source and a resolver in at the same time.'
"A Field cannot have a source and a resolver in at the same time."
)
if callable(default_value):
raise AssertionError(
'The default value can not be a function but received "{}".'
.format(base_type(default_value))
'The default value can not be a function but received "{}".'.format(
base_type(default_value)
)
)
if required:

View File

@ -10,9 +10,8 @@ class MountedType(OrderedType):
"""
if not isinstance(unmounted, UnmountedType):
raise AssertionError(
"{} can't mount {}"
.format(cls.__name__, repr(unmounted))
)
"{} can't mount {}".format(cls.__name__, repr(unmounted))
)
return cls(
unmounted.get_type(),

View File

@ -49,11 +49,12 @@ class ObjectType(BaseType):
if possible_types and cls.is_type_of:
raise AssertionError(
'''
"""
{name}.Meta.possible_types will cause type collision with {name}.is_type_of.
Please use one or other.
'''
.format(name=cls.__name__)
""".format(
name=cls.__name__
)
)
if _meta.fields:

View File

@ -12,7 +12,7 @@ default_resolver = attr_resolver
def set_default_resolver(resolver):
global default_resolver
if not callable(resolver):
raise AssertionError('Received non-callable resolver.')
raise AssertionError("Received non-callable resolver.")
default_resolver = resolver

View File

@ -21,10 +21,7 @@ def assert_valid_root_type(_type):
is_graphene_objecttype = inspect.isclass(_type) and issubclass(_type, ObjectType)
is_graphql_objecttype = isinstance(_type, GraphQLObjectType)
if not (is_graphene_objecttype or is_graphql_objecttype):
raise AssertionError(
"Type {} is not a valid ObjectType."
.format(_type)
)
raise AssertionError("Type {} is not a valid ObjectType.".format(_type))
class Schema(GraphQLSchema):
@ -57,8 +54,9 @@ class Schema(GraphQLSchema):
if not all(isinstance(d, GraphQLDirective) for d in directives):
raise AssertionError(
'Schema directives must be List[GraphQLDirective] if provided but got: {}.'
.format(directives)
"Schema directives must be List[GraphQLDirective] if provided but got: {}.".format(
directives
)
)
self._directives = directives
self.build_typemap()
@ -81,10 +79,7 @@ class Schema(GraphQLSchema):
"""
_type = super(Schema, self).get_type(type_name)
if _type is None:
raise AttributeError(
'Type "{}" not found in the Schema'
.format(type_name)
)
raise AttributeError('Type "{}" not found in the Schema'.format(type_name))
if isinstance(_type, GrapheneGraphQLType):
return _type.graphene_type
return _type
@ -98,13 +93,13 @@ class Schema(GraphQLSchema):
graphql_type = self.get_type(_type._meta.name)
if not graphql_type:
raise AssertionError(
"Type {} not found in this schema."
.format(_type._meta.name)
"Type {} not found in this schema.".format(_type._meta.name)
)
if graphql_type.graphene_type != _type:
raise AssertionError(
'The type {} does not match with the associated graphene type {}.'
.format(_type, graphql_type.graphene_type)
"The type {} does not match with the associated graphene type {}.".format(
_type, graphql_type.graphene_type
)
)
return graphql_type
raise Exception("{} is not a valid GraphQL type.".format(_type))

View File

@ -7,7 +7,6 @@ from ..scalars import String
from ..schema import Schema
from ..structures import NonNull
from ..unmountedtype import UnmountedType
import re
class MyType(Interface):
@ -229,14 +228,15 @@ def test_objecttype_with_possible_types_and_is_type_of_should_raise():
def is_type_of(cls, root, context, info):
return False
assertion_message = '''
assertion_message = """
MyObjectType.Meta.possible_types will cause type collision with MyObjectType.is_type_of.
Please use one or other.
'''
"""
space_removed_excinfo = str(excinfo.value).replace(" ", "")
space_removed_assertion_message = assertion_message.replace(" ", "")
assert space_removed_assertion_message == space_removed_excinfo
def test_objecttype_no_fields_output():
class User(ObjectType):
name = String()

View File

@ -62,13 +62,13 @@ def resolve_type(resolve_type_func, map, type_name, root, info):
graphql_type = map.get(_type._meta.name)
if not graphql_type:
raise AssertionError(
"Can't find type {} in schema"
.format(_type._meta.name)
"Can't find type {} in schema".format(_type._meta.name)
)
if graphql_type.graphene_type != _type:
raise AssertionError(
'The type {} does not match with the associated graphene type {}.'
.format(_type, graphql_type.graphene_type)
"The type {} does not match with the associated graphene type {}.".format(
_type, graphql_type.graphene_type
)
)
return graphql_type
@ -102,8 +102,9 @@ class TypeMap(GraphQLTypeMap):
if isinstance(_type, GrapheneGraphQLType):
if _type.graphene_type is not type:
raise AssertionError(
'Found different types with the same name in the schema: {}, {}.'
.format(_type.graphene_type, type)
"Found different types with the same name in the schema: {}, {}.".format(
_type.graphene_type, type
)
)
return map
@ -183,8 +184,9 @@ class TypeMap(GraphQLTypeMap):
if isinstance(_type, GrapheneGraphQLType):
if _type.graphene_type != type:
raise AssertionError(
'Found different types with the same name in the schema: {}, {}.'
.format(_type.graphene_type, type)
"Found different types with the same name in the schema: {}, {}.".format(
_type.graphene_type, type
)
)
return _type
@ -195,8 +197,9 @@ class TypeMap(GraphQLTypeMap):
internal_type = map[interface._meta.name]
if internal_type.graphene_type != interface:
raise AssertionError(
'Found different types with the same name in the schema: {}, {}.'
.format(internal_type.graphene_type, interface)
"Found different types with the same name in the schema: {}, {}.".format(
internal_type.graphene_type, interface
)
)
interfaces.append(internal_type)
return interfaces
@ -223,8 +226,9 @@ class TypeMap(GraphQLTypeMap):
if isinstance(_type, GrapheneInterfaceType):
if _type.graphene_type != type:
raise AssertionError(
'Found different types with the same name in the schema: {}, {}.'
.format(_type.graphene_type, type)
"Found different types with the same name in the schema: {}, {}.".format(
_type.graphene_type, type
)
)
return _type
@ -266,8 +270,9 @@ class TypeMap(GraphQLTypeMap):
internal_type = map[objecttype._meta.name]
if internal_type.graphene_type != objecttype:
raise AssertionError(
"Found different types with the same name in the schema: {}, {}."
.format(internal_type.graphene_type, objecttype)
"Found different types with the same name in the schema: {}, {}.".format(
internal_type.graphene_type, objecttype
)
)
union_types.append(internal_type)
return union_types

View File

@ -25,8 +25,7 @@ class Union(UnmountedType, BaseType):
def __init_subclass_with_meta__(cls, types=None, **options):
if not (isinstance(types, (list, tuple)) and len(types) > 0):
raise AssertionError(
"Must provide types for Union {name}."
.format(name=cls.__name__)
"Must provide types for Union {name}.".format(name=cls.__name__)
)
_meta = UnionOptions(cls)
_meta.types = types

View File

@ -15,10 +15,7 @@ class UUID(Scalar):
if isinstance(uuid, str):
uuid = _UUID(uuid)
if not isinstance(uuid, _UUID):
raise AssertionError(
"Expected UUID instance, received {}"
.format(uuid)
)
raise AssertionError("Expected UUID instance, received {}".format(uuid))
return str(uuid)
@staticmethod

View File

@ -22,10 +22,11 @@ def annotate(_func=None, _trigger_warning=True, **annotations):
# We make sure the annotations are valid
for key, value in annotations.items():
if not key in func_signature.parameters:
if func_signature.parameters.get(key, None) is None:
raise AssertionError(
'The key {key} is not a function parameter in the function "{func_name}".'
.format(key=key, func_name=func_name(_func))
'The key {key} is not a function parameter in the function "{func_name}".'.format(
key=key, func_name=func_name(_func)
)
)
func_annotations = getattr(_func, "__annotations__", None)

View File

@ -44,11 +44,12 @@ class SubclassWithMeta(six.with_metaclass(SubclassWithMeta_Meta)):
if abstract:
if options:
raise AssertionError(
'''
"""
Abstract types can only contain the abstract attribute.
Received: abstract, {option_keys}
'''
.format(option_keys=', '.join(options.keys()))
""".format(
option_keys=", ".join(options.keys())
)
)
else:
super_class = super(cls, cls)