This rule raises an issue when pytest is imported with from pytest import …​ or under an alias such as import pytest as …​, instead of a plain import pytest.

In Python, this refers to imports such as:

Why is this an issue?

Pytest is conventionally imported as a module: import pytest. Members are then accessed as pytest.raises, pytest.mark, pytest.fixture, and so on.

from pytest import …​

Importing selected names from pytest scatters short unbound names through the test file:

from pytest import mark, raises

@mark.skip
def test_invalid_input():
    with raises(ValueError):
        process_data('hello')

Readers must check the import list to know that mark and raises come from pytest. Mixing import pytest in some files with from pytest import …​ in others makes the suite harder to skim and to search.

Aliased imports

Renaming the module hides the standard name:

import pytest as pt

def test_invalid_input():
    with pt.raises(ValueError):
        process_data('hello')

import pytest keeps every call site consistent across the project.

Preferred form

import pytest

@pytest.mark.skip
def test_invalid_input():
    with pytest.raises(ValueError):
        process_data('hello')

What is the potential impact?

Inconsistent pytest imports force maintainers to resolve where helpers come from and slow down reviews. A single import pytest style keeps test APIs recognizable and easier to search across the suite.

How to fix it

Replace from pytest import …​ and aliased imports with import pytest, then qualify members as pytest…​..

Code examples

Noncompliant code example

from pytest import mark, raises  # Noncompliant

@mark.skip
def test_invalid_input():
    with raises(ValueError):
        process_data('hello')

Compliant solution

import pytest

@pytest.mark.skip
def test_invalid_input():
    with pytest.raises(ValueError):
        process_data('hello')

Noncompliant code example

import pytest as pt  # Noncompliant

def test_invalid_input():
    with pt.raises(ValueError):
        process_data('hello')

Compliant solution

import pytest

def test_invalid_input():
    with pytest.raises(ValueError):
        process_data('hello')

Resources

Documentation