Possible to comment a pylint annotation

Viewed 377

I would like to disable a warning in pylint and also say why I'm disabling it:

from typing import Union, NoReturn, Optional # pylint: disable=unused-import (unused because hack for forward declaration!)

But this gives me the error:

bad-option-value: Bad option value 'declaration'

Is there a way to comment/provide context on why a warning is disabled? Or does it have to be on another line in order to do that?

3 Answers

The source code for parsing these "pragma" comments is here:

https://github.com/PyCQA/pylint/blob/master/pylint/utils/pragma_parser.py

It says:

Allow stopping after the first semicolon/hash encountered, so that an option can be continued with the reasons why it is active or disabled.

So yes, it is possible.

If I understand this code correctly, you should be able to use

# pylint: disable=unused-import # unused because hack for forward declaration!

or

# pylint: disable=unused-import ; unused because hack for forward declaration!

I'm not sure if this is the 'correct' answer but from a bit of trial and error, adding another comment char # after the annotation/disable allows stops the interpretation of the pylint. For example:

from typing import NoReturn, Optional # pylint: disable=unused-import # my comment here...
Related