Skip to content

Python docstrings mistakenly rendered as comment #184836

@lkct

Description

@lkct

Does this issue occur when all extensions are disabled?: Yes/No

  • VS Code Version: Code 1.79.0 (b380da4, 2023-06-07T14:26:35.552Z)
  • OS Version: Windows_NT x64 10.0.22621

Steps to Reproduce:

  1. Update to 1.79.0. Use any bundled theme.
  2. Open any Python file with docstring (or copy-paste the example below). All docstrings are rendered in the same style as comments.

Related Issue/PR

From Python Docs

docstring
A string literal which appears as the first expression in a class, function or module. While ignored when the suite is executed, it is recognized by the compiler and put into the __doc__ attribute of the enclosing class, function or module. Since it is available via introspection, it is the canonical place for documentation of the object.

Takeaway of the above:

  1. docstring is a Python language feature associated with the dunder attribute __doc__. It is NOT a comment.
  2. Not every string literal is a docstring. Only those that appear as the 1st statement of a scope are docstrings.

Therefore, I think the current behaviour is WRONG as docstrings are not comments, but I agree with issue #182163 that comment strings should be rendered as comments.

Additional context

The Python extension additionally treats some non-docstring strings similar to a docstring, like the B and e in this example:

class A:
    """This is the docstring for A. Shown on hover."""

    B = 1
    """Shown on hover for B."""

    def c(self, x:int)->int:
        """This is the docstring for c. Shown on hover."""
        d = 1
        """This is nothing but a comment."""  # pylint: pointless-string-statement / W0105

e = 1
"""Shown on hover for e."""

Hovering on A B c e will all show the respective string content in VSCode. However, for B and e they're actually not docstrings, but it's indeed useful if we can add some "doc"strings for global variables and class attributes.
Also, some linters, e.g. pylint, share the same way to interpret these string literals: pylint generates a pointless-string-statement warning only on the "comment", but not at the other 4 places.

Thus, a better solution could be to expose extra rendering tokens and let the Python extension decide whether each string literal should be treated as a docstring or a comment string.

Finally, many thanks to everyone who has participated in the fruitful discussion in #182162 (cc @ElectricRCAircraftGuy @aeschli @k98kurz @starball5 @torext @dbaeumer). Further comments are welcomed.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions