This rule raises an issue when pytest or one of its subpackages is imported with from pytest import …, instead of a
plain import 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.
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 … 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')