Error: types.coroutine() expects a callable

你。 提交于 2019-12-24 06:59:09

问题


I have the following class:

from tornado import gen

class VertexSync(Vertex):
    @wait_till_complete
    @gen.coroutine
    @classmethod
    def find_by_value(cls, *args, **kwargs):
        stream = yield super().find_by_value(*args, **kwargs)
        aggr = []
        while True:
            resp = yield stream.read()
            if resp is None:
                break
            aggr = aggr + resp
        return aggr

TypeError: types.coroutine() expects a callable

Can you tell me what the problem is?

=> edit Code calling this function

print(DemoVertex.find_by_value('longitude', 55.0))

回答1:


The problem is that classmethod does... interesting things. Once the class definition has finished you have a nice callable method on the class, but during the definition you have a classmethod object, which isn't callable:

>>> a = classmethod(lambda self: None)
>>> a
<classmethod object at 0x10b46b390>
>>> callable(a)
False
>>> a()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'classmethod' object is not callable

The simplest fix is to re-order the decorators, so that rather than trying to turn a classmethod into a coroutine:

@gen.coroutine
@classmethod
def thing(...):
    ...

you are trying to turn a coroutine into a classmethod:

@classmethod
@gen.coroutine
def thing(...):
    ...

Note that the decorators are applied "inside out", see e.g. Decorator execution order



来源:https://stackoverflow.com/questions/37995862/error-types-coroutine-expects-a-callable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!