mirror of
https://github.com/graphql-python/graphene.git
synced 2024-11-10 19:56:45 +03:00
Fixed lint errors
This commit is contained in:
parent
7073208517
commit
a2ab008ead
|
@ -1,5 +1,5 @@
|
|||
#!/bin/bash
|
||||
|
||||
autoflake ./ -r --remove-unused-variables --remove-all-unused-imports --in-place
|
||||
autopep8 ./ -r --in-place
|
||||
autopep8 ./ -r --in-place --experimental --aggressive --max-line-length 120
|
||||
isort -rc .
|
||||
|
|
|
@ -40,7 +40,30 @@ from graphene.decorators import (
|
|||
resolve_only_args
|
||||
)
|
||||
|
||||
__all__ = ['Enum', 'Argument', 'String', 'Int', 'Boolean', 'Float', 'ID', 'List', 'NonNull', 'signals', 'Schema',
|
||||
'BaseType', 'LazyType', 'ObjectType', 'Interface', 'Mutation', 'Field', 'InputField', 'StringField',
|
||||
'IntField', 'BooleanField', 'IDField', 'ListField', 'NonNullField',
|
||||
'FloatField', 'resolve_only_args']
|
||||
__all__ = [
|
||||
'Enum',
|
||||
'Argument',
|
||||
'String',
|
||||
'Int',
|
||||
'Boolean',
|
||||
'Float',
|
||||
'ID',
|
||||
'List',
|
||||
'NonNull',
|
||||
'signals',
|
||||
'Schema',
|
||||
'BaseType',
|
||||
'LazyType',
|
||||
'ObjectType',
|
||||
'Interface',
|
||||
'Mutation',
|
||||
'Field',
|
||||
'InputField',
|
||||
'StringField',
|
||||
'IntField',
|
||||
'BooleanField',
|
||||
'IDField',
|
||||
'ListField',
|
||||
'NonNullField',
|
||||
'FloatField',
|
||||
'resolve_only_args']
|
||||
|
|
|
@ -16,7 +16,8 @@ except AttributeError:
|
|||
@singledispatch
|
||||
def convert_django_field(field):
|
||||
raise Exception(
|
||||
"Don't know how to convert the Django field %s (%s)" % (field, field.__class__))
|
||||
"Don't know how to convert the Django field %s (%s)" %
|
||||
(field, field.__class__))
|
||||
|
||||
|
||||
@convert_django_field.register(models.DateField)
|
||||
|
|
|
@ -32,6 +32,7 @@ class DjangoOptions(Options):
|
|||
return
|
||||
if not self.model:
|
||||
raise Exception(
|
||||
'Django ObjectType %s must have a model in the Meta class attr' % cls)
|
||||
'Django ObjectType %s must have a model in the Meta class attr' %
|
||||
cls)
|
||||
elif not inspect.isclass(self.model) or not issubclass(self.model, models.Model):
|
||||
raise Exception('Provided model in %s is not a Django model' % cls)
|
||||
|
|
|
@ -87,7 +87,8 @@ def test_should_node():
|
|||
article = graphene.Field(ArticleNode)
|
||||
|
||||
def resolve_reporter(self, *args, **kwargs):
|
||||
return ReporterNode(Reporter(id=1, first_name='ABA', last_name='X'))
|
||||
return ReporterNode(
|
||||
Reporter(id=1, first_name='ABA', last_name='X'))
|
||||
|
||||
query = '''
|
||||
query ReporterQuery {
|
||||
|
|
|
@ -46,11 +46,13 @@ class InstanceObjectType(BaseObjectType):
|
|||
return getattr(self.instance, attr)
|
||||
|
||||
|
||||
class DjangoObjectType(six.with_metaclass(DjangoObjectTypeMeta, InstanceObjectType)):
|
||||
class DjangoObjectType(six.with_metaclass(
|
||||
DjangoObjectTypeMeta, InstanceObjectType)):
|
||||
pass
|
||||
|
||||
|
||||
class DjangoInterface(six.with_metaclass(DjangoObjectTypeMeta, InstanceObjectType)):
|
||||
class DjangoInterface(six.with_metaclass(
|
||||
DjangoObjectTypeMeta, InstanceObjectType)):
|
||||
pass
|
||||
|
||||
|
||||
|
|
|
@ -28,11 +28,14 @@ class GraphQLView(View):
|
|||
errors = [{
|
||||
"message": str(e)
|
||||
} for e in errors]
|
||||
return HttpResponse(json.dumps({'errors': errors}), content_type='application/json')
|
||||
return HttpResponse(
|
||||
json.dumps({'errors': errors}),
|
||||
content_type='application/json')
|
||||
|
||||
def execute_query(self, request, query, *args, **kwargs):
|
||||
if not query:
|
||||
return self.response_errors(Exception("Must provide query string."))
|
||||
return self.response_errors(
|
||||
Exception("Must provide query string."))
|
||||
else:
|
||||
try:
|
||||
result = self.schema.execute(query, *args, **kwargs)
|
||||
|
@ -59,7 +62,8 @@ class GraphQLView(View):
|
|||
received_json_data = json.loads(request.body.decode())
|
||||
query = received_json_data.get('query')
|
||||
except ValueError:
|
||||
return self.response_errors(ValueError("Malformed json body in the post data"))
|
||||
return self.response_errors(ValueError(
|
||||
"Malformed json body in the post data"))
|
||||
else:
|
||||
query = request.POST.get('query') or request.GET.get('query')
|
||||
return self.execute_query(request, query or '')
|
||||
|
|
|
@ -52,7 +52,9 @@ class Options(object):
|
|||
# Any leftover attributes must be invalid.
|
||||
if meta_attrs != {}:
|
||||
raise TypeError(
|
||||
"'class Meta' got invalid attribute(s): %s" % ','.join(meta_attrs.keys()))
|
||||
"'class Meta' got invalid attribute(s): %s" %
|
||||
','.join(
|
||||
meta_attrs.keys()))
|
||||
else:
|
||||
self.proxy = False
|
||||
|
||||
|
|
|
@ -21,7 +21,8 @@ class GraphQLSchema(_GraphQLSchema):
|
|||
class Schema(object):
|
||||
_executor = None
|
||||
|
||||
def __init__(self, query=None, mutation=None, name='Schema', executor=None):
|
||||
def __init__(self, query=None, mutation=None,
|
||||
name='Schema', executor=None):
|
||||
self._types_names = {}
|
||||
self._types = {}
|
||||
self.mutation = mutation
|
||||
|
@ -36,7 +37,9 @@ class Schema(object):
|
|||
def T(self, object_type):
|
||||
if not object_type:
|
||||
return
|
||||
if inspect.isclass(object_type) and issubclass(object_type, BaseType) or isinstance(object_type, BaseType):
|
||||
if inspect.isclass(object_type) and issubclass(
|
||||
object_type, BaseType) or isinstance(
|
||||
object_type, BaseType):
|
||||
if object_type not in self._types:
|
||||
internal_type = object_type.internal_type(self)
|
||||
self._types[object_type] = internal_type
|
||||
|
@ -63,7 +66,9 @@ class Schema(object):
|
|||
def schema(self):
|
||||
if not self.query:
|
||||
raise Exception('You have to define a base query type')
|
||||
return GraphQLSchema(self, query=self.T(self.query), mutation=self.T(self.mutation))
|
||||
return GraphQLSchema(
|
||||
self, query=self.T(self.query),
|
||||
mutation=self.T(self.mutation))
|
||||
|
||||
def register(self, object_type):
|
||||
type_name = object_type._meta.type_name
|
||||
|
@ -78,7 +83,8 @@ class Schema(object):
|
|||
name = getattr(type, 'name', None)
|
||||
if name:
|
||||
objecttype = self._types_names.get(name, None)
|
||||
if objecttype and inspect.isclass(objecttype) and issubclass(objecttype, BaseObjectType):
|
||||
if objecttype and inspect.isclass(
|
||||
objecttype) and issubclass(objecttype, BaseObjectType):
|
||||
return objecttype
|
||||
|
||||
def setup(self):
|
||||
|
@ -95,7 +101,8 @@ class Schema(object):
|
|||
def types(self):
|
||||
return self._types_names
|
||||
|
||||
def execute(self, request='', root=None, vars=None, operation_name=None, **kwargs):
|
||||
def execute(self, request='', root=None, vars=None,
|
||||
operation_name=None, **kwargs):
|
||||
root = root or object()
|
||||
return self.executor.execute(
|
||||
self.schema,
|
||||
|
|
|
@ -92,9 +92,9 @@ def test_query_schema_execute():
|
|||
def test_schema_get_type_map():
|
||||
assert_equal_lists(
|
||||
schema.schema.get_type_map().keys(),
|
||||
['__Field', 'String', 'Pet', 'Character', '__InputValue', '__Directive',
|
||||
'__TypeKind', '__Schema', '__Type', 'Human', '__EnumValue', 'Boolean']
|
||||
)
|
||||
['__Field', 'String', 'Pet', 'Character', '__InputValue',
|
||||
'__Directive', '__TypeKind', '__Schema', '__Type', 'Human',
|
||||
'__EnumValue', 'Boolean'])
|
||||
|
||||
|
||||
def test_schema_no_query():
|
||||
|
|
|
@ -5,5 +5,26 @@ from .objecttype import ObjectTypeMeta, BaseObjectType, Interface, ObjectType, M
|
|||
from .scalars import String, ID, Boolean, Int, Float, Scalar
|
||||
from .field import Field, InputField
|
||||
|
||||
__all__ = ['BaseType', 'LazyType', 'OrderedType', 'Argument', 'ArgumentsGroup', 'to_arguments', 'List', 'NonNull', 'Field', 'InputField',
|
||||
'Interface', 'BaseObjectType', 'ObjectTypeMeta', 'ObjectType', 'Mutation', 'InputObjectType', 'String', 'ID', 'Boolean', 'Int', 'Float', 'Scalar']
|
||||
__all__ = [
|
||||
'BaseType',
|
||||
'LazyType',
|
||||
'OrderedType',
|
||||
'Argument',
|
||||
'ArgumentsGroup',
|
||||
'to_arguments',
|
||||
'List',
|
||||
'NonNull',
|
||||
'Field',
|
||||
'InputField',
|
||||
'Interface',
|
||||
'BaseObjectType',
|
||||
'ObjectTypeMeta',
|
||||
'ObjectType',
|
||||
'Mutation',
|
||||
'InputObjectType',
|
||||
'String',
|
||||
'ID',
|
||||
'Boolean',
|
||||
'Int',
|
||||
'Float',
|
||||
'Scalar']
|
||||
|
|
|
@ -9,7 +9,8 @@ from .base import ArgumentType, BaseType, OrderedType
|
|||
|
||||
class Argument(OrderedType):
|
||||
|
||||
def __init__(self, type, description=None, default=None, name=None, _creation_counter=None):
|
||||
def __init__(self, type, description=None, default=None,
|
||||
name=None, _creation_counter=None):
|
||||
super(Argument, self).__init__(_creation_counter=_creation_counter)
|
||||
self.name = name
|
||||
self.type = type
|
||||
|
@ -17,7 +18,9 @@ class Argument(OrderedType):
|
|||
self.default = default
|
||||
|
||||
def internal_type(self, schema):
|
||||
return GraphQLArgument(schema.T(self.type), self.default, self.description)
|
||||
return GraphQLArgument(
|
||||
schema.T(self.type),
|
||||
self.default, self.description)
|
||||
|
||||
def __repr__(self):
|
||||
return self.name
|
||||
|
@ -30,7 +33,8 @@ class ArgumentsGroup(BaseType):
|
|||
self.arguments = OrderedDict([(arg.name, arg) for arg in arguments])
|
||||
|
||||
def internal_type(self, schema):
|
||||
return OrderedDict([(arg.name, schema.T(arg)) for arg in self.arguments.values()])
|
||||
return OrderedDict([(arg.name, schema.T(arg))
|
||||
for arg in self.arguments.values()])
|
||||
|
||||
def __len__(self):
|
||||
return len(self.arguments)
|
||||
|
|
|
@ -54,7 +54,7 @@ class OrderedType(MountType):
|
|||
|
||||
def __eq__(self, other):
|
||||
# Needed for @total_ordering
|
||||
if type(self) == type(other):
|
||||
if isinstance(self, type(other)):
|
||||
return self.creation_counter == other.creation_counter
|
||||
return NotImplemented
|
||||
|
||||
|
@ -87,7 +87,8 @@ class ArgumentType(MirroredType):
|
|||
|
||||
def as_argument(self):
|
||||
from .argument import Argument
|
||||
return Argument(self, _creation_counter=self.creation_counter, *self.args, **self.kwargs)
|
||||
return Argument(
|
||||
self, _creation_counter=self.creation_counter, *self.args, **self.kwargs)
|
||||
|
||||
|
||||
class FieldType(MirroredType):
|
||||
|
@ -103,11 +104,13 @@ class FieldType(MirroredType):
|
|||
|
||||
def as_field(self):
|
||||
from .field import Field
|
||||
return Field(self, _creation_counter=self.creation_counter, *self.args, **self.kwargs)
|
||||
return Field(self, _creation_counter=self.creation_counter,
|
||||
*self.args, **self.kwargs)
|
||||
|
||||
def as_inputfield(self):
|
||||
from .field import InputField
|
||||
return InputField(self, _creation_counter=self.creation_counter, *self.args, **self.kwargs)
|
||||
return InputField(
|
||||
self, _creation_counter=self.creation_counter, *self.args, **self.kwargs)
|
||||
|
||||
|
||||
class MountedType(FieldType, ArgumentType):
|
||||
|
|
|
@ -26,7 +26,9 @@ class Empty(object):
|
|||
|
||||
class Field(OrderedType):
|
||||
|
||||
def __init__(self, type, description=None, args=None, name=None, resolver=None, required=False, default=None, *args_list, **kwargs):
|
||||
def __init__(
|
||||
self, type, description=None, args=None, name=None, resolver=None,
|
||||
required=False, default=None, *args_list, **kwargs):
|
||||
_creation_counter = kwargs.pop('_creation_counter', None)
|
||||
super(Field, self).__init__(_creation_counter=_creation_counter)
|
||||
self.name = name
|
||||
|
@ -43,7 +45,8 @@ class Field(OrderedType):
|
|||
|
||||
def contribute_to_class(self, cls, attname):
|
||||
assert issubclass(
|
||||
cls, BaseObjectType), 'Field {} cannot be mounted in {}'.format(self, cls)
|
||||
cls, BaseObjectType), 'Field {} cannot be mounted in {}'.format(
|
||||
self, cls)
|
||||
if not self.name:
|
||||
self.name = to_camel_case(attname)
|
||||
self.attname = attname
|
||||
|
@ -109,7 +112,7 @@ class Field(OrderedType):
|
|||
|
||||
def __eq__(self, other):
|
||||
eq = super(Field, self).__eq__(other)
|
||||
if type(self) == type(other):
|
||||
if isinstance(self, type(other)):
|
||||
return eq and self.object_type == other.object_type
|
||||
return NotImplemented
|
||||
|
||||
|
@ -119,7 +122,8 @@ class Field(OrderedType):
|
|||
|
||||
class InputField(OrderedType):
|
||||
|
||||
def __init__(self, type, description=None, default=None, name=None, _creation_counter=None, required=False):
|
||||
def __init__(self, type, description=None, default=None,
|
||||
name=None, _creation_counter=None, required=False):
|
||||
super(InputField, self).__init__(_creation_counter=_creation_counter)
|
||||
self.name = name
|
||||
if required:
|
||||
|
@ -130,7 +134,8 @@ class InputField(OrderedType):
|
|||
|
||||
def contribute_to_class(self, cls, attname):
|
||||
assert issubclass(
|
||||
cls, InputObjectType), 'InputField {} cannot be mounted in {}'.format(self, cls)
|
||||
cls, InputObjectType), 'InputField {} cannot be mounted in {}'.format(
|
||||
self, cls)
|
||||
if not self.name:
|
||||
self.name = to_camel_case(attname)
|
||||
self.attname = attname
|
||||
|
@ -141,5 +146,6 @@ class InputField(OrderedType):
|
|||
cls._meta.add_field(self)
|
||||
|
||||
def internal_type(self, schema):
|
||||
return GraphQLInputObjectField(schema.T(self.type), default_value=self.default,
|
||||
description=self.description)
|
||||
return GraphQLInputObjectField(
|
||||
schema.T(self.type),
|
||||
default_value=self.default, description=self.description)
|
||||
|
|
|
@ -79,7 +79,8 @@ class ObjectTypeMeta(type):
|
|||
# on the base classes (we cannot handle shadowed fields at the
|
||||
# moment).
|
||||
for field in parent_fields:
|
||||
if field.name in field_names and field.type.__class__ != field_names[field.name].type.__class__:
|
||||
if field.name in field_names and field.type.__class__ != field_names[
|
||||
field.name].type.__class__:
|
||||
raise Exception(
|
||||
'Local field %r in class %r (%r) clashes '
|
||||
'with field with similar name from '
|
||||
|
@ -111,7 +112,8 @@ class ObjectTypeMeta(type):
|
|||
|
||||
def add_to_class(cls, name, value):
|
||||
# We should call the contribute_to_class method only if it's bound
|
||||
if not inspect.isclass(value) and hasattr(value, 'contribute_to_class'):
|
||||
if not inspect.isclass(value) and hasattr(
|
||||
value, 'contribute_to_class'):
|
||||
value.contribute_to_class(cls, name)
|
||||
else:
|
||||
setattr(cls, name, value)
|
||||
|
@ -157,14 +159,16 @@ class BaseObjectType(BaseType):
|
|||
pass
|
||||
if kwargs:
|
||||
raise TypeError(
|
||||
"'%s' is an invalid keyword argument for this function" % list(kwargs)[0])
|
||||
"'%s' is an invalid keyword argument for this function" %
|
||||
list(kwargs)[0])
|
||||
|
||||
signals.post_init.send(self.__class__, instance=self)
|
||||
|
||||
@classmethod
|
||||
def fields_as_arguments(cls, schema):
|
||||
return OrderedDict([(f.attname, GraphQLArgument(f.internal_type(schema)))
|
||||
for f in cls._meta.fields])
|
||||
return OrderedDict(
|
||||
[(f.attname, GraphQLArgument(f.internal_type(schema)))
|
||||
for f in cls._meta.fields])
|
||||
|
||||
@classmethod
|
||||
def resolve_objecttype(cls, schema, instance, *args):
|
||||
|
|
|
@ -28,7 +28,11 @@ def test_type_as_field_called(Field):
|
|||
a = MountedType(2, description='A', resolver=resolver)
|
||||
a.as_field()
|
||||
Field.assert_called_with(
|
||||
a, 2, _creation_counter=a.creation_counter, description='A', resolver=resolver)
|
||||
a,
|
||||
2,
|
||||
_creation_counter=a.creation_counter,
|
||||
description='A',
|
||||
resolver=resolver)
|
||||
|
||||
|
||||
@patch('graphene.core.types.argument.Argument')
|
||||
|
|
|
@ -40,7 +40,8 @@ def test_custom_scalar():
|
|||
@staticmethod
|
||||
def parse_literal(node):
|
||||
if isinstance(node, ast.StringValue):
|
||||
return datetime.datetime.strptime(node.value, "%Y-%m-%dT%H:%M:%S.%f")
|
||||
return datetime.datetime.strptime(
|
||||
node.value, "%Y-%m-%dT%H:%M:%S.%f")
|
||||
|
||||
@staticmethod
|
||||
def parse_value(value):
|
||||
|
|
|
@ -10,12 +10,17 @@ class ConnectionField(Field):
|
|||
|
||||
def __init__(self, field_type, resolver=None, description='',
|
||||
connection_type=None, edge_type=None, **kwargs):
|
||||
super(ConnectionField, self).__init__(field_type, resolver=resolver,
|
||||
before=String(),
|
||||
after=String(),
|
||||
first=Int(),
|
||||
last=Int(),
|
||||
description=description, **kwargs)
|
||||
super(
|
||||
ConnectionField,
|
||||
self).__init__(
|
||||
field_type,
|
||||
resolver=resolver,
|
||||
before=String(),
|
||||
after=String(),
|
||||
first=Int(),
|
||||
last=Int(),
|
||||
description=description,
|
||||
**kwargs)
|
||||
self.connection_type = connection_type
|
||||
self.edge_type = edge_type
|
||||
|
||||
|
@ -37,8 +42,9 @@ class ConnectionField(Field):
|
|||
connection_type = self.get_connection_type(node)
|
||||
edge_type = self.get_edge_type(node)
|
||||
|
||||
connection = connection_from_list(resolved, args, connection_type=connection_type,
|
||||
edge_type=edge_type, pageinfo_type=PageInfo)
|
||||
connection = connection_from_list(
|
||||
resolved, args, connection_type=connection_type,
|
||||
edge_type=edge_type, pageinfo_type=PageInfo)
|
||||
connection.set_connection_data(resolved)
|
||||
return connection
|
||||
|
||||
|
|
|
@ -11,9 +11,11 @@ from graphql_relay.node.node import to_global_id
|
|||
|
||||
class PageInfo(ObjectType):
|
||||
has_next_page = BooleanField(
|
||||
required=True, description='When paginating forwards, are there more items?')
|
||||
required=True,
|
||||
description='When paginating forwards, are there more items?')
|
||||
has_previous_page = BooleanField(
|
||||
required=True, description='When paginating backwards, are there more items?')
|
||||
required=True,
|
||||
description='When paginating backwards, are there more items?')
|
||||
start_cursor = StringField(
|
||||
description='When paginating backwards, the cursor to continue.')
|
||||
end_cursor = StringField(
|
||||
|
@ -35,7 +37,10 @@ class Edge(ObjectType):
|
|||
def for_node(cls, node):
|
||||
from graphene.relay.utils import is_node
|
||||
assert is_node(node), 'ObjectTypes in a edge have to be Nodes'
|
||||
return type('%s%s' % (node._meta.type_name, cls._meta.type_name), (cls, ), {'node_type': node})
|
||||
return type(
|
||||
'%s%s' % (node._meta.type_name, cls._meta.type_name),
|
||||
(cls,),
|
||||
{'node_type': node})
|
||||
|
||||
|
||||
class Connection(ObjectType):
|
||||
|
@ -56,7 +61,10 @@ class Connection(ObjectType):
|
|||
from graphene.relay.utils import is_node
|
||||
edge_type = edge_type or Edge
|
||||
assert is_node(node), 'ObjectTypes in a connection have to be Nodes'
|
||||
return type('%s%s' % (node._meta.type_name, cls._meta.type_name), (cls, ), {'edge_type': edge_type.for_node(node)})
|
||||
return type(
|
||||
'%s%s' % (node._meta.type_name, cls._meta.type_name),
|
||||
(cls,),
|
||||
{'edge_type': edge_type.for_node(node)})
|
||||
|
||||
def set_connection_data(self, data):
|
||||
self._connection_data = data
|
||||
|
|
|
@ -2,7 +2,8 @@ from graphene.relay.types import BaseNode
|
|||
|
||||
|
||||
def is_node(object_type):
|
||||
return object_type and issubclass(object_type, BaseNode) and not is_node_type(object_type)
|
||||
return object_type and issubclass(
|
||||
object_type, BaseNode) and not is_node_type(object_type)
|
||||
|
||||
|
||||
def is_node_type(object_type):
|
||||
|
|
|
@ -19,11 +19,14 @@ def test_proxy_snake_dict():
|
|||
assert p.get('three_or_for') == 3
|
||||
assert 'inside' in p
|
||||
assert 'other_camel_case' in p['inside']
|
||||
assert sorted(p.items()) == sorted(list([('inside', ProxySnakeDict({'other_camel_case': 3})),
|
||||
('none', None),
|
||||
('three_or_for', 3),
|
||||
('two', 2),
|
||||
('one', 1)]))
|
||||
assert sorted(
|
||||
p.items()) == sorted(
|
||||
list(
|
||||
[('inside', ProxySnakeDict({'other_camel_case': 3})),
|
||||
('none', None),
|
||||
('three_or_for', 3),
|
||||
('two', 2),
|
||||
('one', 1)]))
|
||||
|
||||
|
||||
def test_proxy_snake_dict_as_kwargs():
|
||||
|
|
Loading…
Reference in New Issue
Block a user