【问题标题】:PyCharm: How to get class attributes to be listed in Quick Documentation pop-up?PyCharm:如何获取要在快速文档弹出窗口中列出的类属性?
【发布时间】:2019-03-21 12:33:48
【问题描述】:

我发现快速文档不显示属性,这真的很烦人,在类的文档中列出。我认为原因是文档字符串格式不正确或不受支持,但reSTGoogle 样式的行为相同(我在Python Integrated Tools 中正确设置了它们)。


我当前的文档字符串样式是Google。让我看看图片有什么问题:

这是Actor 类。 如您所见,Attributes 部分显示在文档字符串中,但不在快速文档弹出窗口中。

另外,属性world_id 上没有注释。

Args__init__ 识别就像一个魅力。

让我们直接将文档字符串添加到属性(answer 中建议的方式)。效果很好,不是吗?

并在另一个方法中为属性调用快速文档。哇,又没有描述了。


如何让事情正常进行?如何设置 PyCharm 以便它赶上类的属性并在快速文档中显示它们?

PyCharm 2018.2.4(社区版)。 Ubuntu 16.04。

【问题讨论】:

  • 在 PyCharm 文档中,它说“PyCharm 识别根据PEP-257 创建的内联文档”。
  • @lmiguelvargasf,PEP-257 中有一行:“类的文档字符串应该总结其行为并列出公共方法和实例变量”。我想这足以让 PyC​​harm 识别属性。

标签: python pycharm documentation docstring


【解决方案1】:

对于仍在为此苦苦挣扎并偶然发现这篇文章的人(就像我一样)。

PyCharm 目前不支持这个 - 或者我应该说它仍然不支持它。不适用于 Google 样式的文档字符串,也不适用于结构化文本。他们的 Youtrack 支持网站上有多张关于此问题的票证。

例如https://youtrack.jetbrains.com/issue/PY-17945 - 这个问题已经存在 5 年多了!

【讨论】:

    【解决方案2】:

    以下是我如何在 Pycharm 2020.3.3(我使用专业版)的 Docstring 中显示类属性:

    class TestModel(models.Model):
        """
        A Test model to see the class attributes in the docstring
    
        Attributes:
    
        - :class:`str` name --> The name of the test object
        - :class:`datetime` created_at --> Date and time when the instance was created
        """
        name = models.CharField(max_length=50)
        created_at = models.DateTimeField(auto_now_add=True)
    

    请注意,如果没有 Attributes: 行,它将无法按预期工作。

    这就是它在快速文档中的显示方式:

    【讨论】:

    • 更pythonic的方式是:
    • name : :class:`str`
    猜你喜欢
    • 1970-01-01
    • 2021-07-04
    • 1970-01-01
    • 2016-01-17
    • 1970-01-01
    • 1970-01-01
    • 2012-01-18
    • 1970-01-01
    • 2012-02-20
    相关资源
    最近更新 更多