问题
I am aware of the FAQ entry for PyLint which states that:
4.2 Is there a way to disable a message for a particular module only?
Yes, you can disable or enable (globally disabled) messages at the module level by adding the corresponding option in a comment at the top of the file:
# pylint: disable=wildcard-import, method-hidden # pylint: enable=too-many-lines
However, how can I locally suppress a message and then simply restore the previous state, prior to suppression. I don't want to have to repeat myself when restoring the previous state. Is there a way to achieve this?
A shortcoming of the way PyLint handles this appears to be that if I disable a particular message some-message
globally (e.g. command line or pylintrc
) and then I have the following Python code segment:
# pylint: disable=some-message
foo, bar, baz = 0, 1, 2
# ... the code that creates the warning
# pylint: enable=some-message
the documentation suggests that the first line would not have any effect (i.e. the already disabled message would remain disabled) while the last line would undo not just the local effect of the first line - whether it was benign or not due to global settings - but would also undo the command line option or pylintrc
setting.
What I am looking for is a functionality similar to that in MS Visual C++ for warnings:
#pragma warning(push) /* save current state of warning settings */
#pragma warning(disable:somewarning)
/* warning occurs in code here */
#pragma warning(pop) /* restore previous state of warning settings */
an more concise alternative is:
#pragma warning(disable:somewarning)
/* warning occurs in code here */
#pragma warning(default:somewarning)
Does either functionality exist in PyLint?
回答1:
There is no such functionnality in pylint. You may suggest it on its tracker though: https://bitbucket.org/logilab/pylint/issues
来源:https://stackoverflow.com/questions/26656086/in-pylint-is-there-a-way-to-locally-disable-a-warning-and-then-undo-the-previous