文档字符串中 PyCharm 中未解决的属性引用问题

Unresolved attribute reference issue in PyCharm in doc string

当我使用 attrs 库创建 class 时,PyCharm 中的 docstring 显示 linting 错误 未解析的属性引用

另一方面,当我通常使用 __init__ 方法创建 class 时。它没有显示这样的错误。

我无法确定此错误是由 attrs 或 PyCharm 引起的,因为默认情况下 attrs 具有所有必要的 mypy 类型检查存根。在使用 attrs 时,除了这一次在文档字符串中,到目前为止我找不到任何 linting 错误。


import attr


@attr.s
class UsingAttrs:
    """
    class created using attrs shows linting error.

    Attributes
    ----------
    attribute_with_attr : str
    """
    attribute_with_attr: str = attr.ib(default='some_string_value')


class NotUsingAttrs:
    """
    class created normally does not show linting error.

    Attributes
    ----------
    attribute_without_attr : str
    """
    attribute_without_attr: str

    def __init__(self, param='some string value'):
        self.attribute_without_attr = param

Linting 错误如下图所示 --

如有任何帮助,我们将不胜感激。

这是 PyCharm 中的错误。 attrs 对他们的分析无能为力,尤其是考虑到检查文档字符串完全是他们的功能。据我所知,他们根本不在内部使用 mypy,必须自己重新实现所有内容。

P.S。如果你使用 @attr.s(auto_attrib=True)

,你可以只写 attribute_with_attr: str = "some_string_value"