From 85d31458624f809d38ee3c79c21dc7d55debd326 Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Sun, 23 Jul 2017 19:13:33 -0700 Subject: [PATCH] Improved abstract type --- graphene/types/abstracttype.py | 13 +++++--- graphene/types/tests/test_abstracttype.py | 40 +++++++++++++++++++++++ 2 files changed, 48 insertions(+), 5 deletions(-) create mode 100644 graphene/types/tests/test_abstracttype.py diff --git a/graphene/types/abstracttype.py b/graphene/types/abstracttype.py index efc756a1..3e283493 100644 --- a/graphene/types/abstracttype.py +++ b/graphene/types/abstracttype.py @@ -1,9 +1,12 @@ -from ..pyutils.init_subclass import InitSubclassMeta +from ..utils.subclass_with_meta import SubclassWithMeta +from ..utils.deprecated import warn_deprecation -class AbstractType(object): - __metaclass__ = InitSubclassMeta +class AbstractType(SubclassWithMeta): def __init_subclass__(cls, *args, **kwargs): - print("Abstract type is deprecated") - # super(AbstractType, cls).__init_subclass__(*args, **kwargs) + warn_deprecation( + "Abstract type is deprecated, please use normal object inheritance instead.\n" + "See more: https://github.com/graphql-python/graphene/blob/2.0/UPGRADE-v2.0.md#deprecations" + ) + super(AbstractType, cls).__init_subclass__(*args, **kwargs) diff --git a/graphene/types/tests/test_abstracttype.py b/graphene/types/tests/test_abstracttype.py new file mode 100644 index 00000000..44fc570d --- /dev/null +++ b/graphene/types/tests/test_abstracttype.py @@ -0,0 +1,40 @@ +import pytest + +from ..objecttype import ObjectType +from ..unmountedtype import UnmountedType +from ..abstracttype import AbstractType +from ..field import Field +from ...utils import deprecated + + +class MyType(ObjectType): + pass + + +class MyScalar(UnmountedType): + + def get_type(self): + return MyType + + +def test_abstract_objecttype_warn_deprecation(mocker): + mocker.patch.object(deprecated, 'warn_deprecation') + + class MyAbstractType(AbstractType): + field1 = MyScalar() + + deprecated.warn_deprecation.assert_called_once() + + +def test_generate_objecttype_inherit_abstracttype(): + class MyAbstractType(AbstractType): + field1 = MyScalar() + + class MyObjectType(ObjectType, MyAbstractType): + field2 = MyScalar() + + assert MyObjectType._meta.description is None + assert MyObjectType._meta.interfaces == () + assert MyObjectType._meta.name == "MyObjectType" + assert list(MyObjectType._meta.fields.keys()) == ['field1', 'field2'] + assert list(map(type, MyObjectType._meta.fields.values())) == [Field, Field]