在石墨烯中使用Metaclasses时出错

时间:2017-06-12 12:17:55

标签: python metaprogramming metaclass graphene-python

我正在尝试为我的graphene.ObjectType子类创建一个元类。我的主要目标是从包含其名称的一些字符串列表中创建类的属性(即Graphene对象的字段)。

例如。 list = ['quantity', 'position']

对于此列表,我的班级Attack必须是这样的:

class Attack(graphene.ObjectType):
    quantity = graphene.String()
    position = graphene.String()

我这样做的方式如下:

import graphene
from graphene.types.objecttype import ObjectType, ObjectTypeMeta

class mytype(ObjectTypeMeta):
    def __new__(cls, clsname, base, clsdict):
        print "Hello"
        setattr(cls, 'quantity', graphene.String())
        return ObjectTypeMeta.__new__(cls, clsname, base, clsdict)

class combined_meta(mytype, ObjectTypeMeta):
    pass

class Attack(graphene.ObjectType):
    __metaclass__ = combined_meta
    def __init__(self, dic):
        self.quantity = "123"
        print "Hello world"
        print dic

class Query(graphene.ObjectType):
    attack = graphene.Field(Attack)
    def resolve_attack(self, args, context, info):
        return Attack(dict())

schema = graphene.Schema(query = Query)

query = '''
{
    attack
}
'''

result = schema.execute(query)
print result.data

我看到"Hello"有很多错误..

AssertionError: Attack fields must be a mapping (dict / OrderedDict) with field names as keys or a function which returns such a mapping.

我做错了什么......?

1 个答案:

答案 0 :(得分:2)

您正在以不正确的方式创建__new__。元类的第一个参数' __new__是元类本身,而不是您正在创建的类。实际的" cls"正在创建的是type.__new__的返回 - 在这种情况下由ObjectTypeMeta.__new__调用。 另一方面,你得到的第四个参数是类命名空间 - 它是注入新属性的正确位置。

所以,试试这样的事情:

import graphene
from graphene.types.objecttype import ObjectType, ObjectTypeMeta

class mytype(ObjectTypeMeta):
    def __new__(metacls, clsname, base, clsdict):
        print "Hello"
        clsdict['quantity'] = graphene.String()
        return ObjectTypeMeta.__new__(metacls, clsname, base, clsdict)

...