Terris
Terris

Reputation: 1019

PEP-526 Is not compatible with docstrings for class variables?

It appears if I use PEP-526 for class-level variables I must document them in the class's docstring in which case I need to pick my favorite way to do this.

Python 3.6.4 pylint==1.8.4

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

pylint says (correctly, regardless of whether the variable is assigned to):

W: 3, 2: String statement has no effect (pointless-string-statement)

Upvotes: 1

Views: 438

Answers (2)

JOHN_16
JOHN_16

Reputation: 626

In this case pylint understand line with comment as string expression, not class doc comment. It mean that it string as expression not stored in any variables and will be lost after expression executed. It is the same example:

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

In your case more correctly will be put docstring in top class body:

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

and pylint understand it correctly:

************* 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)

Upvotes: 1

user2357112
user2357112

Reputation: 281187

It's not PEP 526 being incompatible with the docstring; it's Pylint not understanding what you've done. Wait for an update.

Some tools support a concept of docstrings for class variables, but it's never been part of Python itself, and PEP 526 doesn't change things.

Upvotes: 2

Related Questions