【问题标题】:Type hinting class not yet imported类型提示类尚未导入
【发布时间】:2013-12-02 15:05:44
【问题描述】:

我有以下代码:

def render(self, notification):
    """
    @type notification: Notification
    """
    return NotificationRepresentation(notification).to_dict()
    # some irrelevant code

notificationNotification 类实例。 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

有什么干净的方法可以做到这一点吗?

【问题讨论】:

标签: python pycharm type-hinting


【解决方案1】:

提供要引用的类的完整路径。

def render(self, notification, my_subtask):
    """
    @type notification: full.path.to.Notification
    @type my_subtask: celery.canvas.subtask
    """
    return NotificationRepresentation(notification).to_dict()
    # some irrelevant code

这可能是您旧安装的问题,因为在 PyCharm 3.0 中提供完整路径对我有用。尝试升级;)

【讨论】:

  • 你能举一个完整路径的例子吗?尝试键入提示属于非导入库的类怎么样 - 比如 wx ?
猜你喜欢
  • 1970-01-01
  • 2021-12-12
  • 2023-01-10
  • 1970-01-01
  • 2019-04-12
  • 2020-11-23
  • 2017-02-06
  • 2016-08-17
相关资源
最近更新 更多