【发布时间】:2013-12-02 15:05:44
【问题描述】:
我有以下代码:
def render(self, notification):
"""
@type notification: Notification
"""
return NotificationRepresentation(notification).to_dict()
# some irrelevant code
notification 是 Notification 类实例。 Notification 类未在此处导入,PyCharm 无法使用该类型提示 (inferred type: unknown)。
我尝试过使用完整的类名,但没有用。显而易见的方法是导入该类,但它从未使用过,因此这将是一个冗余导入(PyCharm 将在提交前优化导入时将其删除)。不太明显的方法是做一些奇怪的事情,比如Celery.task do:
STATICA_HACK = True
globals()['kcah_acitats'[::-1].upper()] = False
if STATICA_HACK:
# This is never executed, but tricks static analyzers (PyDev, PyCharm,
# pylint, etc.) into knowing the types of these symbols, and what
# they contain.
from celery.canvas import group, chord, subtask
from .base import BaseTask, Task, PeriodicTask, task, periodic_task
from .sets import TaskSet
有什么干净的方法可以做到这一点吗?
【问题讨论】:
-
链接到 STATICA_HACK 代码:docs.celeryproject.org/en/3.0/_modules/celery/task.html
-
@Mr_and_Mrs_D 谢谢你的链接 - 添加到问题中。此外,您也可以自己添加。
标签: python pycharm type-hinting