diff --git a/graphene/relay/mutation.py b/graphene/relay/mutation.py index c236c49b..f5fbec83 100644 --- a/graphene/relay/mutation.py +++ b/graphene/relay/mutation.py @@ -40,8 +40,10 @@ 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__, diff --git a/graphene/types/argument.py b/graphene/types/argument.py index 23146e17..a3febbb2 100644 --- a/graphene/types/argument.py +++ b/graphene/types/argument.py @@ -1,4 +1,3 @@ -import copy import inspect from collections import OrderedDict from itertools import chain @@ -39,6 +38,16 @@ class Argument(GraphQLArgument, OrderedType): def type(self, type): self._type = type + @classmethod + def copy_from(cls, argument): + return cls( + type=argument.type, + default_value=argument.default_value, + description=argument.description, + name=argument.name, + _creation_counter=argument.creation_counter if isinstance(argument, Argument) else None, + ) + def to_arguments(*args, **extra): from .unmountedtype import UnmountedType @@ -53,7 +62,7 @@ def to_arguments(*args, **extra): if not isinstance(arg, GraphQLArgument): raise ValueError('Unknown argument "{}".'.format(default_name)) - arg = copy.copy(arg) + arg = Argument.copy_from(arg) arg.name = arg.name or default_name assert arg.name, 'All arguments must have a name.' assert arg.name not in arguments_names, 'More than one Argument have same name "{}".'.format(arg.name) diff --git a/graphene/types/enum.py b/graphene/types/enum.py index aa02fbde..0c7e3c2f 100644 --- a/graphene/types/enum.py +++ b/graphene/types/enum.py @@ -29,7 +29,7 @@ def values_from_enum(enum): class EnumTypeMeta(type): def __new__(cls, name, bases, attrs): - super_new = super(EnumTypeMeta, cls).__new__ + super_new = type.__new__ # Also ensure initialization is only performed for subclasses of Model # (excluding Model class itself). @@ -43,11 +43,12 @@ class EnumTypeMeta(type): enum=None, graphql_type=None ) - if not options.enum: - options.enum = type(cls.__name__, (PyEnum,), attrs) + options.enum = PyEnum(cls.__name__, attrs) - cls = super_new(cls, name, bases, dict(attrs, _meta=options, **options.enum.__members__)) + new_attrs = OrderedDict(attrs, _meta=options, **options.enum.__members__) + + cls = super_new(cls, name, bases, new_attrs) if not options.graphql_type: values = values_from_enum(options.enum) diff --git a/graphene/types/tests/test_enum.py b/graphene/types/tests/test_enum.py index 4e26e12f..4813d902 100644 --- a/graphene/types/tests/test_enum.py +++ b/graphene/types/tests/test_enum.py @@ -1,4 +1,4 @@ -from graphql.type import GraphQLEnumType, GraphQLEnumValue +from graphql.type import GraphQLEnumType from ..argument import Argument from ..enum import Enum, PyEnum @@ -13,10 +13,10 @@ def test_enum_construction(): assert isinstance(RGB._meta.graphql_type, GraphQLEnumType) values = RGB._meta.graphql_type.get_values() - assert values == [ - GraphQLEnumValue(name='RED', value=1), - GraphQLEnumValue(name='GREEN', value=2), - GraphQLEnumValue(name='BLUE', value=3), + assert sorted([v.name for v in values]) == [ + 'BLUE', + 'GREEN', + 'RED' ] assert isinstance(RGB(name='field_name').as_field(), Field) assert isinstance(RGB(name='field_name').as_argument(), Argument) @@ -27,10 +27,10 @@ def test_enum_instance_construction(): assert isinstance(RGB._meta.graphql_type, GraphQLEnumType) values = RGB._meta.graphql_type.get_values() - assert values == [ - GraphQLEnumValue(name='RED', value=1), - GraphQLEnumValue(name='GREEN', value=2), - GraphQLEnumValue(name='BLUE', value=3), + assert sorted([v.name for v in values]) == [ + 'BLUE', + 'GREEN', + 'RED' ] assert isinstance(RGB(name='field_name').as_field(), Field) assert isinstance(RGB(name='field_name').as_argument(), Argument) @@ -42,10 +42,10 @@ def test_enum_from_builtin_enum(): RGB = Enum.from_enum(PyRGB) assert isinstance(RGB._meta.graphql_type, GraphQLEnumType) values = RGB._meta.graphql_type.get_values() - assert values == [ - GraphQLEnumValue(name='RED', value=1), - GraphQLEnumValue(name='GREEN', value=2), - GraphQLEnumValue(name='BLUE', value=3), + assert sorted([v.name for v in values]) == [ + 'BLUE', + 'GREEN', + 'RED' ] assert isinstance(RGB(name='field_name').as_field(), Field) assert isinstance(RGB(name='field_name').as_argument(), Argument) diff --git a/graphene/types/tests/test_mutation.py b/graphene/types/tests/test_mutation.py index fa77f662..49855746 100644 --- a/graphene/types/tests/test_mutation.py +++ b/graphene/types/tests/test_mutation.py @@ -76,158 +76,3 @@ def test_mutation_raises_exception_if_no_mutate(): pass assert "All mutations must define a mutate method in it" == str(excinfo.value) - -# def test_objecttype_inheritance(): -# class MyInheritedObjectType(ObjectType): -# inherited = Field(GraphQLString) - -# class MyObjectType(MyInheritedObjectType): -# field = Field(GraphQLString) - -# graphql_type = MyObjectType._meta.graphql_type -# fields = graphql_type.get_fields() -# assert 'field' in fields -# assert 'inherited' in fields -# assert fields['field'] > fields['inherited'] - - -# def test_objecttype_as_container_only_args(): -# container = Container("1", "2") -# assert container.field1 == "1" -# assert container.field2 == "2" - - -# def test_objecttype_as_container_args_kwargs(): -# container = Container("1", field2="2") -# assert container.field1 == "1" -# assert container.field2 == "2" - - -# def test_objecttype_as_container_few_kwargs(): -# container = Container(field2="2") -# assert container.field2 == "2" - - -# def test_objecttype_as_container_all_kwargs(): -# container = Container(field1="1", field2="2") -# assert container.field1 == "1" -# assert container.field2 == "2" - - -# def test_objecttype_as_container_extra_args(): -# with pytest.raises(IndexError) as excinfo: -# Container("1", "2", "3") - -# assert "Number of args exceeds number of fields" == str(excinfo.value) - - -# def test_objecttype_as_container_invalid_kwargs(): -# with pytest.raises(TypeError) as excinfo: -# Container(unexisting_field="3") - -# assert "'unexisting_field' is an invalid keyword argument for this function" == str(excinfo.value) - - -# def test_objecttype_reuse_graphql_type(): -# MyGraphQLType = GraphQLObjectType('MyGraphQLType', fields={ -# 'field': GraphQLField(GraphQLString) -# }) - -# class GrapheneObjectType(ObjectType): -# class Meta: -# graphql_type = MyGraphQLType - -# graphql_type = GrapheneObjectType._meta.graphql_type -# assert graphql_type == MyGraphQLType -# instance = GrapheneObjectType(field="A") -# assert instance.field == "A" - - -# def test_objecttype_add_fields_in_reused_graphql_type(): -# MyGraphQLType = GraphQLObjectType('MyGraphQLType', fields={ -# 'field': GraphQLField(GraphQLString) -# }) - -# with pytest.raises(AssertionError) as excinfo: -# class GrapheneObjectType(ObjectType): -# field = Field(GraphQLString) - -# class Meta: -# graphql_type = MyGraphQLType - -# assert """Field "MyGraphQLType.field" can only be mounted in ObjectType or Interface, received GrapheneObjectType.""" == str(excinfo.value) - - -# def test_objecttype_graphql_interface(): -# MyInterface = GraphQLInterfaceType('MyInterface', fields={ -# 'field': GraphQLField(GraphQLString) -# }) - -# class GrapheneObjectType(ObjectType): -# class Meta: -# interfaces = [MyInterface] - -# graphql_type = GrapheneObjectType._meta.graphql_type -# assert graphql_type.get_interfaces() == (MyInterface, ) -# # assert graphql_type.is_type_of(MyInterface, None, None) -# fields = graphql_type.get_fields() -# assert 'field' in fields - - -# def test_objecttype_graphene_interface(): -# class GrapheneInterface(Interface): -# field = Field(GraphQLString) - -# class GrapheneObjectType(ObjectType): -# class Meta: -# interfaces = [GrapheneInterface] - -# graphql_type = GrapheneObjectType._meta.graphql_type -# assert graphql_type.get_interfaces() == (GrapheneInterface._meta.graphql_type, ) -# assert graphql_type.is_type_of(GrapheneObjectType(), None, None) -# fields = graphql_type.get_fields() -# assert 'field' in fields - - -# def test_objecttype_graphene_interface_extended(): -# class GrapheneInterface(Interface): -# field = Field(GraphQLString) - -# class GrapheneObjectType(ObjectType): -# class Meta: -# interfaces = [GrapheneInterface] - -# schema = Schema(query=GrapheneObjectType) -# assert str(schema) == """ -# schema { -# query: GrapheneObjectType -# } - -# interface GrapheneInterface { -# field: String -# } - -# type GrapheneObjectType implements GrapheneInterface { -# field: String -# } -# """.lstrip() -# GrapheneInterface._meta.graphql_type.add_field(Field(String, name='dynamic')) -# # GrapheneObjectType._meta.graphql_type._field_map = None -# assert GrapheneInterface._meta.graphql_type.get_fields().keys() == ['field', 'dynamic'] -# assert GrapheneObjectType._meta.graphql_type.get_fields().keys() == ['field', 'dynamic'] -# schema.rebuild() -# assert str(schema) == """ -# schema { -# query: GrapheneObjectType -# } - -# interface GrapheneInterface { -# field: String -# dynamic: String -# } - -# type GrapheneObjectType implements GrapheneInterface { -# field: String -# dynamic: String -# } -# """.lstrip() diff --git a/graphene/types/tests/test_scalars.py b/graphene/types/tests/test_scalars.py index 4caa8363..7ff232a2 100644 --- a/graphene/types/tests/test_scalars.py +++ b/graphene/types/tests/test_scalars.py @@ -106,7 +106,7 @@ def test_custom_scalar_empty(): @pytest.mark.parametrize("scalar_class", (DatetimeScalar, DatetimeScalarGraphQL)) -def test_custom_scalar_empty(scalar_class): +def test_custom_scalar_query(scalar_class): class Query(ObjectType): datetime = scalar_class(_in=scalar_class(name='in')) diff --git a/setup.cfg b/setup.cfg index fa7b331c..89077559 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,5 +1,5 @@ [flake8] -exclude = setup.py,docs/* +exclude = setup.py,docs/*,*/examples/* max-line-length = 120 [coverage:run]