Skip to content

Commit 38db32e

Browse files
authored
Merge pull request #630 from dfee/425-extended
extended support for subclassing with meta to Enum and InputObjectType
2 parents 25dab92 + a16c5ba commit 38db32e

File tree

3 files changed

+83
-4
lines changed

3 files changed

+83
-4
lines changed

graphene/tests/issues/test_425.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
# Adapted for Graphene 2.0
33

44
from graphene.types.objecttype import ObjectType, ObjectTypeOptions
5+
from graphene.types.inputobjecttype import InputObjectType, InputObjectTypeOptions
6+
from graphene.types.enum import Enum, EnumOptions
57

68

9+
# ObjectType
710
class SpecialOptions(ObjectTypeOptions):
811
other_attr = None
912

@@ -40,3 +43,77 @@ class MyType(SpecialObjectType):
4043
assert MyType._meta.name == 'MyType'
4144
assert MyType._meta.default_resolver is None
4245
assert MyType._meta.interfaces == ()
46+
47+
48+
# InputObjectType
49+
class SpecialInputObjectTypeOptions(ObjectTypeOptions):
50+
other_attr = None
51+
52+
53+
class SpecialInputObjectType(InputObjectType):
54+
55+
@classmethod
56+
def __init_subclass_with_meta__(cls, other_attr='default', **options):
57+
_meta = SpecialInputObjectTypeOptions(cls)
58+
_meta.other_attr = other_attr
59+
super(SpecialInputObjectType, cls).__init_subclass_with_meta__(_meta=_meta, **options)
60+
61+
62+
def test_special_inputobjecttype_could_be_subclassed():
63+
class MyInputObjectType(SpecialInputObjectType):
64+
65+
class Meta:
66+
other_attr = 'yeah!'
67+
68+
assert MyInputObjectType._meta.other_attr == 'yeah!'
69+
70+
71+
def test_special_inputobjecttype_could_be_subclassed_default():
72+
class MyInputObjectType(SpecialInputObjectType):
73+
pass
74+
75+
assert MyInputObjectType._meta.other_attr == 'default'
76+
77+
78+
def test_special_inputobjecttype_inherit_meta_options():
79+
class MyInputObjectType(SpecialInputObjectType):
80+
pass
81+
82+
assert MyInputObjectType._meta.name == 'MyInputObjectType'
83+
84+
85+
# Enum
86+
class SpecialEnumOptions(EnumOptions):
87+
other_attr = None
88+
89+
90+
class SpecialEnum(Enum):
91+
92+
@classmethod
93+
def __init_subclass_with_meta__(cls, other_attr='default', **options):
94+
_meta = SpecialEnumOptions(cls)
95+
_meta.other_attr = other_attr
96+
super(SpecialEnum, cls).__init_subclass_with_meta__(_meta=_meta, **options)
97+
98+
99+
def test_special_enum_could_be_subclassed():
100+
class MyEnum(SpecialEnum):
101+
102+
class Meta:
103+
other_attr = 'yeah!'
104+
105+
assert MyEnum._meta.other_attr == 'yeah!'
106+
107+
108+
def test_special_enum_could_be_subclassed_default():
109+
class MyEnum(SpecialEnum):
110+
pass
111+
112+
assert MyEnum._meta.other_attr == 'default'
113+
114+
115+
def test_special_enum_inherit_meta_options():
116+
class MyEnum(SpecialEnum):
117+
pass
118+
119+
assert MyEnum._meta.name == 'MyEnum'

graphene/types/enum.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,9 @@ def from_enum(cls, enum, description=None, deprecation_reason=None): # noqa: N8
6060
class Enum(six.with_metaclass(EnumMeta, UnmountedType, BaseType)):
6161

6262
@classmethod
63-
def __init_subclass_with_meta__(cls, enum=None, **options):
64-
_meta = EnumOptions(cls)
63+
def __init_subclass_with_meta__(cls, enum=None, _meta=None, **options):
64+
if not _meta:
65+
_meta = EnumOptions(cls)
6566
_meta.enum = enum or cls.__enum__
6667
_meta.deprecation_reason = options.pop('deprecation_reason', None)
6768
for key, value in _meta.enum.__members__.items():

graphene/types/inputobjecttype.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ class InputObjectType(UnmountedType, BaseType):
4040
'''
4141

4242
@classmethod
43-
def __init_subclass_with_meta__(cls, container=None, **options):
44-
_meta = InputObjectTypeOptions(cls)
43+
def __init_subclass_with_meta__(cls, container=None, _meta=None, **options):
44+
if not _meta:
45+
_meta = InputObjectTypeOptions(cls)
4546

4647
fields = OrderedDict()
4748
for base in reversed(cls.__mro__):

0 commit comments

Comments
 (0)