Fixed Python3 issues

This commit is contained in:
Syrus Akbary 2016-06-15 21:53:33 -07:00
parent 6e38e48faf
commit dd377c7da3
7 changed files with 35 additions and 178 deletions

View File

@ -40,8 +40,10 @@ class ClientIDMutationMeta(MutationMeta):
output_fields = copy_fields(Field, get_fields(ObjectType, attrs, bases)) output_fields = copy_fields(Field, get_fields(ObjectType, attrs, bases))
mutate_and_get_payload = getattr(cls, 'mutate_and_get_payload', None) 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( assert mutate_and_get_payload, (
cls.__name__) "{}.mutate_and_get_payload method is required"
" in a ClientIDMutation ObjectType.".format(cls.__name__)
)
field = mutation_with_client_mutation_id( field = mutation_with_client_mutation_id(
name=options.name or cls.__name__, name=options.name or cls.__name__,

View File

@ -1,4 +1,3 @@
import copy
import inspect import inspect
from collections import OrderedDict from collections import OrderedDict
from itertools import chain from itertools import chain
@ -39,6 +38,16 @@ class Argument(GraphQLArgument, OrderedType):
def type(self, type): def type(self, type):
self._type = 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): def to_arguments(*args, **extra):
from .unmountedtype import UnmountedType from .unmountedtype import UnmountedType
@ -53,7 +62,7 @@ def to_arguments(*args, **extra):
if not isinstance(arg, GraphQLArgument): if not isinstance(arg, GraphQLArgument):
raise ValueError('Unknown argument "{}".'.format(default_name)) raise ValueError('Unknown argument "{}".'.format(default_name))
arg = copy.copy(arg) arg = Argument.copy_from(arg)
arg.name = arg.name or default_name arg.name = arg.name or default_name
assert arg.name, 'All arguments must have a 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) assert arg.name not in arguments_names, 'More than one Argument have same name "{}".'.format(arg.name)

View File

@ -29,7 +29,7 @@ def values_from_enum(enum):
class EnumTypeMeta(type): class EnumTypeMeta(type):
def __new__(cls, name, bases, attrs): 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 # Also ensure initialization is only performed for subclasses of Model
# (excluding Model class itself). # (excluding Model class itself).
@ -43,11 +43,12 @@ class EnumTypeMeta(type):
enum=None, enum=None,
graphql_type=None graphql_type=None
) )
if not options.enum: 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: if not options.graphql_type:
values = values_from_enum(options.enum) values = values_from_enum(options.enum)

View File

@ -1,4 +1,4 @@
from graphql.type import GraphQLEnumType, GraphQLEnumValue from graphql.type import GraphQLEnumType
from ..argument import Argument from ..argument import Argument
from ..enum import Enum, PyEnum from ..enum import Enum, PyEnum
@ -13,10 +13,10 @@ def test_enum_construction():
assert isinstance(RGB._meta.graphql_type, GraphQLEnumType) assert isinstance(RGB._meta.graphql_type, GraphQLEnumType)
values = RGB._meta.graphql_type.get_values() values = RGB._meta.graphql_type.get_values()
assert values == [ assert sorted([v.name for v in values]) == [
GraphQLEnumValue(name='RED', value=1), 'BLUE',
GraphQLEnumValue(name='GREEN', value=2), 'GREEN',
GraphQLEnumValue(name='BLUE', value=3), 'RED'
] ]
assert isinstance(RGB(name='field_name').as_field(), Field) assert isinstance(RGB(name='field_name').as_field(), Field)
assert isinstance(RGB(name='field_name').as_argument(), Argument) 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) assert isinstance(RGB._meta.graphql_type, GraphQLEnumType)
values = RGB._meta.graphql_type.get_values() values = RGB._meta.graphql_type.get_values()
assert values == [ assert sorted([v.name for v in values]) == [
GraphQLEnumValue(name='RED', value=1), 'BLUE',
GraphQLEnumValue(name='GREEN', value=2), 'GREEN',
GraphQLEnumValue(name='BLUE', value=3), 'RED'
] ]
assert isinstance(RGB(name='field_name').as_field(), Field) assert isinstance(RGB(name='field_name').as_field(), Field)
assert isinstance(RGB(name='field_name').as_argument(), Argument) assert isinstance(RGB(name='field_name').as_argument(), Argument)
@ -42,10 +42,10 @@ def test_enum_from_builtin_enum():
RGB = Enum.from_enum(PyRGB) RGB = Enum.from_enum(PyRGB)
assert isinstance(RGB._meta.graphql_type, GraphQLEnumType) assert isinstance(RGB._meta.graphql_type, GraphQLEnumType)
values = RGB._meta.graphql_type.get_values() values = RGB._meta.graphql_type.get_values()
assert values == [ assert sorted([v.name for v in values]) == [
GraphQLEnumValue(name='RED', value=1), 'BLUE',
GraphQLEnumValue(name='GREEN', value=2), 'GREEN',
GraphQLEnumValue(name='BLUE', value=3), 'RED'
] ]
assert isinstance(RGB(name='field_name').as_field(), Field) assert isinstance(RGB(name='field_name').as_field(), Field)
assert isinstance(RGB(name='field_name').as_argument(), Argument) assert isinstance(RGB(name='field_name').as_argument(), Argument)

View File

@ -76,158 +76,3 @@ def test_mutation_raises_exception_if_no_mutate():
pass pass
assert "All mutations must define a mutate method in it" == str(excinfo.value) 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()

View File

@ -106,7 +106,7 @@ def test_custom_scalar_empty():
@pytest.mark.parametrize("scalar_class", (DatetimeScalar, DatetimeScalarGraphQL)) @pytest.mark.parametrize("scalar_class", (DatetimeScalar, DatetimeScalarGraphQL))
def test_custom_scalar_empty(scalar_class): def test_custom_scalar_query(scalar_class):
class Query(ObjectType): class Query(ObjectType):
datetime = scalar_class(_in=scalar_class(name='in')) datetime = scalar_class(_in=scalar_class(name='in'))

View File

@ -1,5 +1,5 @@
[flake8] [flake8]
exclude = setup.py,docs/* exclude = setup.py,docs/*,*/examples/*
max-line-length = 120 max-line-length = 120
[coverage:run] [coverage:run]