ruff: ERA001: autofix removes valid comments that do not contain code or removes not everything

Example:

# Example of headers:
# X-token: <token_id>
# X-scope: no-scope
# End of comment about headers


# {
#    "some-key": {
#         '1': 1,
#         '2': 2',
#     }
# }


# some = 1
# awesome = 2
#
# foobar = 3
# barfoo = 4


# example code from IPython:
# In [20]: parse.parse_qsl('&l=55.123;123.1&count=3', keep_blank_values=1)
# Out[20]: [('l', '55.123'), ('123.1', ''), ('count', '3')]
# End of comment


# (пример, пример2) = (example1, example2)


# if not success:  # TODO: comment
#    errors.append('Ошибка: error')


# comment
# 1. [{пример первый}]
# 2. [[пример]]
# 3. [] - пусто


# Пример: {"description": "weight: "}


# comment about calculation
# 100 / 2 = 50
# end of comment


# comment:
# - tuple: ()
# - list: []
# - set: set()
# - dict: {}


# example of output:
# 'OK: message sent. Message-ID: 123456'


# MySQL: enum('positive', 'negative')


# query: lock + user.id

Ruff output:

$ ruff --fix --select ERA001 ruff_errors/ERA001.py
Found 24 errors (24 fixed, 0 remaining).

Contents of the file after fix:

# Example of headers:
# X-token: <token_id>
# End of comment about headers


#    "some-key": {


#


# example code from IPython:
# End of comment




# if not success:  # TODO: comment


# comment
# 1. [{пример первый}]
# 3. [] - пусто




# comment about calculation
# end of comment


# comment:


# example of output:





About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 19 (16 by maintainers)

Commits related to this issue

Most upvoted comments

Is there scope for a PR for better algorithms, and a better set of false positive rules here?

It will deviate from the eradicate plugin, would that be okay or should it be a different rule entirely then?

I don’t think we’re in a position to improve this much, the logic itself is derived from eradicate, and I’d bet eradicate has the same or very similar behavior. Perhaps we should remove the autofix from this rule though. It seems correct to flag that there’s some commented-out code here, but rely on users to remove it.

Alternatively, we could only autofix if all lines in a comment block are identified as code.

#  Checking if the first default_path is there in api_paths if true then, return the action i.e create, read, update, delete

this is the comment I wrote, it contains return in this sentence only because of that, it is raising a warning

@jaap3 - Funnily enough that specific case was reported recently and is fixed on main.

We’ve updated the applicability of the fix here to “Manual” which will never automatically apply once we merge #5119 — I don’t think there’s more to do here this issue will be resolved once applicability is respected by the CLI per https://github.com/astral-sh/ruff/issues/4185

I like

Alternatively, we could only autofix if all lines in a comment block are identified as code.

Perhaps we could use Suggested in that case and Manual otherwise?