Skip to content

W505: Warning raised for comments in addition to docstrings #15733

Closed

Description

@martimlobao

Description

W505 (doc line too long) is raised for docstrings that are too long, but it is also raised for comments (and commented-out code). Comments, and commented-out code in particular, are not docs and I feel they should be treated differently.

# ruff.toml
line-length = 119

[lint]
select = ["ALL"]

[lint.pycodestyle]
max-doc-length = 80
# example.py
class Foo:
    """A really long docstring that goes on and on and should be split into multiple lines."""

    def __init__(self):
        pass
        # print("This is some code that is less than 119 characters and that we commented out.")
$ ruff check
example.py:2:81: W505 Doc line too long (94 > 80)
  |
1 | class Foo:
2 |     """A really long docstring that goes on and on and should be split into multiple lines."""
  |                                                                                 ^^^^^^^^^^^^^^ W505
3 |
4 |     def __init__(self):
  |

example.py:6:81: W505 Doc line too long (100 > 80)
  |
4 |     def __init__(self):
5 |         pass
6 |         # print("This is some code that is less than 119 characters and that we commented out.")
  |                                                                                 ^^^^^^^^^^^^^^^^ W505
  |
$ ruff --version
ruff 0.9.3

Activity

  1. ntBre commented on Jan 24, 2025

    @ntBre
    Contributor

    It looks like we inherited this behavior from pycodestyle, which also applies it to comments:

    https://github.com/PyCQA/pycodestyle/blob/368d9dc21496e4cbb26a00b9e167deed39865781/pycodestyle.py#L1664-L1665

    So I'm not sure we'd really want to depart from that.

    It's a bit hacky, but as the docs suggest, you can avoid W505 on lines that match one of your lint.task-tags if you combine it with lint.pycodestyle.ignore-overlong-task-comments:

    ruff check --select W505 \
        --config 'lint.pycodestyle.max-doc-length = 80' \
        --config 'lint.pycodestyle.ignore-overlong-task-comments = true' \
        --config 'lint.task-tags = [""]' example.py

    The empty task-tag should match any comment.

    This reports the error for the docstring but not for the comment.

  2. added
    question
    on Jan 25, 2025
  3. martimlobao commented on Jan 25, 2025

    @martimlobao
    Author

    @ntBre Thanks for the reply! That's interesting, I'm fairly sure I didn't used to get these warnings for comments in the past before switching to ruff, but maybe I wasn't using pycodestyle. In that case, it looks like this is behaving as intended (i.e. it matches pycodestyle's behavior), and with your suggested workaround I'm sufficiently satisfied that I think I can close this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    question

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions