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:
from pytest import mark, raises (and other from pytest import … forms, including subpackages)import pytest as pt (and other aliases that rename pytest)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.
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.
import pytest
@pytest.mark.skip
def test_invalid_input():
with pytest.raises(ValueError):
process_data('hello')
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.
Replace from pytest import … and aliased imports with import pytest, then qualify members as pytest…..
from pytest import mark, raises # Noncompliant
@mark.skip
def test_invalid_input():
with raises(ValueError):
process_data('hello')
import pytest
@pytest.mark.skip
def test_invalid_input():
with pytest.raises(ValueError):
process_data('hello')
import pytest as pt # Noncompliant
def test_invalid_input():
with pt.raises(ValueError):
process_data('hello')
import pytest
def test_invalid_input():
with pytest.raises(ValueError):
process_data('hello')