【问题标题】:PEP-526 Is not compatible with docstrings for class variables?PEP-526 与类变量的文档字符串不兼容?
【发布时间】:2018-04-09 16:47:23
【问题描述】:

如果我将 PEP-526 用于类级变量,我必须将它们记录在类的文档字符串中,在这种情况下,我需要选择我最喜欢的方式来执行此操作。

Python 3.6.4 pylint==1.8.4

class Joe(object):
  counter: int = 0
  """This is a counter"""

pylint 说(正确,不管变量是否被赋值):

W: 3, 2: 字符串语句无效(无意义字符串语句)

【问题讨论】:

  • 该 pep 的哪一部分阻止您将文档字符串放在注释上方?

标签: python python-3.x pylint type-hinting docstring


【解决方案1】:

不是 PEP 526 与文档字符串不兼容;是皮林特不明白你做了什么。等待更新。

一些工具支持类变量的文档字符串概念,但它从来都不是 Python 本身的一部分,而且 PEP 526 不会改变任何事情。

【讨论】:

    【解决方案2】:

    在这种情况下,pylint 将带有注释的行理解为字符串表达式,而不是类文档注释。这意味着它作为表达式字符串不存储在任何变量中,并且在表达式执行后将丢失。还是一样的例子:

    def foo():
        a = 1
        """ this is string expression, not doc string """
        b = 2
    

    在你的情况下,更正确的是将文档字符串放在顶级正文中:

    class Joe(object):
        """This is a counter"""
        counter: int = 0
    

    并且 pylint 正确理解它:

    ************* Module tt
    C:  4, 0: Trailing newlines (trailing-newlines)
    C:  1, 0: Missing module docstring (missing-docstring)
    R:  1, 0: Too few public methods (0/2) (too-few-public-methods)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多