Question

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?

Was it helpful?

Solution

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

Yes.

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

  1. Settings/Preferences | Appearance & Behavior | Scopes
  2. Create a new scope that would include such "unwanted" file(s)
  3. Settings/Preferences | Editor | Inspections
  4. Find that "problematic" inspection
  5. Select that newly created scope in the appropriate list (the old IDE versions had to use Right click and choose "Add scope")
  6. Disable that inspection for that specific scope.

An example from my PhpStorm 2022.3 (would be the same or very similar in PyChram):
enter image description here

enter image description here

Alternatively (should work: although this depends on the actual inspection... I'm not PyCharm user so cannot check it out)

  1. Use Alt + Enter while caret is standing on the error/warning place in your code (or via light bulb icon using a mouse)
  2. Select correct entry from appeared popup menu
  3. Using Arrow Right key expand the submenu (with mouse it can be quite tricky to do: the hit box is rather small)
  4. Look for Suppress inspection option
  5. It will add a special comment on the preceding line (or at the top of the file depending on what you choose) to ignore that inspection only

enter image description here (this is how it looks in PhpStorm; available options can differ between inspections)

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

OTHER TIPS

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.

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top