55
votes

PyCharm provides some helpful warnings on code style, conventions and logical gotchas. It also provides a notification if I try to commit code with warnings (or errors).

Sometimes I consciously ignore these warnings for particular lines of code (for various reasons, typically to account for implementation details of third-party libraries). I want to suppress the warning, but just for that line (if the warning crops up on a different line where I'm not being deliberate, I want to know about it!)

How can I do that in PyCharm? (Following a universal Python convention strongly preferable.)

2
It's nice that there's a way to do it in PyCharm, but since not all of my teammates use PyCharm, I was really hoping for an equivalent to Java's @SuppressWarnings annotation, to indicate 'yeah, this line of code looks bad, but it's okay, I promise'. stackoverflow.com/a/14463362/1450294 ← This answer looked hopeful, but I still couldn't get the 'Too broad exception clause' warnings to go away when I'm logging the exceptions.Michael Scheper
@Michael Scheper: AFAIK, it is not PyCharm specific suppression but rather pylint suppression. As long as all of you use pylint you should get what you expect.sophros
@sophros: I know this is a bit off-topic (because the question is specifically about PyCharm), but can you elaborate on your reply? Is there a way to tell pylint to ignore particular warnings for particular lines of code, within the code?Michael Scheper
@MichaelScheper: after a thorough check I must admit - I was wrong above - there have to be used separate commands for PyCharm and pylint explicitly. This is all depicted in the answer to the question about both.sophros

2 Answers

40
votes

To suppress PyCharm code inspections for a particular line of code you can use the following construct:

# noinspection INSPECTION_NAME
your_line_of_code_to_suppress

where the name of the inspection (INSPECTION_NAME above) you can take from the list of inspection names (they are pretty descriptive).

To suppress pylint command line messages explicitly you have to use different comments/commands, as described here (pylint error names).

33
votes

Essentially, for the file in which you want to suppress the warning, run it through code inspection, post which PyCharm will give you warnings in that particular file. You can then review the warnings and tell PyCharm to suppress warning for a particular statement.

Code Inspection can be accessed from Code-->Inspect Code menu from where you can select the file that you want to inspect.

Below is an image that shows how to do it for an image, after running code via CodeInspection Code Inspection Output

Link for more details around suppressing warnings: https://www.jetbrains.com/help/pycharm/2016.1/suppressing-inspections.html#1