Can I get PyCharm to suppress a particular warning on a single line?

22,928

Solution 1

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).

Solution 2

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

Share:
22,928
lofidevops
Author by

lofidevops

Updated on July 08, 2022

Comments

  • lofidevops
    lofidevops almost 2 years

    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.)

  • Noumenon
    Noumenon over 4 years
    If I suppress a pylint error then Pycharm will not warn me about that error ever again? Is that what it's for?
  • sophros
    sophros over 4 years
    Yes. Suppressed pylint warning is not going to reappear unless you remove the suppression.
  • east825
    east825 over 4 years
    Actually, there is no need to use a dedicated list of available inspections for that matter. For every inspection warning there is a special quick fix right in the editor that inserts such a comment with the proper inspection ID automatically. It can be found as: "Show Context Actions" (Alt+Enter on Windows/Linux) -> some standard action to fix the problem -> submenu -> "Suppress for ..." For instance, Alt+Enter -> Remove statement -> Right arrow -> Suppress for statement will insert a suppressing comment for an unused local variable, etc.
  • Jérémie
    Jérémie about 4 years
    Thanks to @pylover for the amazing list of PyCharm names (and thanks to post owner for linking to them).
  • Matt-Mac-Muffin
    Matt-Mac-Muffin over 3 years
    Is there a way to disable inspection on a specific line of a multi-line statement (for instance, aside this call to lambda function myfunc( some_params, lambda: ..., # noinspection PyTypeChecker more_params) to remove typecheck in parameter where expected type is function and a lambda is passed, while keeping the typecheck on other parameters). It does not seem supported. Anyone knows a way?
  • sophros
    sophros over 3 years
    @Matt-Mac-Muffin: I don't think it is possible. But warrants a separate question in my view.