From d4498c323265e9b08d28c952d151166dfe017cb0 Mon Sep 17 00:00:00 2001 From: Kacppian Date: Tue, 17 Jul 2018 22:58:06 +0530 Subject: [PATCH] reverting failing tests --- graphene/pyutils/version.py | 16 +++--- graphene/relay/connection.py | 31 +++++------ graphene/relay/mutation.py | 4 +- graphene/relay/node.py | 19 +++---- graphene/types/argument.py | 9 ++- graphene/types/base.py | 7 +-- graphene/types/datetime.py | 22 +++----- graphene/types/dynamic.py | 10 ++-- graphene/types/field.py | 29 +++++----- graphene/types/mountedtype.py | 9 ++- graphene/types/objecttype.py | 20 +++---- graphene/types/resolver.py | 9 +-- graphene/types/schema.py | 40 ++++++-------- graphene/types/typemap.py | 83 ++++++++++++++-------------- graphene/types/union.py | 11 ++-- graphene/types/uuid.py | 7 +-- graphene/utils/annotate.py | 13 ++--- graphene/utils/subclass_with_meta.py | 13 +++-- 18 files changed, 155 insertions(+), 197 deletions(-) diff --git a/graphene/pyutils/version.py b/graphene/pyutils/version.py index a922659b..9d33209f 100644 --- a/graphene/pyutils/version.py +++ b/graphene/pyutils/version.py @@ -3,7 +3,10 @@ from __future__ import unicode_literals import datetime import os import subprocess +<<<<<<< HEAD from .comparison_helper import raise_assertion_if +======= +>>>>>>> parent of 41dcbdc... DRY-ed up def get_version(version=None): @@ -45,14 +48,11 @@ def get_complete_version(version=None): if version is None: from graphene import VERSION as version else: - raise_assertion_if( - condition=len(version) is not 5, - message="Version needs to be 5" - ) - raise_assertion_if( - condition=version[3] not in ("alpha", "beta", "rc", "final"), - message="Release version is unkown" - ) + 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") + return version diff --git a/graphene/relay/connection.py b/graphene/relay/connection.py index 01dacab3..3f58fdbf 100644 --- a/graphene/relay/connection.py +++ b/graphene/relay/connection.py @@ -9,7 +9,6 @@ from ..types import Boolean, Enum, Int, Interface, List, NonNull, Scalar, String from ..types.field import Field from ..types.objecttype import ObjectType, ObjectTypeOptions from .node import is_node -from ..utils.comparison_helper import raise_assertion_if class PageInfo(ObjectType): @@ -47,20 +46,16 @@ class Connection(ObjectType): @classmethod def __init_subclass_with_meta__(cls, node=None, name=None, **options): _meta = ConnectionOptions(cls) - - error_message = "You have to provide a node in {}.Meta".format(cls.__name__) - raise_assertion_if( - condition=not node, - message=error_message + if not node: + raise AssertionError( + "You have to provide a node in {}.Meta".format(cls.__name__) ) - error_message = 'Received incompatible node "{}" for Connection {}.'.format( - node, cls.__name__ - ) - condition = not issubclass(node, (Scalar, Enum, ObjectType, Interface, Union, NonNull)) - raise_assertion_if( - condition=condition, - message=error_message + if not issubclass(node, (Scalar, Enum, ObjectType, Interface, Union, NonNull)): + raise AssertionError( + 'Received incompatible node "{}" for Connection {}.'.format( + node, cls.__name__ + ) ) base_name = re.sub("Connection$", "", name or cls.__name__) or node._meta.name @@ -117,11 +112,11 @@ class IterableConnectionField(Field): "Read more: https://github.com/graphql-python/graphene/blob/v2.0.0/UPGRADE-v2.0.md#node-connections" ) - error_message = '{} type have to be a subclass of Connection. Received "{}".' - .format(self.__class__.__name__, connection_type) - raise_assertion_if( - condition= not issubclass(connection_type, Connection), - message=error_message + if not issubclass(connection_type, Connection): + raise AssertionError( + '{} type have to be a subclass of Connection. Received "{}".'.format( + self.__class__.__name__, connection_type + ) ) return type diff --git a/graphene/relay/mutation.py b/graphene/relay/mutation.py index bad16785..211ee44f 100644 --- a/graphene/relay/mutation.py +++ b/graphene/relay/mutation.py @@ -5,7 +5,6 @@ from promise import Promise, is_thenable from ..types import Field, InputObjectType, String from ..types.mutation import Mutation -from ..utils.comparison_helper import raise_assertion_if class ClientIDMutation(Mutation): @@ -19,7 +18,8 @@ class ClientIDMutation(Mutation): input_class = getattr(cls, "Input", None) base_name = re.sub("Payload$", "", name or cls.__name__) - raise_assertion_if(condition=output, message="Can't specify any output") + if output: + raise AssertionError("Can't specify any output") if arguments: raise AssertionError("Can't specify any arguments") diff --git a/graphene/relay/node.py b/graphene/relay/node.py index 5dd494f7..44020ae3 100644 --- a/graphene/relay/node.py +++ b/graphene/relay/node.py @@ -7,9 +7,6 @@ from graphql_relay import from_global_id, to_global_id from ..types import ID, Field, Interface, ObjectType from ..types.interface import InterfaceOptions from ..types.utils import get_type -from ..utils.comparison_helper import raise_assertion_if - - def is_node(objecttype): @@ -52,10 +49,8 @@ class GlobalID(Field): class NodeField(Field): def __init__(self, node, type=False, deprecation_reason=None, name=None, **kwargs): - raise_assertion_if( - condition= not issubclass(node, Node), - message="NodeField can only operate in Nodes" - ) + if not issubclass(node, Node): + raise AssertionError("NodeField can only operate in Nodes") self.node_type = node self.field_type = type @@ -103,10 +98,12 @@ class Node(AbstractNode): except Exception: return None - raise_assertion_if( - condition=only_type and (graphene_type is not only_type), - message="Must receive a {} id.".format(only_type._meta.name) - ) + if only_type: + if graphene_type != only_type: + raise AssertionError( + "Must receive a {} id.".format(only_type._meta.name) + ) + # We make sure the ObjectType implements the "Node" interface if cls not in graphene_type._meta.interfaces: return None diff --git a/graphene/types/argument.py b/graphene/types/argument.py index 46ff62e7..97ff3b27 100644 --- a/graphene/types/argument.py +++ b/graphene/types/argument.py @@ -5,7 +5,6 @@ from .dynamic import Dynamic from .mountedtype import MountedType from .structures import NonNull from .utils import get_type -from ..utils.comparison_helper import raise_assertion_if class Argument(MountedType): def __init__( @@ -73,10 +72,10 @@ def to_arguments(args, extra_args=None): raise ValueError('Unknown argument "{}".'.format(default_name)) arg_name = default_name or arg.name - raise_assertion_if( - condition=arg_name in arguments, - message='More than one Argument have same name "{}".'.format(arg_name) - ) + if arg_name in arguments: + raise AssertionError( + 'More than one Argument have same name "{}".'.format(arg_name) + ) arguments[arg_name] = arg return arguments diff --git a/graphene/types/base.py b/graphene/types/base.py index 86321a60..6c71e599 100644 --- a/graphene/types/base.py +++ b/graphene/types/base.py @@ -1,6 +1,5 @@ from ..utils.subclass_with_meta import SubclassWithMeta from ..utils.trim_docstring import trim_docstring -from ..utils.comparison_helper import raise_assertion_if class BaseOptions(object): name = None # type: str @@ -31,10 +30,8 @@ class BaseType(SubclassWithMeta): @classmethod def __init_subclass_with_meta__(cls, name=None, description=None, _meta=None): - raise_assertion_if( - condition="_meta" in cls.__dict__, - message="Can't assign meta directly" - ) + if "_meta" in cls.__dict__: + raise AssertionError("Can't assign meta directly") if not _meta: return _meta.name = name or cls.__name__ diff --git a/graphene/types/datetime.py b/graphene/types/datetime.py index 03e1bf17..724bf31f 100644 --- a/graphene/types/datetime.py +++ b/graphene/types/datetime.py @@ -6,7 +6,6 @@ from aniso8601 import parse_date, parse_datetime, parse_time from graphql.language import ast from .scalars import Scalar -from ..utils.comparison_helper import raise_assertion_if class Date(Scalar): """ @@ -19,10 +18,8 @@ class Date(Scalar): def serialize(date): if isinstance(date, datetime.datetime): date = date.date() - raise_assertion_if( - condition=not isinstance(date, datetime.date), - message='Received not compatible date "{}"'.format(repr(date)) - ) + if not isinstance(date, datetime.date): + raise AssertionError('Received not compatible date "{}"'.format(repr(date))) return date.isoformat() @classmethod @@ -47,11 +44,10 @@ class DateTime(Scalar): @staticmethod def serialize(dt): - - raise_assertion_if( - condition=not isinstance(dt, (datetime.datetime, datetime.date)), - message='Received not compatible datetime "{}"'.format(repr(dt)) - ) + if not isinstance(dt, (datetime.datetime, datetime.date)): + raise AssertionError( + 'Received not compatible datetime "{}"'.format(repr(dt)) + ) return dt.isoformat() @classmethod @@ -76,10 +72,8 @@ class Time(Scalar): @staticmethod def serialize(time): - raise_assertion_if( - condition= not isinstance(time, datetime.time), - message='Received not compatible time "{}"'.format(repr(time)) - ) + if not isinstance(time, datetime.time): + raise AssertionError('Received not compatible time "{}"'.format(repr(time))) return time.isoformat() @classmethod diff --git a/graphene/types/dynamic.py b/graphene/types/dynamic.py index dd279600..d7747644 100644 --- a/graphene/types/dynamic.py +++ b/graphene/types/dynamic.py @@ -3,8 +3,6 @@ from functools import partial from .mountedtype import MountedType -from ..utils.comparison_helper import raise_assertion_if - class Dynamic(MountedType): """ @@ -14,10 +12,10 @@ class Dynamic(MountedType): def __init__(self, type, with_schema=False, _creation_counter=None): super(Dynamic, self).__init__(_creation_counter=_creation_counter) - raise_assertion_if( - condition=not (inspect.isfunction(type) or isinstance(type, partial)), - message="type is expected to be a function or an instance of partial" - ) + if not (inspect.isfunction(type) or isinstance(type, partial)): + raise AssertionError( + "type is expected to be a function or an instance of partial" + ) self.type = type self.with_schema = with_schema diff --git a/graphene/types/field.py b/graphene/types/field.py index 683c70d1..a42d131a 100644 --- a/graphene/types/field.py +++ b/graphene/types/field.py @@ -7,7 +7,6 @@ from .mountedtype import MountedType from .structures import NonNull from .unmountedtype import UnmountedType from .utils import get_type -from ..utils.comparison_helper import raise_assertion_if base_type = type @@ -36,20 +35,20 @@ class Field(MountedType): ): super(Field, self).__init__(_creation_counter=_creation_counter) - raise_assertion_if( - condition=args and not isinstance(args, Mapping), - message='Arguments in a field have to be a mapping, received "{}".'.format(args) - ) - - raise_assertion_if( - condition=source and resolver, - message="A Field cannot have a source and a resolver in at the same time." - ) - - raise_assertion_if( - condition=callable(default_value), - message='The default value can not be a function but received "{}".'.format(base_type(default_value) - ) + if args and not isinstance(args, Mapping): + raise AssertionError( + '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." + ) + if callable(default_value): + raise AssertionError( + 'The default value can not be a function but received "{}".'.format( + base_type(default_value) + ) + ) if required: type = NonNull(type) diff --git a/graphene/types/mountedtype.py b/graphene/types/mountedtype.py index 49b887cf..fc4efb85 100644 --- a/graphene/types/mountedtype.py +++ b/graphene/types/mountedtype.py @@ -1,7 +1,6 @@ from ..utils.orderedtype import OrderedType from .unmountedtype import UnmountedType -from ..utils.comparison_helper import raise_assertion_if class MountedType(OrderedType): @classmethod @@ -9,10 +8,10 @@ class MountedType(OrderedType): """ Mount the UnmountedType instance """ - raise_assertion_if( - condition=not isinstance(unmounted, UnmountedType), - message="{} can't mount {}".format(cls.__name__, repr(unmounted) - ) + if not isinstance(unmounted, UnmountedType): + raise AssertionError( + "{} can't mount {}".format(cls.__name__, repr(unmounted)) + ) return cls( unmounted.get_type(), diff --git a/graphene/types/objecttype.py b/graphene/types/objecttype.py index 847c83f6..2be97358 100644 --- a/graphene/types/objecttype.py +++ b/graphene/types/objecttype.py @@ -5,8 +5,6 @@ from .field import Field from .interface import Interface from .utils import yank_fields_from_attrs -from ..utils.comparison_helper import raise_assertion_if - # For static type checking with Mypy MYPY = False if MYPY: @@ -49,15 +47,15 @@ class ObjectType(BaseType): for base in reversed(cls.__mro__): fields.update(yank_fields_from_attrs(base.__dict__, _as=Field)) - error_message = """ - {name}.Meta.possible_types will cause type collision with {name}.is_type_of. - Please use one or other. - """.format(name=cls.__name__) - - raise_assertion_if( - condition=possible_types and cls.is_type_of, - message=error_message - ) + 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__ + ) + ) if _meta.fields: _meta.fields.update(fields) diff --git a/graphene/types/resolver.py b/graphene/types/resolver.py index a050b17e..d9151b2a 100644 --- a/graphene/types/resolver.py +++ b/graphene/types/resolver.py @@ -1,5 +1,3 @@ -from ..utils.comparison_helper import raise_assertion_if - def attr_resolver(attname, default_value, root, info, **args): return getattr(root, attname, default_value) @@ -13,11 +11,8 @@ default_resolver = attr_resolver def set_default_resolver(resolver): global default_resolver - - raise_assertion_if( - condition=not callable(resolver), - message="Received non-callable resolver." - ) + if not callable(resolver): + raise AssertionError("Received non-callable resolver.") default_resolver = resolver diff --git a/graphene/types/schema.py b/graphene/types/schema.py index b106ad36..e54a13ca 100644 --- a/graphene/types/schema.py +++ b/graphene/types/schema.py @@ -13,17 +13,14 @@ from graphql.utils.schema_printer import print_schema from .definitions import GrapheneGraphQLType from .objecttype import ObjectType from .typemap import TypeMap, is_graphene_type -from ..utils.comparison_helper import raise_assertion_if def assert_valid_root_type(_type): if _type is None: return is_graphene_objecttype = inspect.isclass(_type) and issubclass(_type, ObjectType) is_graphql_objecttype = isinstance(_type, GraphQLObjectType) - raise_assertion_if( - condition=not (is_graphene_objecttype or is_graphql_objecttype), - message="Type {} is not a valid ObjectType.".format(_type) - ) + if not (is_graphene_objecttype or is_graphql_objecttype): + raise AssertionError("Type {} is not a valid ObjectType.".format(_type)) class Schema(GraphQLSchema): @@ -54,10 +51,12 @@ class Schema(GraphQLSchema): if directives is None: directives = [GraphQLIncludeDirective, GraphQLSkipDirective] - raise_assertion_if( - condition=not all(isinstance(d, GraphQLDirective) for d in directives), - message="Schema directives must be List[GraphQLDirective] if provided but got: {}.".format(directives) - ) + if not all(isinstance(d, GraphQLDirective) for d in directives): + raise AssertionError( + "Schema directives must be List[GraphQLDirective] if provided but got: {}.".format( + directives + ) + ) self._directives = directives self.build_typemap() @@ -91,19 +90,16 @@ class Schema(GraphQLSchema): return _type if is_graphene_type(_type): graphql_type = self.get_type(_type._meta.name) - - raise_assertion_if( - condition=not graphql_type, - message="Type {} not found in this schema.".format(_type._meta.name) - ) - - error_message = "The type {} does not match with the associated graphene type {}." - .format(_type, graphql_type.graphene_type) - raise_assertion_if( - condition=graphql_type.graphene_type is not _type, - message=error_message - ) - + if not graphql_type: + raise AssertionError( + "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 + ) + ) return graphql_type raise Exception("{} is not a valid GraphQL type.".format(_type)) diff --git a/graphene/types/typemap.py b/graphene/types/typemap.py index 72044877..f34cd50e 100644 --- a/graphene/types/typemap.py +++ b/graphene/types/typemap.py @@ -40,7 +40,6 @@ from .scalars import ID, Boolean, Float, Int, Scalar, String from .structures import List, NonNull from .union import Union from .utils import get_field_as -from ..utils.comparison_helper import raise_assertion_if def is_graphene_type(_type): @@ -61,18 +60,16 @@ def resolve_type(resolve_type_func, map, type_name, root, info): if inspect.isclass(_type) and issubclass(_type, ObjectType): graphql_type = map.get(_type._meta.name) - raise_assertion_if( - condition=not graphql_type, - message="Can't find type {} in schema".format(_type._meta.name) - ) - - error_message = "The type {} does not match with the associated graphene type {}." - .format( _type, graphql_type.graphene_type) - raise_assertion_if( - condition=graphql_type.graphene_type is not _type, - message=error_message - ) - + if not graphql_type: + raise AssertionError( + "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 + ) + ) return graphql_type return _type @@ -103,12 +100,12 @@ class TypeMap(GraphQLTypeMap): if type._meta.name in map: _type = map[type._meta.name] if isinstance(_type, GrapheneGraphQLType): - error_message = "Found different types with the same name in the schema: {}, {}." - .format(_type.graphene_type, type) - raise_assertion_if( - condition=_type.graphene_type is not type, - message=error_message - ) + if _type.graphene_type is not type: + raise AssertionError( + "Found different types with the same name in the schema: {}, {}.".format( + _type.graphene_type, type + ) + ) return map if issubclass(type, ObjectType): @@ -185,12 +182,12 @@ class TypeMap(GraphQLTypeMap): if type._meta.name in map: _type = map[type._meta.name] if isinstance(_type, GrapheneGraphQLType): - error_message = "Found different types with the same name in the schema: {}, {}." - .format(_type.graphene_type, type) - raise_assertion_if( - condition=_type.graphene_type is not type, - message=error_message - ) + if _type.graphene_type != type: + raise AssertionError( + "Found different types with the same name in the schema: {}, {}.".format( + _type.graphene_type, type + ) + ) return _type def interfaces(): @@ -198,12 +195,12 @@ class TypeMap(GraphQLTypeMap): for interface in type._meta.interfaces: self.graphene_reducer(map, interface) internal_type = map[interface._meta.name] - error_message = "Found different types with the same name in the schema: {}, {}." - .format(internal_type.graphene_type, interface) - raise_assertion_if( - condition=internal_type.graphene_type is not interface, - message=error_message - ) + if internal_type.graphene_type != interface: + raise AssertionError( + "Found different types with the same name in the schema: {}, {}.".format( + internal_type.graphene_type, interface + ) + ) interfaces.append(internal_type) return interfaces @@ -227,12 +224,12 @@ class TypeMap(GraphQLTypeMap): if type._meta.name in map: _type = map[type._meta.name] if isinstance(_type, GrapheneInterfaceType): - error_message = "Found different types with the same name in the schema: {}, {}." - .format(_type.graphene_type, type) - raise_assertion_if( - condition= _type.graphene_type is not type, - message=error_message - ) + if _type.graphene_type != type: + raise AssertionError( + "Found different types with the same name in the schema: {}, {}.".format( + _type.graphene_type, type + ) + ) return _type _resolve_type = None @@ -271,12 +268,12 @@ class TypeMap(GraphQLTypeMap): for objecttype in type._meta.types: self.graphene_reducer(map, objecttype) internal_type = map[objecttype._meta.name] - error_message = "Found different types with the same name in the schema: {}, {}." - .format(internal_type.graphene_type, objecttype) - raise_assertion_if( - condition=internal_type.graphene_type is not objecttype, - message=error_message - ) + if internal_type.graphene_type != objecttype: + raise AssertionError( + "Found different types with the same name in the schema: {}, {}.".format( + internal_type.graphene_type, objecttype + ) + ) union_types.append(internal_type) return union_types diff --git a/graphene/types/union.py b/graphene/types/union.py index 0122382e..1c14fd71 100644 --- a/graphene/types/union.py +++ b/graphene/types/union.py @@ -1,8 +1,6 @@ from .base import BaseOptions, BaseType from .unmountedtype import UnmountedType -from ..utils.comparison_helper import raise_assertion_if - # For static type checking with Mypy MYPY = False if MYPY: @@ -25,11 +23,10 @@ class Union(UnmountedType, BaseType): @classmethod def __init_subclass_with_meta__(cls, types=None, **options): - - raise_assertion_if( - condition=not (isinstance(types, (list, tuple)) and len(types) > 0, - message="Must provide types for Union {name}.".format(name=cls.__name__) - ) + if not (isinstance(types, (list, tuple)) and len(types) > 0): + raise AssertionError( + "Must provide types for Union {name}.".format(name=cls.__name__) + ) _meta = UnionOptions(cls) _meta.types = types super(Union, cls).__init_subclass_with_meta__(_meta=_meta, **options) diff --git a/graphene/types/uuid.py b/graphene/types/uuid.py index 1f06cbf9..4d6fd316 100644 --- a/graphene/types/uuid.py +++ b/graphene/types/uuid.py @@ -5,7 +5,6 @@ from uuid import UUID as _UUID from graphql.language import ast from .scalars import Scalar -from ..utils.comparison_helper import raise_assertion_if class UUID(Scalar): """UUID""" @@ -14,10 +13,8 @@ class UUID(Scalar): def serialize(uuid): if isinstance(uuid, str): uuid = _UUID(uuid) - raise_assertion_if( - condition=not isinstance(uuid, _UUID), - message="Expected UUID instance, received {}".format(uuid) - ) + if not isinstance(uuid, _UUID): + raise AssertionError("Expected UUID instance, received {}".format(uuid)) return str(uuid) @staticmethod diff --git a/graphene/utils/annotate.py b/graphene/utils/annotate.py index 39b91eeb..18dcd881 100644 --- a/graphene/utils/annotate.py +++ b/graphene/utils/annotate.py @@ -2,7 +2,6 @@ import six from ..pyutils.compat import func_name, signature from .deprecated import warn_deprecation -from .comparison_helper import raise_assertion_if def annotate(_func=None, _trigger_warning=True, **annotations): @@ -23,13 +22,11 @@ def annotate(_func=None, _trigger_warning=True, **annotations): # We make sure the annotations are valid for key, value in annotations.items(): - func_param = func_signature.parameters.get(key, None) - assertion_message = 'The key {key} is not a function parameter in the function "{func_name}".' - .format(key=key, func_name=func_name(_func) - - raise_assertion_if( - condition=func_param is None, - message=assertion_message + 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) + ) ) func_annotations = getattr(_func, "__annotations__", None) diff --git a/graphene/utils/subclass_with_meta.py b/graphene/utils/subclass_with_meta.py index 0546dc08..2889862f 100644 --- a/graphene/utils/subclass_with_meta.py +++ b/graphene/utils/subclass_with_meta.py @@ -41,13 +41,16 @@ class SubclassWithMeta(six.with_metaclass(SubclassWithMeta_Meta)): options = dict(meta_options, **_meta_props) abstract = options.pop("abstract", False) - error_message = """ + if abstract: + if options: + raise AssertionError( + """ Abstract types can only contain the abstract attribute. Received: abstract, {option_keys} - """.format(option_keys=", ".join(options.keys()) - - raise_assertion_if(condition=options and abstract, message=error_message) - + """.format( + option_keys=", ".join(options.keys()) + ) + ) else: super_class = super(cls, cls) if hasattr(super_class, "__init_subclass_with_meta__"):