3

When I'm using attrs library to create class, docstring in PyCharm shows a linting error unresolved attribute reference.

On the other hand when I create class normally with __init__ method. It shows no such error.

I can't figure out this error is generated due to attrs or PyCharm because attrs by default has all necessary stubs for mypy type checking. While using attrs, I could not find any linting errors so far except for this time in the docstrings.


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 error is shown in the image attached below --

enter image description here

Any help would be appreciated.

Akarsh Jain
  • 930
  • 10
  • 15

1 Answers1

2

That's a bug in PyCharm. attrs can't do anything about their analysis, especially given the fact it's entirely their feature to check docstrings. AFAIK they don't use mypy internally at all and have to reimplement everything themselves.

P.S. You can write just attribute_with_attr: str = "some_string_value" if you use @attr.s(auto_attrib=True)

hynek
  • 3,647
  • 1
  • 18
  • 26