From c74a75133e69c54de217a5ad0447bd9fcc308981 Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Tue, 14 Jun 2016 22:27:25 -0700 Subject: [PATCH] Autolint all the files --- examples/starwars/tests/test_schema.py | 5 +- examples/starwars_relay/schema.py | 3 +- graphene/__init__.py | 20 ++++- graphene/relay/__init__.py | 6 ++ graphene/relay/connection.py | 20 ++--- graphene/relay/mutation.py | 21 ++--- graphene/relay/node.py | 19 +++-- graphene/relay/tests/test_connection.py | 8 +- graphene/relay/tests/test_mutation.py | 12 +-- graphene/relay/tests/test_node.py | 7 +- graphene/relay/tests/test_node_custom.py | 73 ++++++++--------- graphene/types/__init__.py | 19 ++++- graphene/types/argument.py | 2 +- graphene/types/definitions.py | 1 + graphene/types/enum.py | 12 ++- graphene/types/field.py | 16 ++-- graphene/types/inputobjecttype.py | 12 +-- graphene/types/interface.py | 10 +-- graphene/types/mutation.py | 8 +- graphene/types/objecttype.py | 13 +-- graphene/types/scalars.py | 12 +-- graphene/types/schema.py | 7 +- graphene/types/structures.py | 1 + graphene/types/tests/test_argument.py | 5 +- graphene/types/tests/test_enum.py | 4 +- graphene/types/tests/test_field.py | 12 +-- graphene/types/tests/test_inputobjecttype.py | 8 +- graphene/types/tests/test_interface.py | 6 +- graphene/types/tests/test_mutation.py | 13 ++- graphene/types/tests/test_objecttype.py | 14 ++-- graphene/types/tests/test_scalars.py | 10 ++- graphene/types/tests/test_schema.py | 13 +-- graphene/types/tests/test_structures.py | 7 +- graphene/types/unmountedtype.py | 4 +- graphene/utils/copy_fields.py | 1 - graphene/utils/enum.py | 80 +++++++++++-------- graphene/utils/get_fields.py | 4 +- graphene/utils/tests/test_get_fields.py | 8 +- graphene/utils/tests/test_get_graphql_type.py | 8 +- graphene/utils/tests/test_is_graphene_type.py | 5 +- 40 files changed, 295 insertions(+), 214 deletions(-) diff --git a/examples/starwars/tests/test_schema.py b/examples/starwars/tests/test_schema.py index 264642ca..b7ae49e4 100644 --- a/examples/starwars/tests/test_schema.py +++ b/examples/starwars/tests/test_schema.py @@ -1,10 +1,9 @@ -from ..data import setup -from ..schema import schema, Droid +from ..schema import Droid def test_query_types(): graphql_type = Droid._meta.graphql_type fields = graphql_type.get_fields() assert fields['friends'].parent == Droid - assert fields \ No newline at end of file + assert fields diff --git a/examples/starwars_relay/schema.py b/examples/starwars_relay/schema.py index be410dac..e4fe0bc0 100644 --- a/examples/starwars_relay/schema.py +++ b/examples/starwars_relay/schema.py @@ -1,5 +1,5 @@ import graphene -from graphene import implements, relay, resolve_only_args +from graphene import relay, resolve_only_args from .data import create_ship, get_empire, get_faction, get_rebels, get_ship @@ -65,5 +65,4 @@ class Mutation(graphene.ObjectType): introduce_ship = IntroduceShip.Field() - schema = graphene.Schema(query=Query, mutation=Mutation) diff --git a/graphene/__init__.py b/graphene/__init__.py index f86859f4..827a6340 100644 --- a/graphene/__init__.py +++ b/graphene/__init__.py @@ -14,4 +14,22 @@ from .types import ( ) from .utils.resolve_only_args import resolve_only_args -__all__ = ['ObjectType', 'InputObjectType', 'Interface', 'implements', 'Field', 'InputField', 'Schema', 'Scalar', 'String', 'ID', 'Int', 'Float', 'Enum', 'Boolean', 'List','NonNull', 'Argument','resolve_only_args'] +__all__ = [ + 'ObjectType', + 'InputObjectType', + 'Interface', + 'implements', + 'Field', + 'InputField', + 'Schema', + 'Scalar', + 'String', + 'ID', + 'Int', + 'Float', + 'Enum', + 'Boolean', + 'List', + 'NonNull', + 'Argument', + 'resolve_only_args'] diff --git a/graphene/relay/__init__.py b/graphene/relay/__init__.py index a886c8ec..576b696c 100644 --- a/graphene/relay/__init__.py +++ b/graphene/relay/__init__.py @@ -1,3 +1,9 @@ from .node import Node from .mutation import ClientIDMutation from .connection import Connection + +__all__ = [ + 'Node', + 'ClientIDMutation', + 'Connection', +] diff --git a/graphene/relay/connection.py b/graphene/relay/connection.py index 3be23d08..16a4d441 100644 --- a/graphene/relay/connection.py +++ b/graphene/relay/connection.py @@ -1,23 +1,17 @@ import re from collections import Iterable + import six + from graphql_relay import connection_definitions, connection_from_list from ..types.field import Field from ..types.objecttype import ObjectType, ObjectTypeMeta - -from ..utils.props import props - -from ..types.field import Field, InputField -from ..utils.get_fields import get_fields -from ..utils.copy_fields import copy_fields -from ..utils.props import props - - -from ..types.objecttype import ObjectType - -from ..utils.is_base_type import is_base_type from ..types.options import Options +from ..utils.copy_fields import copy_fields +from ..utils.get_fields import get_fields +from ..utils.is_base_type import is_base_type +from ..utils.props import props class ConnectionMeta(ObjectTypeMeta): @@ -69,7 +63,7 @@ class Connection(six.with_metaclass(ConnectionMeta, ObjectType)): class IterableConnectionField(Field): # def __init__(self, type, *args, **kwargs): - # if + # if def resolver(self, root, args, context, info): iterable = super(ConnectionField, self).resolver(root, args, context, info) diff --git a/graphene/relay/mutation.py b/graphene/relay/mutation.py index ee9128dd..c236c49b 100644 --- a/graphene/relay/mutation.py +++ b/graphene/relay/mutation.py @@ -1,19 +1,18 @@ from functools import partial + import six + from graphql_relay import mutation_with_client_mutation_id -from ..types.mutation import Mutation, MutationMeta -from ..types.inputobjecttype import InputObjectType from ..types.field import Field, InputField -from ..utils.get_fields import get_fields -from ..utils.copy_fields import copy_fields -from ..utils.props import props - - +from ..types.inputobjecttype import InputObjectType +from ..types.mutation import Mutation, MutationMeta from ..types.objecttype import ObjectType - -from ..utils.is_base_type import is_base_type from ..types.options import Options +from ..utils.copy_fields import copy_fields +from ..utils.get_fields import get_fields +from ..utils.is_base_type import is_base_type +from ..utils.props import props class ClientIDMutationMeta(MutationMeta): @@ -41,7 +40,8 @@ class ClientIDMutationMeta(MutationMeta): output_fields = copy_fields(Field, get_fields(ObjectType, attrs, bases)) mutate_and_get_payload = getattr(cls, 'mutate_and_get_payload', None) - assert mutate_and_get_payload, "{}.mutate_and_get_payload method is required in a ClientIDMutation ObjectType.".format(cls.__name__) + assert mutate_and_get_payload, "{}.mutate_and_get_payload method is required in a ClientIDMutation ObjectType.".format( + cls.__name__) field = mutation_with_client_mutation_id( name=options.name or cls.__name__, @@ -55,5 +55,6 @@ class ClientIDMutationMeta(MutationMeta): class ClientIDMutation(six.with_metaclass(ClientIDMutationMeta, Mutation)): + class Meta: abstract = True diff --git a/graphene/relay/node.py b/graphene/relay/node.py index a2f67817..778f8295 100644 --- a/graphene/relay/node.py +++ b/graphene/relay/node.py @@ -1,11 +1,13 @@ from functools import partial + import six -from graphql_relay import node_definitions, from_global_id, to_global_id + +from graphql_relay import from_global_id, node_definitions, to_global_id from ..types.field import Field -from ..types.options import Options -from ..types.objecttype import ObjectTypeMeta from ..types.interface import Interface +from ..types.objecttype import ObjectTypeMeta +from ..types.options import Options class NodeMeta(ObjectTypeMeta): @@ -22,7 +24,12 @@ class NodeMeta(ObjectTypeMeta): id_resolver=id_resolver, ) cls._meta = Options(None, graphql_type=node_interface) - cls.Field = partial(Field.copy_and_extend, node_field, type=node_field.type, parent=cls, _creation_counter=None) + cls.Field = partial( + Field.copy_and_extend, + node_field, + type=node_field.type, + parent=cls, + _creation_counter=None) else: # The interface provided by node_definitions is not an instance # of GrapheneInterfaceType, so it will have no graphql_type, @@ -71,6 +78,8 @@ class Node(six.with_metaclass(NodeMeta, Interface)): in it ''' if cls.require_get_node(): - assert hasattr(object_type, 'get_node'), '{}.get_node method is required by the Node interface.'.format(object_type.__name__) + assert hasattr( + object_type, 'get_node'), '{}.get_node method is required by the Node interface.'.format( + object_type.__name__) return super(Node, cls).implements(object_type) diff --git a/graphene/relay/tests/test_connection.py b/graphene/relay/tests/test_connection.py index 8a25b306..fc14d9da 100644 --- a/graphene/relay/tests/test_connection.py +++ b/graphene/relay/tests/test_connection.py @@ -1,10 +1,9 @@ -import pytest +from ...types import ObjectType, Schema +from ...types.field import Field +from ...types.scalars import String from ..connection import Connection from ..node import Node -from ...types import ObjectType, Schema -from ...types.scalars import String -from ...types.field import Field class MyObject(Node, ObjectType): @@ -16,6 +15,7 @@ class MyObject(Node, ObjectType): class MyObjectConnection(Connection): + class Meta: node = MyObject diff --git a/graphene/relay/tests/test_mutation.py b/graphene/relay/tests/test_mutation.py index 7765e615..39bc1b85 100644 --- a/graphene/relay/tests/test_mutation.py +++ b/graphene/relay/tests/test_mutation.py @@ -1,13 +1,12 @@ import pytest -from graphql_relay import to_global_id - -from ..mutation import ClientIDMutation -from ...types import ObjectType, Schema, implements +from ...types import ObjectType, Schema from ...types.scalars import String +from ..mutation import ClientIDMutation class SaySomething(ClientIDMutation): + class Input: what = String() phrase = String() @@ -33,7 +32,8 @@ def test_no_mutate_and_get_payload(): class MyMutation(ClientIDMutation): pass - assert "MyMutation.mutate_and_get_payload method is required in a ClientIDMutation ObjectType." == str(excinfo.value) + assert "MyMutation.mutate_and_get_payload method is required in a ClientIDMutation ObjectType." == str( + excinfo.value) def test_node_good(): @@ -81,4 +81,4 @@ def test_node_query(): # first: String # node(id: ID!): Node # } -# """.lstrip() \ No newline at end of file +# """.lstrip() diff --git a/graphene/relay/tests/test_node.py b/graphene/relay/tests/test_node.py index 58c0eb55..2a57539a 100644 --- a/graphene/relay/tests/test_node.py +++ b/graphene/relay/tests/test_node.py @@ -2,9 +2,9 @@ import pytest from graphql_relay import to_global_id -from ..node import Node -from ...types import ObjectType, Schema, implements +from ...types import ObjectType, Schema from ...types.scalars import String +from ..node import Node class MyNode(Node, ObjectType): @@ -59,6 +59,7 @@ def test_node_query_incorrect_id(): assert not executed.errors assert executed.data == {'node': None} + def test_str_schema(): assert str(schema) == """ schema { @@ -78,4 +79,4 @@ type RootQuery { first: String node(id: ID!): Node } -""".lstrip() \ No newline at end of file +""".lstrip() diff --git a/graphene/relay/tests/test_node_custom.py b/graphene/relay/tests/test_node_custom.py index fcccec9b..73a17fc9 100644 --- a/graphene/relay/tests/test_node_custom.py +++ b/graphene/relay/tests/test_node_custom.py @@ -1,11 +1,12 @@ from graphql import graphql +from ...types import ObjectType, Schema +from ...types.scalars import Int, String from ..node import Node -from ...types import ObjectType, Schema, implements -from ...types.scalars import String, Int class CustomNode(Node): + @staticmethod def to_global_id(type, id): return id @@ -222,20 +223,20 @@ def test_have_correct_node_interface(): ''' expected = { '__type': { - 'name': 'Node', - 'kind': 'INTERFACE', - 'fields': [ - { - 'name': 'id', - 'type': { - 'kind': 'NON_NULL', - 'ofType': { - 'name': 'ID', - 'kind': 'SCALAR' + 'name': 'Node', + 'kind': 'INTERFACE', + 'fields': [ + { + 'name': 'id', + 'type': { + 'kind': 'NON_NULL', + 'ofType': { + 'name': 'ID', + 'kind': 'SCALAR' + } + } } - } - } - ] + ] } } result = graphql(schema, query) @@ -271,29 +272,29 @@ def test_has_correct_node_root_field(): ''' expected = { '__schema': { - 'queryType': { - 'fields': [ - { - 'name': 'node', - 'type': { - 'name': 'Node', - 'kind': 'INTERFACE' - }, - 'args': [ - { - 'name': 'id', - 'type': { - 'kind': 'NON_NULL', - 'ofType': { - 'name': 'ID', - 'kind': 'SCALAR' - } + 'queryType': { + 'fields': [ + { + 'name': 'node', + 'type': { + 'name': 'Node', + 'kind': 'INTERFACE' + }, + 'args': [ + { + 'name': 'id', + 'type': { + 'kind': 'NON_NULL', + 'ofType': { + 'name': 'ID', + 'kind': 'SCALAR' + } + } + } + ] } - } ] - } - ] - } + } } } result = graphql(schema, query) diff --git a/graphene/types/__init__.py b/graphene/types/__init__.py index e9d1d1a8..bf0de0fe 100644 --- a/graphene/types/__init__.py +++ b/graphene/types/__init__.py @@ -8,4 +8,21 @@ from .field import Field, InputField from .argument import Argument from .inputobjecttype import InputObjectType -__all__ = ['ObjectType', 'InputObjectType', 'Interface', 'implements', 'Enum', 'Field', 'InputField', 'Schema', 'Scalar', 'String', 'ID', 'Int', 'Float', 'Boolean', 'List', 'NonNull', 'Argument'] +__all__ = [ + 'ObjectType', + 'InputObjectType', + 'Interface', + 'implements', + 'Enum', + 'Field', + 'InputField', + 'Schema', + 'Scalar', + 'String', + 'ID', + 'Int', + 'Float', + 'Boolean', + 'List', + 'NonNull', + 'Argument'] diff --git a/graphene/types/argument.py b/graphene/types/argument.py index 52a96c9e..23146e17 100644 --- a/graphene/types/argument.py +++ b/graphene/types/argument.py @@ -1,6 +1,6 @@ import copy -from collections import OrderedDict import inspect +from collections import OrderedDict from itertools import chain from graphql import GraphQLArgument diff --git a/graphene/types/definitions.py b/graphene/types/definitions.py index 5190e59a..978cdf01 100644 --- a/graphene/types/definitions.py +++ b/graphene/types/definitions.py @@ -1,4 +1,5 @@ class GrapheneGraphQLType(object): + def __init__(self, *args, **kwargs): self.graphene_type = kwargs.pop('graphene_type') super(GrapheneGraphQLType, self).__init__(*args, **kwargs) diff --git a/graphene/types/enum.py b/graphene/types/enum.py index a60d26ff..5f2d4452 100644 --- a/graphene/types/enum.py +++ b/graphene/types/enum.py @@ -1,16 +1,19 @@ -import six -from graphql.type import GraphQLEnumType, GraphQLEnumValue from collections import OrderedDict +import six + +from graphql.type import GraphQLEnumType, GraphQLEnumValue + +from ..utils.is_base_type import is_base_type from .definitions import GrapheneGraphQLType from .options import Options -from ..utils.is_base_type import is_base_type +from .unmountedtype import UnmountedType + try: from enum import Enum as PyEnum except ImportError: from ..utils.enum import Enum as PyEnum -from .unmountedtype import UnmountedType class GrapheneEnumType(GrapheneGraphQLType, GraphQLEnumType): @@ -65,6 +68,7 @@ class EnumTypeMeta(type): class Enum(six.with_metaclass(EnumTypeMeta, UnmountedType)): + @classmethod def from_enum(cls, python_enum): class Meta: diff --git a/graphene/types/field.py b/graphene/types/field.py index 2ae51f69..2b4331ea 100644 --- a/graphene/types/field.py +++ b/graphene/types/field.py @@ -9,6 +9,7 @@ from .argument import to_arguments class AbstractField(object): + @property def name(self): return self._name or self.attname and to_camel_case(self.attname) @@ -39,7 +40,8 @@ class AbstractField(object): class Field(AbstractField, GraphQLField, OrderedType): - def __init__(self, type, args=None, resolver=None, source=None, deprecation_reason=None, name=None, description=None, required=False, _creation_counter=None, **extra_args): + def __init__(self, type, args=None, resolver=None, source=None, deprecation_reason=None, + name=None, description=None, required=False, _creation_counter=None, **extra_args): self.name = name self.attname = None self.parent = None @@ -74,8 +76,7 @@ class Field(AbstractField, GraphQLField, OrderedType): @property def resolver(self): - from .objecttype import ObjectType - from .interface import GrapheneInterfaceType + pass resolver = getattr(self.parent, 'resolve_{}'.format(self.attname), None) @@ -105,7 +106,7 @@ class Field(AbstractField, GraphQLField, OrderedType): # root = self.parent() # return resolver(root, *args, **kwargs) - return self._resolver or resolver # resolver_wrapper + return self._resolver or resolver # resolver_wrapper @resolver.setter def resolver(self, resolver): @@ -115,7 +116,9 @@ class Field(AbstractField, GraphQLField, OrderedType): return self.copy_and_extend(self) @classmethod - def copy_and_extend(cls, field, type=None, args=None, resolver=None, source=None, deprecation_reason=None, name=None, description=None, required=False, _creation_counter=False, parent=None, attname=None, **extra_args): + def copy_and_extend( + cls, field, type=None, args=None, resolver=None, source=None, deprecation_reason=None, name=None, + description=None, required=False, _creation_counter=False, parent=None, attname=None, **extra_args): if isinstance(field, Field): type = type or field._type resolver = resolver or field._resolver @@ -185,7 +188,8 @@ class InputField(AbstractField, GraphQLInputObjectField, OrderedType): return self.copy_and_extend(self) @classmethod - def copy_and_extend(cls, field, type=None, default_value=None, description=None, name=None, required=False, parent=None, attname=None, _creation_counter=False): + def copy_and_extend(cls, field, type=None, default_value=None, description=None, name=None, + required=False, parent=None, attname=None, _creation_counter=False): if isinstance(field, Field): type = type or field._type name = name or field._name diff --git a/graphene/types/inputobjecttype.py b/graphene/types/inputobjecttype.py index d1fce2a1..465b6db0 100644 --- a/graphene/types/inputobjecttype.py +++ b/graphene/types/inputobjecttype.py @@ -2,14 +2,14 @@ import six from graphql import GraphQLInputObjectType -from .definitions import GrapheneGraphQLType -from .interface import attrs_without_fields -from .unmountedtype import UnmountedType -from .options import Options -from ..utils.is_base_type import is_base_type -from ..utils.get_fields import get_fields from ..utils.copy_fields import copy_fields +from ..utils.get_fields import get_fields +from ..utils.is_base_type import is_base_type +from .definitions import GrapheneGraphQLType from .field import InputField +from .interface import attrs_without_fields +from .options import Options +from .unmountedtype import UnmountedType class GrapheneInputObjectType(GrapheneGraphQLType, GraphQLInputObjectType): diff --git a/graphene/types/interface.py b/graphene/types/interface.py index d54ab991..190d9f66 100644 --- a/graphene/types/interface.py +++ b/graphene/types/interface.py @@ -1,12 +1,13 @@ import six from graphql import GraphQLInterfaceType -from .definitions import GrapheneGraphQLType -from .options import Options -from ..utils.is_base_type import is_base_type -from ..utils.get_fields import get_fields + from ..utils.copy_fields import copy_fields +from ..utils.get_fields import get_fields +from ..utils.is_base_type import is_base_type +from .definitions import GrapheneGraphQLType from .field import Field +from .options import Options class GrapheneInterfaceType(GrapheneGraphQLType, GraphQLInterfaceType): @@ -72,4 +73,3 @@ class Interface(six.with_metaclass(InterfaceTypeMeta)): For example, if we want to check that the ObjectType have some required things in it like Node.get_node ''' - pass diff --git a/graphene/types/mutation.py b/graphene/types/mutation.py index 8a1fd0dc..ee015d76 100644 --- a/graphene/types/mutation.py +++ b/graphene/types/mutation.py @@ -1,11 +1,11 @@ from functools import partial + import six -from .objecttype import ObjectTypeMeta, ObjectType -from .field import Field - -from ..utils.props import props from ..utils.is_base_type import is_base_type +from ..utils.props import props +from .field import Field +from .objecttype import ObjectType, ObjectTypeMeta class MutationMeta(ObjectTypeMeta): diff --git a/graphene/types/objecttype.py b/graphene/types/objecttype.py index 37061822..9e899da4 100644 --- a/graphene/types/objecttype.py +++ b/graphene/types/objecttype.py @@ -1,15 +1,17 @@ import copy + import six from graphql import GraphQLObjectType -from .definitions import GrapheneGraphQLType -from .interface import GrapheneInterfaceType, InterfaceTypeMeta, Interface, attrs_without_fields -from .options import Options -from ..utils.is_base_type import is_base_type -from ..utils.get_fields import get_fields from ..utils.copy_fields import copy_fields +from ..utils.get_fields import get_fields +from ..utils.is_base_type import is_base_type +from .definitions import GrapheneGraphQLType from .field import Field +from .interface import (GrapheneInterfaceType, Interface, InterfaceTypeMeta, + attrs_without_fields) +from .options import Options class GrapheneObjectType(GrapheneGraphQLType, GraphQLObjectType): @@ -120,6 +122,7 @@ def implements(*interfaces): class ObjectType(six.with_metaclass(ObjectTypeMeta)): + def __init__(self, *args, **kwargs): # GraphQL ObjectType acting as container args_len = len(args) diff --git a/graphene/types/scalars.py b/graphene/types/scalars.py index bf01420c..3934fbde 100644 --- a/graphene/types/scalars.py +++ b/graphene/types/scalars.py @@ -1,10 +1,12 @@ import six -from graphql import GraphQLScalarType, GraphQLString, GraphQLInt, GraphQLFloat, GraphQLBoolean, GraphQLID -from .definitions import GrapheneGraphQLType -from .unmountedtype import UnmountedType -from .options import Options +from graphql import (GraphQLBoolean, GraphQLFloat, GraphQLID, GraphQLInt, + GraphQLScalarType, GraphQLString) + from ..utils.is_base_type import is_base_type +from .definitions import GrapheneGraphQLType +from .options import Options +from .unmountedtype import UnmountedType class GrapheneScalarType(GrapheneGraphQLType, GraphQLScalarType): @@ -53,7 +55,7 @@ def construct_scalar_class(graphql_type): # class String(Scalar): # class Meta: # graphql_type = graphql_type - Meta = type('Meta', (object,), {'graphql_type':graphql_type}) + Meta = type('Meta', (object,), {'graphql_type': graphql_type}) return type(graphql_type.name, (Scalar, ), {'Meta': Meta}) diff --git a/graphene/types/schema.py b/graphene/types/schema.py index cc18d31a..24b68702 100644 --- a/graphene/types/schema.py +++ b/graphene/types/schema.py @@ -1,9 +1,12 @@ -from graphql import graphql, GraphQLSchema +from graphql import GraphQLSchema, graphql from graphql.utils.introspection_query import introspection_query from graphql.utils.schema_printer import print_schema -# from graphql.type.schema import assert_object_implements_interface from ..utils.get_graphql_type import get_graphql_type + + +# from graphql.type.schema import assert_object_implements_interface + # from collections import defaultdict diff --git a/graphene/types/structures.py b/graphene/types/structures.py index d2ad1716..de1b6b1e 100644 --- a/graphene/types/structures.py +++ b/graphene/types/structures.py @@ -6,6 +6,7 @@ from .unmountedtype import UnmountedType class Structure(UnmountedType): + def __init__(self, of_type, *args, **kwargs): super(Structure, self).__init__(*args, **kwargs) self.of_type = of_type diff --git a/graphene/types/tests/test_argument.py b/graphene/types/tests/test_argument.py index 2c4b057c..6969130f 100644 --- a/graphene/types/tests/test_argument.py +++ b/graphene/types/tests/test_argument.py @@ -1,7 +1,8 @@ -import pytest import copy -from graphql import GraphQLString, GraphQLArgument +import pytest + +from graphql import GraphQLArgument, GraphQLString from ..argument import Argument, to_arguments from ..scalars import String diff --git a/graphene/types/tests/test_enum.py b/graphene/types/tests/test_enum.py index 8f07ccab..af7c4a9e 100644 --- a/graphene/types/tests/test_enum.py +++ b/graphene/types/tests/test_enum.py @@ -1,9 +1,9 @@ from graphql.type import GraphQLEnumType, GraphQLEnumValue +from ...utils.enum import Enum as PyEnum +from ..argument import Argument from ..enum import Enum from ..field import Field -from ..argument import Argument -from ...utils.enum import Enum as PyEnum def test_enum_construction(): diff --git a/graphene/types/tests/test_field.py b/graphene/types/tests/test_field.py index daaa253f..521f9b7d 100644 --- a/graphene/types/tests/test_field.py +++ b/graphene/types/tests/test_field.py @@ -1,12 +1,12 @@ -import pytest import copy -from graphql import GraphQLString, GraphQLField, GraphQLInt, GraphQLNonNull +import pytest + +from graphql import GraphQLField, GraphQLInt, GraphQLNonNull, GraphQLString -from ..field import Field from ..argument import Argument -from ..objecttype import ObjectType -from ..scalars import String, Int +from ..field import Field +from ..scalars import Int, String def test_field(): @@ -33,7 +33,7 @@ def test_field_wrong_name(): def test_not_source_and_resolver(): with pytest.raises(AssertionError) as excinfo: - Field(GraphQLString, source="a", resolver=lambda *_:None) + Field(GraphQLString, source="a", resolver=lambda *_: None) assert "You cannot have a source and a resolver at the same time" == str(excinfo.value) diff --git a/graphene/types/tests/test_inputobjecttype.py b/graphene/types/tests/test_inputobjecttype.py index f6546b2b..6455b1c0 100644 --- a/graphene/types/tests/test_inputobjecttype.py +++ b/graphene/types/tests/test_inputobjecttype.py @@ -1,17 +1,14 @@ -import pytest -from graphql import GraphQLObjectType, GraphQLField, GraphQLString, GraphQLInputObjectType +from graphql import GraphQLInputObjectType, GraphQLString -from ..objecttype import ObjectType +from ..field import InputField from ..inputobjecttype import InputObjectType -from ..field import Field, InputField from ..scalars import String def test_generate_inputobjecttype(): class MyObjectType(InputObjectType): '''Documentation''' - pass graphql_type = MyObjectType._meta.graphql_type assert isinstance(graphql_type, GraphQLInputObjectType) @@ -21,6 +18,7 @@ def test_generate_inputobjecttype(): def test_generate_inputobjecttype_with_meta(): class MyObjectType(InputObjectType): + class Meta: name = 'MyOtherObjectType' description = 'Documentation' diff --git a/graphene/types/tests/test_interface.py b/graphene/types/tests/test_interface.py index 7c48b3a9..9370de98 100644 --- a/graphene/types/tests/test_interface.py +++ b/graphene/types/tests/test_interface.py @@ -1,15 +1,14 @@ import pytest -from graphql import GraphQLInterfaceType, GraphQLField, GraphQLString, GraphQLInterfaceType +from graphql import GraphQLField, GraphQLInterfaceType, GraphQLString -from ..interface import Interface from ..field import Field +from ..interface import Interface def test_generate_interface(): class MyInterface(Interface): '''Documentation''' - pass graphql_type = MyInterface._meta.graphql_type assert isinstance(graphql_type, GraphQLInterfaceType) @@ -19,6 +18,7 @@ def test_generate_interface(): def test_generate_interface_with_meta(): class MyInterface(Interface): + class Meta: name = 'MyOtherInterface' description = 'Documentation' diff --git a/graphene/types/tests/test_mutation.py b/graphene/types/tests/test_mutation.py index 97ac39df..fa77f662 100644 --- a/graphene/types/tests/test_mutation.py +++ b/graphene/types/tests/test_mutation.py @@ -1,14 +1,11 @@ import pytest -from graphql import GraphQLObjectType, GraphQLField, GraphQLString, GraphQLInterfaceType +from graphql import GraphQLObjectType, GraphQLString -from ..schema import Schema -from ..objecttype import ObjectType -from ..mutation import Mutation -from ..interface import Interface -from ..scalars import String from ..field import Field -from ..argument import Argument +from ..mutation import Mutation +from ..objecttype import ObjectType +from ..scalars import String def test_generate_mutation_no_args(): @@ -48,6 +45,7 @@ def test_generate_mutation_with_args(): def test_generate_mutation_with_meta(): class MyMutation(Mutation): + class Meta: name = 'MyOtherMutation' description = 'Documentation' @@ -64,6 +62,7 @@ def test_generate_mutation_with_meta(): def test_empty_mutation_has_meta(): class MyMutation(Mutation): + @classmethod def mutate(cls, *args, **kwargs): pass diff --git a/graphene/types/tests/test_objecttype.py b/graphene/types/tests/test_objecttype.py index 9379cc6a..64177a2f 100644 --- a/graphene/types/tests/test_objecttype.py +++ b/graphene/types/tests/test_objecttype.py @@ -1,12 +1,11 @@ import pytest -from graphql import GraphQLObjectType, GraphQLField, GraphQLString, GraphQLInterfaceType +from graphql import (GraphQLField, GraphQLInterfaceType, GraphQLObjectType, + GraphQLString) -from ..schema import Schema -from ..objecttype import ObjectType -from ..interface import Interface -from ..scalars import String from ..field import Field +from ..interface import Interface +from ..objecttype import ObjectType class Container(ObjectType): @@ -17,7 +16,6 @@ class Container(ObjectType): def test_generate_objecttype(): class MyObjectType(ObjectType): '''Documentation''' - pass graphql_type = MyObjectType._meta.graphql_type assert isinstance(graphql_type, GraphQLObjectType) @@ -27,6 +25,7 @@ def test_generate_objecttype(): def test_generate_objecttype_with_meta(): class MyObjectType(ObjectType): + class Meta: name = 'MyOtherObjectType' description = 'Documentation' @@ -139,6 +138,7 @@ def test_objecttype_reuse_graphql_type(): }) class GrapheneObjectType(ObjectType): + class Meta: graphql_type = MyGraphQLType @@ -169,6 +169,7 @@ def test_objecttype_graphql_interface(): }) class GrapheneObjectType(ObjectType): + class Meta: interfaces = [MyInterface] @@ -185,6 +186,7 @@ def test_objecttype_graphene_interface(): extended = Field(GraphQLString) class GrapheneObjectType(ObjectType): + class Meta: interfaces = [GrapheneInterface] diff --git a/graphene/types/tests/test_scalars.py b/graphene/types/tests/test_scalars.py index ad9ab102..0e43d203 100644 --- a/graphene/types/tests/test_scalars.py +++ b/graphene/types/tests/test_scalars.py @@ -1,13 +1,15 @@ import pytest -from ..scalars import Scalar, String, Int, Float, Boolean + +from graphene.utils.get_graphql_type import get_graphql_type +from graphql import GraphQLBoolean, GraphQLFloat, GraphQLInt, GraphQLString + from ..field import Field from ..objecttype import ObjectType - -from graphql import GraphQLString, GraphQLInt, GraphQLFloat, GraphQLBoolean -from graphene.utils.get_graphql_type import get_graphql_type +from ..scalars import Boolean, Float, Int, Scalar, String class DatetimeScalar(Scalar): + def serialize(value): return value.isoformat() diff --git a/graphene/types/tests/test_schema.py b/graphene/types/tests/test_schema.py index b42a9343..fcda7283 100644 --- a/graphene/types/tests/test_schema.py +++ b/graphene/types/tests/test_schema.py @@ -1,9 +1,9 @@ -from ..scalars import String from ..field import Field -from ..objecttype import ObjectType from ..interface import Interface -from ..structures import List +from ..objecttype import ObjectType +from ..scalars import String from ..schema import Schema +from ..structures import List class Character(Interface): @@ -18,6 +18,7 @@ class Pet(ObjectType): # @implements(Character) class Human(ObjectType): + class Meta: interfaces = [Character] @@ -44,9 +45,11 @@ schema = Schema(query=RootQuery, types=[Human]) def test_schema(): - executed = schema.execute('{ character {name, bestFriend { name }, friends { name}, ...on Human {pet { type } } } }') + executed = schema.execute( + '{ character {name, bestFriend { name }, friends { name}, ...on Human {pet { type } } } }') assert not executed.errors - assert executed.data == {'character': {'name': 'Harry', 'bestFriend': {'name': 'Best'}, 'friends': [{'name': 'Peter'}], 'pet': {'type': 'Dog'}}} + assert executed.data == {'character': {'name': 'Harry', 'bestFriend': { + 'name': 'Best'}, 'friends': [{'name': 'Peter'}], 'pet': {'type': 'Dog'}}} def test_schema_introspect(): diff --git a/graphene/types/tests/test_structures.py b/graphene/types/tests/test_structures.py index 48b39568..42025705 100644 --- a/graphene/types/tests/test_structures.py +++ b/graphene/types/tests/test_structures.py @@ -1,10 +1,9 @@ -import pytest -from graphql import GraphQLString, GraphQLList, GraphQLNonNull +from graphql import GraphQLList, GraphQLNonNull, GraphQLString -from ..structures import List, NonNull -from ..scalars import String from ..field import Field +from ..scalars import String +from ..structures import List, NonNull def test_list(): diff --git a/graphene/types/unmountedtype.py b/graphene/types/unmountedtype.py index a7514e16..28745810 100644 --- a/graphene/types/unmountedtype.py +++ b/graphene/types/unmountedtype.py @@ -1,6 +1,6 @@ -from .field import Field, InputField -from .argument import Argument from ..utils.orderedtype import OrderedType +from .argument import Argument +from .field import Field, InputField # UnmountedType ? diff --git a/graphene/utils/copy_fields.py b/graphene/utils/copy_fields.py index 19c5ef38..53570250 100644 --- a/graphene/utils/copy_fields.py +++ b/graphene/utils/copy_fields.py @@ -1,5 +1,4 @@ from collections import OrderedDict -from ..types.field import Field, InputField def copy_fields(like, fields, **extra): diff --git a/graphene/utils/enum.py b/graphene/utils/enum.py index b71c212c..1c535ffc 100644 --- a/graphene/utils/enum.py +++ b/graphene/utils/enum.py @@ -35,6 +35,7 @@ except NameError: # In Python 3 unicode no longer exists (it's just str) unicode = str + class _RouteClassAttributeToGetattr(object): """Route attribute access on a class to __getattr__. @@ -44,6 +45,7 @@ class _RouteClassAttributeToGetattr(object): class's __getattr__ method; this is done by raising AttributeError. """ + def __init__(self, fget=None): self.fget = fget @@ -62,9 +64,9 @@ class _RouteClassAttributeToGetattr(object): def _is_descriptor(obj): """Returns True if obj is a descriptor, False otherwise.""" return ( - hasattr(obj, '__get__') or - hasattr(obj, '__set__') or - hasattr(obj, '__delete__')) + hasattr(obj, '__get__') or + hasattr(obj, '__set__') or + hasattr(obj, '__delete__')) def _is_dunder(name): @@ -85,6 +87,7 @@ def _is_sunder(name): def _make_class_unpicklable(cls): """Make the given class un-picklable.""" + def _break_on_call_reduce(self, protocol=None): raise TypeError('%r cannot be pickled' % self) cls.__reduce_ex__ = _break_on_call_reduce @@ -98,6 +101,7 @@ class _EnumDict(dict): enumeration member names. """ + def __init__(self): super(_EnumDict, self).__init__() self._member_names = [] @@ -155,7 +159,7 @@ class EnumMeta(type): # cannot be mixed with other types (int, float, etc.) if it has an # inherited __new__ unless a new __new__ is defined (or the resulting # class will fail). - if type(classdict) is dict: + if isinstance(classdict, dict): original_dict = classdict classdict = _EnumDict() for k, v in original_dict.items(): @@ -163,7 +167,7 @@ class EnumMeta(type): member_type, first_enum = metacls._get_mixins_(bases) __new__, save_new, use_args = metacls._find_new_(classdict, member_type, - first_enum) + first_enum) # save enum items into separate mapping so they don't get baked into # the new class members = dict((k, classdict[k]) for k in classdict._member_names) @@ -257,7 +261,6 @@ class EnumMeta(type): except TypeError: pass - # If a custom type is mixed into the Enum, and it does not know how # to pickle itself, pickle.dumps will succeed but pickle.loads will # fail. Rather than have the error show up later and possibly far @@ -272,17 +275,16 @@ class EnumMeta(type): if '__reduce_ex__' not in classdict: if member_type is not object: methods = ('__getnewargs_ex__', '__getnewargs__', - '__reduce_ex__', '__reduce__') + '__reduce_ex__', '__reduce__') if not any(m in member_type.__dict__ for m in methods): _make_class_unpicklable(enum_class) unpicklable = True - # double check that repr and friends are not the mixin's or various # things break (such as pickle) for name in ('__repr__', '__str__', '__format__', '__reduce_ex__'): class_method = getattr(enum_class, name) - obj_method = getattr(member_type, name, None) + getattr(member_type, name, None) enum_method = getattr(first_enum, name, None) if name not in classdict and class_method is not enum_method: if name == '__reduce_ex__' and unpicklable: @@ -308,7 +310,7 @@ class EnumMeta(type): '__eq__', '__ne__', '__hash__', - ): + ): setattr(enum_class, method, getattr(int, method)) # replace any other __new__ with our own (as long as Enum is not None, @@ -356,7 +358,7 @@ class EnumMeta(type): # (see issue19025). if attr in cls._member_map_: raise AttributeError( - "%s: cannot delete Enum member." % cls.__name__) + "%s: cannot delete Enum member." % cls.__name__) super(EnumMeta, cls).__delattr__(attr) def __dir__(self): @@ -450,7 +452,7 @@ class EnumMeta(type): if isinstance(names, basestring): names = names.replace(',', ' ').split() if isinstance(names, (tuple, list)) and isinstance(names[0], basestring): - names = [(e, i+start) for (i, e) in enumerate(names)] + names = [(e, i + start) for (i, e) in enumerate(names)] # Here, names is either an iterable of (name, value) or a mapping. item = None # in case names is empty @@ -491,20 +493,19 @@ class EnumMeta(type): if not bases or Enum is None: return object, Enum - # double check that we are not subclassing a class with existing # enumeration members; while we're at it, see if any other data # type has been mixed in so we can use the correct __new__ member_type = first_enum = None for base in bases: - if (base is not Enum and + if (base is not Enum and issubclass(base, Enum) and base._member_names_): raise TypeError("Cannot extend enumerations") # base is now the last base in bases if not issubclass(base, Enum): raise TypeError("new enumerations must be created as " - "`ClassName([mixin_type,] enum_type)`") + "`ClassName([mixin_type,] enum_type)`") # get correct mix-in type (either mix-in type of Enum subclass, or # first base if last base is Enum) @@ -562,7 +563,7 @@ class EnumMeta(type): N__new__, O__new__, E__new__, - ]: + ]: if method == '__member_new__': classdict['__new__'] = target return None, False, True @@ -613,7 +614,7 @@ class EnumMeta(type): None.__new__, object.__new__, Enum.__new__, - ): + ): __new__ = target break if __new__ is not None: @@ -641,14 +642,15 @@ class EnumMeta(type): temp_enum_dict = {} temp_enum_dict['__doc__'] = "Generic enumeration.\n\n Derive from this class to define new enumerations.\n\n" + def __new__(cls, value): # all enum instances are actually created during class construction # without calling this method; this method is called by the metaclass' # __call__ (i.e. Color(3) ), and by pickle - if type(value) is cls: + if isinstance(value, cls): # For lookups like Color(Color.red) value = value.value - #return value + # return value # by-value search for a matching enum member # see if it's in the reverse mapping (for hashable values) try: @@ -663,12 +665,14 @@ def __new__(cls, value): temp_enum_dict['__new__'] = __new__ del __new__ + def __repr__(self): return "<%s.%s: %r>" % ( - self.__class__.__name__, self._name_, self._value_) + self.__class__.__name__, self._name_, self._value_) temp_enum_dict['__repr__'] = __repr__ del __repr__ + def __str__(self): return "%s.%s" % (self.__class__.__name__, self._name_) temp_enum_dict['__str__'] = __str__ @@ -677,15 +681,16 @@ del __str__ if pyver >= 3.0: def __dir__(self): added_behavior = [ - m - for cls in self.__class__.mro() - for m in cls.__dict__ - if m[0] != '_' and m not in self._member_map_ - ] + m + for cls in self.__class__.mro() + for m in cls.__dict__ + if m[0] != '_' and m not in self._member_map_ + ] return (['__class__', '__doc__', '__module__', ] + added_behavior) temp_enum_dict['__dir__'] = __dir__ del __dir__ + def __format__(self, format_spec): # mixed-in Enums should use the mixed-in type's __format__, otherwise # we can get strange results with the Enum name showing up instead of @@ -710,7 +715,7 @@ del __format__ if pyver < 2.6: def __cmp__(self, other): - if type(other) is self.__class__: + if isinstance(other, self.__class__): if self is other: return 0 return -1 @@ -743,24 +748,27 @@ else: def __eq__(self, other): - if type(other) is self.__class__: + if isinstance(other, self.__class__): return self is other return NotImplemented temp_enum_dict['__eq__'] = __eq__ del __eq__ + def __ne__(self, other): - if type(other) is self.__class__: + if isinstance(other, self.__class__): return self is not other return NotImplemented temp_enum_dict['__ne__'] = __ne__ del __ne__ + def __hash__(self): return hash(self._name_) temp_enum_dict['__hash__'] = __hash__ del __hash__ + def __reduce_ex__(self, proto): return self.__class__, (self._value_, ) temp_enum_dict['__reduce_ex__'] = __reduce_ex__ @@ -773,18 +781,21 @@ del __reduce_ex__ # members are not set directly on the enum class -- __getattr__ is # used to look them up. + @_RouteClassAttributeToGetattr def name(self): return self._name_ temp_enum_dict['name'] = name del name + @_RouteClassAttributeToGetattr def value(self): return self._value_ temp_enum_dict['value'] = value del value + @classmethod def _convert(cls, name, module, filter, source=None): """ @@ -815,12 +826,15 @@ del temp_enum_dict # Enum has now been created ########################### + class IntEnum(int, Enum): """Enum where members are also (and must be) ints""" + def _reduce_ex_by_name(self, proto): return self.name + def unique(enumeration): """Class decorator that ensures only unique members exist in an enumeration.""" duplicates = [] @@ -829,9 +843,9 @@ def unique(enumeration): duplicates.append((name, member.name)) if duplicates: duplicate_names = ', '.join( - ["%s -> %s" % (alias, name) for (alias, name) in duplicates] - ) + ["%s -> %s" % (alias, name) for (alias, name) in duplicates] + ) raise ValueError('duplicate names found in %r: %s' % - (enumeration, duplicate_names) - ) - return enumeration \ No newline at end of file + (enumeration, duplicate_names) + ) + return enumeration diff --git a/graphene/utils/get_fields.py b/graphene/utils/get_fields.py index d8da6916..29eb0397 100644 --- a/graphene/utils/get_fields.py +++ b/graphene/utils/get_fields.py @@ -1,9 +1,9 @@ from collections import OrderedDict -from .get_graphql_type import get_graphql_type -from .is_graphene_type import is_graphene_type from ..types.field import Field, InputField from ..types.unmountedtype import UnmountedType +from .get_graphql_type import get_graphql_type +from .is_graphene_type import is_graphene_type def get_fields_from_attrs(in_type, attrs): diff --git a/graphene/utils/tests/test_get_fields.py b/graphene/utils/tests/test_get_fields.py index fbb3d0f4..342b582c 100644 --- a/graphene/utils/tests/test_get_fields.py +++ b/graphene/utils/tests/test_get_fields.py @@ -1,8 +1,10 @@ from collections import OrderedDict -from graphql import GraphQLField, GraphQLString, GraphQLInterfaceType, GraphQLInt, GraphQLFloat -from ..get_fields import get_fields_from_attrs, get_fields_from_types -from ...types import Field, String, Argument, ObjectType +from graphql import (GraphQLField, GraphQLFloat, GraphQLInt, + GraphQLInterfaceType, GraphQLString) + +from ...types import Argument, Field, ObjectType, String +from ..get_fields import get_fields_from_attrs, get_fields_from_types def test_get_fields_from_attrs(): diff --git a/graphene/utils/tests/test_get_graphql_type.py b/graphene/utils/tests/test_get_graphql_type.py index 75b1232f..66b0d4f5 100644 --- a/graphene/utils/tests/test_get_graphql_type.py +++ b/graphene/utils/tests/test_get_graphql_type.py @@ -1,11 +1,10 @@ import pytest -from graphql.type.definition import is_type -from graphql import GraphQLObjectType, GraphQLField, GraphQLString - from graphene.types import ObjectType -from ..get_graphql_type import get_graphql_type +from graphql import GraphQLField, GraphQLObjectType, GraphQLString +from graphql.type.definition import is_type +from ..get_graphql_type import get_graphql_type MyGraphQLType = GraphQLObjectType('MyGraphQLType', fields={ 'field': GraphQLField(GraphQLString) @@ -21,6 +20,7 @@ def test_get_graphql_type_graphene(): def test_get_graphql_type_custom_graphene_type(): class MyGrapheneType(ObjectType): + class Meta: graphql_type = MyGraphQLType diff --git a/graphene/utils/tests/test_is_graphene_type.py b/graphene/utils/tests/test_is_graphene_type.py index 563ee8b4..4311a3af 100644 --- a/graphene/utils/tests/test_is_graphene_type.py +++ b/graphene/utils/tests/test_is_graphene_type.py @@ -1,9 +1,8 @@ -from graphql import GraphQLObjectType, GraphQLField, GraphQLString +from graphene.types import ObjectType +from graphql import GraphQLField, GraphQLObjectType, GraphQLString from ..is_graphene_type import is_graphene_type -from graphene.types import ObjectType - def test_is_graphene_type_objecttype(): class MyObjectType(ObjectType):