plinss / flake8-noqa

flake8 plugin to validate #noqa comments - Mirror of https://gitlab.linss.com/open-source/flake8/flake8-noqa
GNU Lesser General Public License v3.0
41 stars 6 forks source link

Sometimes noqa is not on the Exact Same Line #9

Closed erikvanderwerf closed 2 years ago

erikvanderwerf commented 2 years ago

Hello!

I have run into a tricky problem, where flake checks relating to docstrings and docstring formatting are raised inside multiline comments.

My environment approximates the packages below. I am using other libraries to make my problem easier to spot, but it does not strictly depend on them.

darglint                      ==1.8.1 # (DAR)
flake8                        ==4.0.1
flake8-noqa                   ==1.2.3 # (NQA)
flake8-docstrings             ==1.6.0 # (D)

example.py

"""Hello There."""

import asyncio

def foo(spam: str, task: asyncio.Task):
    """Does a thing.                   <-- Note that this is line 7.

    If task had an exception, raise it here.

    :param spam: String to print.
    :param task: Exception to raise.
    """  # noqa: D401, DAR401          <-- Note noqa line here, following docstring.
    print(spam)
    e = task.exception()
    raise e

As written, I get the output:

(venv) $ flake8 --docstring-style=sphinx --max-line-length=100 example.py 
example.py:13:10: NQA102 "# noqa: D401, DAR401          " has no matching violations

With the noqa in place, Flake8 is able to suppress D401 and DAR401 (coincidental codes) that relate to exceptions and docstring formatting. The lint errors are "produced" on line 7, you can see this if you remove the noqa. Flake must be performing some logic to detect the noqa at the end of a multiline comment, since it would be bad-form to put the noqa literally inside the docstring.

Would you be able to perform the same check so that I can keep my noqa after the docstring?

plinss commented 2 years ago

Can you try this with the previous version? (v1.2.2) I just made a change to how the noqa comments are tracked (to fix an opposite problem), and I think this is why I had done it the other way to begin with. I might have to re-think that change...

erikvanderwerf commented 2 years ago

Wow! I would not have guessed that this was so recent. I noticed that 1.2.3 was released only a few hours before I tried it for the first time!

(venv) $ pip install -q -U flake8-noqa==1.2.3 && pip list | grep noqa && flake8 --docstring-style=sphinx --max-line-length=100 example.py 
flake8-noqa                   1.2.3
example.py:13:10: NQA102 "# noqa: D401, DAR401          " has no matching violations
(venv) $ pip install -q -U flake8-noqa==1.2.2 && pip list | grep noqa && flake8 --docstring-style=sphinx --max-line-length=100 example.py 
flake8-noqa                   1.2.2