mirror of
https://github.com/graphql-python/graphene.git
synced 2025-02-08 23:50:38 +03:00
Improved abstracttypes implementation
This commit is contained in:
parent
cd0be62993
commit
55a1450dd3
|
@ -10,7 +10,7 @@ class AbstractTypeMeta(type):
|
||||||
|
|
||||||
def __new__(cls, name, bases, attrs):
|
def __new__(cls, name, bases, attrs):
|
||||||
# Also ensure initialization is only performed for subclasses of
|
# Also ensure initialization is only performed for subclasses of
|
||||||
# ObjectType
|
# AbstractType
|
||||||
if not is_base_type(bases, AbstractTypeMeta):
|
if not is_base_type(bases, AbstractTypeMeta):
|
||||||
return type.__new__(cls, name, bases, attrs)
|
return type.__new__(cls, name, bases, attrs)
|
||||||
|
|
||||||
|
@ -20,7 +20,7 @@ class AbstractTypeMeta(type):
|
||||||
return type.__new__(cls, name, bases, attrs)
|
return type.__new__(cls, name, bases, attrs)
|
||||||
|
|
||||||
attrs = merge_fields_in_attrs(bases, attrs)
|
attrs = merge_fields_in_attrs(bases, attrs)
|
||||||
fields = get_fields_in_type(cls, attrs)
|
fields = get_fields_in_type(AbstractType, attrs)
|
||||||
yank_fields_from_attrs(attrs, fields)
|
yank_fields_from_attrs(attrs, fields)
|
||||||
|
|
||||||
options = attrs.get('_meta', Options())
|
options = attrs.get('_meta', Options())
|
||||||
|
|
|
@ -23,7 +23,7 @@ class InterfaceMeta(AbstractTypeMeta):
|
||||||
)
|
)
|
||||||
|
|
||||||
attrs = merge_fields_in_attrs(bases, attrs)
|
attrs = merge_fields_in_attrs(bases, attrs)
|
||||||
options.fields = get_fields_in_type(cls, attrs)
|
options.fields = get_fields_in_type(Interface, attrs)
|
||||||
yank_fields_from_attrs(attrs, options.fields)
|
yank_fields_from_attrs(attrs, options.fields)
|
||||||
|
|
||||||
return type.__new__(cls, name, bases, dict(attrs, _meta=options))
|
return type.__new__(cls, name, bases, dict(attrs, _meta=options))
|
||||||
|
@ -35,6 +35,6 @@ class Interface(six.with_metaclass(InterfaceMeta)):
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
raise Exception("An interface cannot be intitialized")
|
raise Exception("An interface cannot be intitialized")
|
||||||
|
|
||||||
@classmethod
|
# @classmethod
|
||||||
def implements(cls, objecttype):
|
# def implements(cls, objecttype):
|
||||||
pass
|
# pass
|
||||||
|
|
|
@ -23,7 +23,7 @@ class ObjectTypeMeta(AbstractTypeMeta):
|
||||||
)
|
)
|
||||||
|
|
||||||
attrs = merge_fields_in_attrs(bases, attrs)
|
attrs = merge_fields_in_attrs(bases, attrs)
|
||||||
options.fields = get_fields_in_type(cls, attrs)
|
options.fields = get_fields_in_type(ObjectType, attrs)
|
||||||
yank_fields_from_attrs(attrs, options.fields)
|
yank_fields_from_attrs(attrs, options.fields)
|
||||||
|
|
||||||
return type.__new__(cls, name, bases, dict(attrs, _meta=options))
|
return type.__new__(cls, name, bases, dict(attrs, _meta=options))
|
||||||
|
|
|
@ -40,61 +40,3 @@ def test_generate_abstracttype_inheritance():
|
||||||
assert MyAbstractType2._meta.fields.keys() == ['field1', 'field2']
|
assert MyAbstractType2._meta.fields.keys() == ['field1', 'field2']
|
||||||
assert not hasattr(MyAbstractType1, 'field1')
|
assert not hasattr(MyAbstractType1, 'field1')
|
||||||
assert not hasattr(MyAbstractType2, 'field2')
|
assert not hasattr(MyAbstractType2, 'field2')
|
||||||
|
|
||||||
# def test_ordered_fields_in_objecttype():
|
|
||||||
# class MyObjectType(ObjectType):
|
|
||||||
# b = Field(MyType)
|
|
||||||
# a = Field(MyType)
|
|
||||||
# field = MyScalar()
|
|
||||||
# asa = Field(MyType)
|
|
||||||
|
|
||||||
# assert list(MyObjectType._meta.fields.keys()) == ['b', 'a', 'field', 'asa']
|
|
||||||
|
|
||||||
|
|
||||||
# def test_generate_objecttype_unmountedtype():
|
|
||||||
# class MyObjectType(ObjectType):
|
|
||||||
# field = MyScalar(MyType)
|
|
||||||
|
|
||||||
# assert 'field' in MyObjectType._meta.fields
|
|
||||||
# assert isinstance(MyObjectType._meta.fields['field'], Field)
|
|
||||||
|
|
||||||
|
|
||||||
# def test_parent_container_get_fields():
|
|
||||||
# assert list(Container._meta.fields.keys()) == ['field1', 'field2']
|
|
||||||
|
|
||||||
|
|
||||||
# def test_objecttype_as_container_only_args():
|
|
||||||
# container = Container("1", "2")
|
|
||||||
# assert container.field1 == "1"
|
|
||||||
# assert container.field2 == "2"
|
|
||||||
|
|
||||||
|
|
||||||
# def test_objecttype_as_container_args_kwargs():
|
|
||||||
# container = Container("1", field2="2")
|
|
||||||
# assert container.field1 == "1"
|
|
||||||
# assert container.field2 == "2"
|
|
||||||
|
|
||||||
|
|
||||||
# def test_objecttype_as_container_few_kwargs():
|
|
||||||
# container = Container(field2="2")
|
|
||||||
# assert container.field2 == "2"
|
|
||||||
|
|
||||||
|
|
||||||
# def test_objecttype_as_container_all_kwargs():
|
|
||||||
# container = Container(field1="1", field2="2")
|
|
||||||
# assert container.field1 == "1"
|
|
||||||
# assert container.field2 == "2"
|
|
||||||
|
|
||||||
|
|
||||||
# def test_objecttype_as_container_extra_args():
|
|
||||||
# with pytest.raises(IndexError) as excinfo:
|
|
||||||
# Container("1", "2", "3")
|
|
||||||
|
|
||||||
# assert "Number of args exceeds number of fields" == str(excinfo.value)
|
|
||||||
|
|
||||||
|
|
||||||
# def test_objecttype_as_container_invalid_kwargs():
|
|
||||||
# with pytest.raises(TypeError) as excinfo:
|
|
||||||
# Container(unexisting_field="3")
|
|
||||||
|
|
||||||
# assert "'unexisting_field' is an invalid keyword argument for Container" == str(excinfo.value)
|
|
||||||
|
|
|
@ -25,14 +25,14 @@ def unmounted_field_in_type(attname, unmounted_field, type):
|
||||||
InputObjectType -> InputField
|
InputObjectType -> InputField
|
||||||
'''
|
'''
|
||||||
# from ..types.inputobjecttype import InputObjectType
|
# from ..types.inputobjecttype import InputObjectType
|
||||||
from ..new_types.objecttype import ObjectTypeMeta
|
from ..new_types.objecttype import ObjectType
|
||||||
from ..new_types.interface import InterfaceMeta
|
from ..new_types.interface import Interface
|
||||||
from ..new_types.abstracttype import AbstractTypeMeta
|
from ..new_types.abstracttype import AbstractType
|
||||||
|
|
||||||
if issubclass(type, (ObjectTypeMeta, InterfaceMeta)):
|
if issubclass(type, (ObjectType, Interface)):
|
||||||
return unmounted_field.as_field()
|
return unmounted_field.as_field()
|
||||||
|
|
||||||
elif issubclass(type, (AbstractTypeMeta)):
|
elif issubclass(type, (AbstractType)):
|
||||||
return unmounted_field
|
return unmounted_field
|
||||||
# elif issubclass(type, (InputObjectType)):
|
# elif issubclass(type, (InputObjectType)):
|
||||||
# return unmounted_field.as_inputfield()
|
# return unmounted_field.as_inputfield()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user