From 5ee6e2b8e79b47ac00ce77f07ce97d2bb99fa222 Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Tue, 11 Jul 2017 20:59:02 -0700 Subject: [PATCH] Simplified Scalar --- graphene/types/scalars.py | 31 +++++++++-------------------- graphene/types/tests/test_scalar.py | 11 ++++++++++ 2 files changed, 20 insertions(+), 22 deletions(-) create mode 100644 graphene/types/tests/test_scalar.py diff --git a/graphene/types/scalars.py b/graphene/types/scalars.py index e1ff80d3..1ca17ff8 100644 --- a/graphene/types/scalars.py +++ b/graphene/types/scalars.py @@ -1,34 +1,17 @@ import six + from graphql.language.ast import (BooleanValue, FloatValue, IntValue, StringValue) -from ..utils.is_base_type import is_base_type -from ..utils.trim_docstring import trim_docstring -from .options import Options from .unmountedtype import UnmountedType +from .base import BaseOptions, BaseType -class ScalarTypeMeta(type): - - def __new__(cls, name, bases, attrs): - # Also ensure initialization is only performed for subclasses of Model - # (excluding Model class itself). - if not is_base_type(bases, ScalarTypeMeta): - return type.__new__(cls, name, bases, attrs) - - options = Options( - attrs.pop('Meta', None), - name=name, - description=trim_docstring(attrs.get('__doc__')), - ) - - return type.__new__(cls, name, bases, dict(attrs, _meta=options)) - - def __str__(cls): # noqa: N802 - return cls._meta.name +class ScalarOptions(BaseOptions): + pass -class Scalar(six.with_metaclass(ScalarTypeMeta, UnmountedType)): +class Scalar(UnmountedType, BaseType): ''' Scalar Type Definition @@ -36,6 +19,10 @@ class Scalar(six.with_metaclass(ScalarTypeMeta, UnmountedType)): Scalars (or Enums) and are defined with a name and a series of functions used to parse input from ast or variables and to ensure validity. ''' + @classmethod + def __init_subclass_with_meta__(cls, **options): + _meta = ScalarOptions(cls) + super(Scalar, cls).__init_subclass_with_meta__(_meta=_meta, **options) serialize = None parse_value = None diff --git a/graphene/types/tests/test_scalar.py b/graphene/types/tests/test_scalar.py new file mode 100644 index 00000000..af62faa1 --- /dev/null +++ b/graphene/types/tests/test_scalar.py @@ -0,0 +1,11 @@ +import pytest + +from ..scalars import Scalar + + +def test_scalar(): + class JSONScalar(Scalar): + '''Documentation''' + + assert JSONScalar._meta.name == "JSONScalar" + assert JSONScalar._meta.description == "Documentation"