Skip to content

Commit

Permalink
Merge 7c652ef into 2e87ebe
Browse files Browse the repository at this point in the history
  • Loading branch information
ruohola committed Jan 23, 2021
2 parents 2e87ebe + 7c652ef commit 3b512b1
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
8 changes: 7 additions & 1 deletion graphene/utils/subclass_with_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ class SubclassWithMeta(metaclass=SubclassWithMeta_Meta):
"""This class improves __init_subclass__ to receive automatically the options from meta"""

def __init_subclass__(cls, **meta_options):
"""This method just terminates the super() chain"""
"""Consume all the passed kwargs and cls's possible `Meta`.
The consumed kwargs and all of the attributes/keys from `Meta`
will be combined into a dict and passed for further processing to the next
`__init_subclass_with_meta__` in the method resolution order.
"""
_Meta = getattr(cls, "Meta", None)
_meta_props = {}
if _Meta:
Expand All @@ -44,6 +49,7 @@ def __init_subclass__(cls, **meta_options):
super_class = super(cls, cls)
if hasattr(super_class, "__init_subclass_with_meta__"):
super_class.__init_subclass_with_meta__(**options)
super().__init_subclass__()

@classmethod
def __init_subclass_with_meta__(cls, **meta_options):
Expand Down
63 changes: 63 additions & 0 deletions graphene/utils/tests/test_subclass_with_meta.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
from ..subclass_with_meta import SubclassWithMeta


def test_init_subclass_calls_super():
class DefinesInitSubclass:
subclass_init_count = 0

def __init_subclass__(cls, **kwargs):
DefinesInitSubclass.subclass_init_count += 1
super().__init_subclass__(**kwargs)

class InheritsAsFirst(SubclassWithMeta, DefinesInitSubclass):
pass

class InheritsAsLast(DefinesInitSubclass, SubclassWithMeta):
pass

assert DefinesInitSubclass.subclass_init_count == 2


def test_kwargs_are_consumed():
class DefinesInitSubclass:
passed_kwargs = []

def __init_subclass__(cls, **kwargs):
if kwargs:
DefinesInitSubclass.passed_kwargs.append(kwargs)
super().__init_subclass__(**kwargs)

class InheritsAsFirst(SubclassWithMeta, DefinesInitSubclass, is_consumed="foo"):
pass

class InheritsAsLast(DefinesInitSubclass, SubclassWithMeta, not_consumed="bar"):
pass

assert DefinesInitSubclass.passed_kwargs == [{"not_consumed": "bar"}]


def test_meta_is_deleted_and_props_passed():
class DefinesInitSubclassWithMeta(SubclassWithMeta):
passed_meta_options = []

@classmethod
def __init_subclass_with_meta__(cls, **options):
if options:
DefinesInitSubclassWithMeta.passed_meta_options.append(options)
super().__init_subclass_with_meta__()

class HasMetaClass(DefinesInitSubclassWithMeta, foo1=1):
class Meta:
foo2 = 2
foo3 = 3

class HasMetaDict(DefinesInitSubclassWithMeta, bar1=1):
Meta = {"bar2": 2, "bar3": 3}

assert not hasattr(HasMetaClass, "Meta")
assert not hasattr(HasMetaDict, "Meta")

assert DefinesInitSubclassWithMeta.passed_meta_options == [
{"foo1": 1, "foo2": 2, "foo3": 3},
{"bar1": 1, "bar2": 2, "bar3": 3},
]

0 comments on commit 3b512b1

Please sign in to comment.