Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyCharm noinspection for whole file?

Is it possible to disable an inspection for the whole file in PyCharm?

The reason this is needed is when dealing with py.test. It uses fixtures which appear to shadow function parameters, and at the same time cause unresolved references. e.g.:

from myfixtures import user  # Unused import statement warning

def test_is_awesome(user):  # Shadows name 'user' from outer scope warning
    assert user.is_awesome()

There is also other warnings from py.test, such as using pytest.raises() causes a "Can not find reference 'raises'" in pytest.py.

Maybe there's another way to fix these problems? Maybe I'm using py.test incorrectly?

like image 831
gak Avatar asked Apr 09 '14 02:04

gak


People also ask

How do I ignore a warning in PyCharm?

Disable inspectionsIn the Settings/Preferences dialog ( Ctrl+Alt+S ), select Editor | Inspections. Locate the inspection you want to disable, and clear the checkbox next to it. Apply the changes and close the dialog.

Does PyCharm follow PEP8?

Tracking PEP8 rules So, as you can see, PyCharm supports PEP8 as the official Python style guide. If you explore the list of inspections ( Ctrl+Alt+S - Inspections), you will see that PyCharm launches the pep8.py tool on your code, and pinpoints the code style violations.

Which Linter does PyCharm use?

By default, PyCharm uses the TSLint package from the project node_modules folder and the tslint. json configuration file from the folder where the current file is stored. If no tslint. json is found in the current file folder, PyCharm will look for one in its parent folders up to the project root.

What is inspection in PyCharm?

Code inspections In PyCharm, there is a set of code inspections that detect and correct abnormal code in your project. The IDE can find and highlight various problems, locate dead code, find probable bugs, spelling problems, and improve the overall code structure.


3 Answers

Is it possible to disable an inspection for the whole file in PyCharm?

Yes. This answer is for this question only (and not about "Maybe there's another way to fix these problems? Maybe I'm using py.test incorrectly?").

  1. "Settings | Scopes"
  2. Create new scope that would include such "unwanted" file(s)
  3. "Settings | Inspections"
  4. Find "problematic" inspection
  5. Right click and choose "Add scope"
  6. Disable that inspection for that specific scope

Alternatively (may work or may not: depends on actual inspection .. and I'm not sure if it actually works in PyCharm this way -- not a PyCharm user myself unfortunately)

  1. Alt + Enter while caret is standing on error/warning place in your code
  2. Select correct entry from appeared popup menu
  3. Using Arrow Right key expand submenu
  4. Look for "Suppress inspection" option

This is how it looks in PhpStorm (screenshot shows "suppress for statement" option and not "suppress for whole file"):

enter image description here

Related: https://stackoverflow.com/a/20803118/783119

like image 200
LazyOne Avatar answered Sep 21 '22 06:09

LazyOne


To answer the "Maybe I'm using py.test incorrectly?" question:

Importing fixtures is not the best pattern to follow. Instead it is better to put fixtures in a conftest.py file of the package which needs them. If a fixture is used in two packages just put a conftest.py in their parent directory and put the fixture in there. This should get rid of the unused import and shadowing warnings.

As for the pytest.raises namespace issue, I don't think there's a solution to this currently. This is something that pylint suffers as well (and I think there's an effort to create a py.test plugin to pylint to address these things). So I think at the end of the day the linter will still need to know a little about py.test.

like image 20
flub Avatar answered Sep 21 '22 06:09

flub


I found that putting the per statement inspection:

    ...
    # noinspection PyUnusedLocal
    instance = mommy.make(Contact)

at the start of the file suppressed it for the whole file:

# noinspection PyUnusedLocal
from django.urls import reverse
like image 43
hum3 Avatar answered Sep 21 '22 06:09

hum3