pythonruff

How to make ruff ignore comments in measuring the line length?


The line

demo_code = print("foo bar")  # some comments and this line length exceed 79 that i config.

is being formatted by ruff format to

demo_code = print(
    "foo bar"
)  # some comments and this line length exceed 79 that i config.

as it exceeds the value defined in line-length.

I want to make ruff count only command characters and ignore the comments.


Solution

  • The best (unique?) solution to ignore lengthy code line errors (E501) with ruff is using codetags in you code:

    # pyproject.toml
    [tool.ruff.lint.pycodestyle]
    ignore-overlong-task-comments = true
    
    [tool.ruff.lint]
    task-tags = ["HACK"]
    
    demo_code = print("foo bar")  # HACK: That is a so savvy solution. Now, I can write very lengthy inline comments without any problem :)