From a16c5ba00be1d963d438c5e96962b79d1b145cc3 Mon Sep 17 00:00:00 2001 From: Devin Fee Date: Sat, 16 Dec 2017 21:49:23 -0800 Subject: [PATCH] extended support for subclassing with meta to Enum and InputObjectType --- graphene/tests/issues/test_425.py | 77 +++++++++++++++++++++++++++++++ graphene/types/enum.py | 5 +- graphene/types/inputobjecttype.py | 5 +- 3 files changed, 83 insertions(+), 4 deletions(-) diff --git a/graphene/tests/issues/test_425.py b/graphene/tests/issues/test_425.py index 7f92a75a..d50edf84 100644 --- a/graphene/tests/issues/test_425.py +++ b/graphene/tests/issues/test_425.py @@ -2,8 +2,11 @@ # Adapted for Graphene 2.0 from graphene.types.objecttype import ObjectType, ObjectTypeOptions +from graphene.types.inputobjecttype import InputObjectType, InputObjectTypeOptions +from graphene.types.enum import Enum, EnumOptions +# ObjectType class SpecialOptions(ObjectTypeOptions): other_attr = None @@ -40,3 +43,77 @@ def test_special_objecttype_inherit_meta_options(): assert MyType._meta.name == 'MyType' assert MyType._meta.default_resolver is None assert MyType._meta.interfaces == () + + +# InputObjectType +class SpecialInputObjectTypeOptions(ObjectTypeOptions): + other_attr = None + + +class SpecialInputObjectType(InputObjectType): + + @classmethod + def __init_subclass_with_meta__(cls, other_attr='default', **options): + _meta = SpecialInputObjectTypeOptions(cls) + _meta.other_attr = other_attr + super(SpecialInputObjectType, cls).__init_subclass_with_meta__(_meta=_meta, **options) + + +def test_special_inputobjecttype_could_be_subclassed(): + class MyInputObjectType(SpecialInputObjectType): + + class Meta: + other_attr = 'yeah!' + + assert MyInputObjectType._meta.other_attr == 'yeah!' + + +def test_special_inputobjecttype_could_be_subclassed_default(): + class MyInputObjectType(SpecialInputObjectType): + pass + + assert MyInputObjectType._meta.other_attr == 'default' + + +def test_special_inputobjecttype_inherit_meta_options(): + class MyInputObjectType(SpecialInputObjectType): + pass + + assert MyInputObjectType._meta.name == 'MyInputObjectType' + + +# Enum +class SpecialEnumOptions(EnumOptions): + other_attr = None + + +class SpecialEnum(Enum): + + @classmethod + def __init_subclass_with_meta__(cls, other_attr='default', **options): + _meta = SpecialEnumOptions(cls) + _meta.other_attr = other_attr + super(SpecialEnum, cls).__init_subclass_with_meta__(_meta=_meta, **options) + + +def test_special_enum_could_be_subclassed(): + class MyEnum(SpecialEnum): + + class Meta: + other_attr = 'yeah!' + + assert MyEnum._meta.other_attr == 'yeah!' + + +def test_special_enum_could_be_subclassed_default(): + class MyEnum(SpecialEnum): + pass + + assert MyEnum._meta.other_attr == 'default' + + +def test_special_enum_inherit_meta_options(): + class MyEnum(SpecialEnum): + pass + + assert MyEnum._meta.name == 'MyEnum' diff --git a/graphene/types/enum.py b/graphene/types/enum.py index 4e3f2ac2..da0b6240 100644 --- a/graphene/types/enum.py +++ b/graphene/types/enum.py @@ -60,8 +60,9 @@ class EnumMeta(SubclassWithMeta_Meta): class Enum(six.with_metaclass(EnumMeta, UnmountedType, BaseType)): @classmethod - def __init_subclass_with_meta__(cls, enum=None, **options): - _meta = EnumOptions(cls) + def __init_subclass_with_meta__(cls, enum=None, _meta=None, **options): + if not _meta: + _meta = EnumOptions(cls) _meta.enum = enum or cls.__enum__ _meta.deprecation_reason = options.pop('deprecation_reason', None) for key, value in _meta.enum.__members__.items(): diff --git a/graphene/types/inputobjecttype.py b/graphene/types/inputobjecttype.py index 76f5dd09..dbfccc46 100644 --- a/graphene/types/inputobjecttype.py +++ b/graphene/types/inputobjecttype.py @@ -40,8 +40,9 @@ class InputObjectType(UnmountedType, BaseType): ''' @classmethod - def __init_subclass_with_meta__(cls, container=None, **options): - _meta = InputObjectTypeOptions(cls) + def __init_subclass_with_meta__(cls, container=None, _meta=None, **options): + if not _meta: + _meta = InputObjectTypeOptions(cls) fields = OrderedDict() for base in reversed(cls.__mro__):