Skip to content

Pytest 7 not ignoring warnings as instructed on pytest.ini #9643

@abravalheri

Description

@abravalheri
  • a detailed description of the bug or problem you are having
  • output of pip list from the virtual environment you are using
  • pytest and operating system versions
  • minimal example if possible

Problem

Hello, with the latest version of Pytest a series of new issues has started to pop-up to warn users (and plugin maintainers) about future changes on how Pytest will work. This is a great work done by Pytest maintainers, and pushes the community towards a better future.

However, after informing plugin maintainers about the upcoming changes, I would like to silence these warnings so I can focus in fixing the errors detected on the tests (otherwise the output is too big and difficult to scroll through to find useful information).

To solve this, I naturally tried to modify my pytest.ini file with a ignore filter for these warnings, however Pytest is still printing some of them even after I added the warning filters.

Example for reproduction

The following script can be used to reproduce the problem:

git clone https://github.com/pypa/sampleproject.git
cd sampleproject
sed -i '/^\s*pytest$/a \    pytest-flake8' tox.ini
sed -i '/^\s*pytest$/a \    pytest-black' tox.ini
sed -i 's/py.test tests/py.test tests --black --flake8/' tox.ini
cat << _STR > pytest.ini
[pytest]
filterwarnings=
    # Fail on warnings
    error

    # tholo/pytest-flake8#83
    # shopkeep/pytest-black#55
    # dbader/pytest-mypy#131
    ignore:<class '.*'> is not using a cooperative constructor:pytest.PytestDeprecationWarning
    ignore:The \(fspath. py.path.local\) argument to .* is deprecated.:pytest.PytestDeprecationWarning
    ignore:.* is an Item subclass and should not be a collector.*:pytest.PytestWarning
_STR
tox -e py38

Relevant output:

(...)
py38 run-test: commands[3] | py.test tests --black --flake8
/tmp/sampleproject/.tox/py38/lib/python3.8/site-packages/_pytest/nodes.py:664: PytestWarning: BlackItem is an Item subclass and should not be a collector, however its bases File are collectors.
Please split the Collectors and the Item into separate node types.
Pytest Doc example: https://docs.pytest.org/en/latest/example/nonpython.html
example pull request on a plugin: https://github.com/asmeurer/pytest-flakes/pull/40/
  warnings.warn(
/tmp/sampleproject/.tox/py38/lib/python3.8/site-packages/_pytest/nodes.py:664: PytestWarning: Flake8Item is an Item subclass and should not be a collector, however its bases File are collectors.
Please split the Collectors and the Item into separate node types.
Pytest Doc example: https://docs.pytest.org/en/latest/example/nonpython.html
example pull request on a plugin: https://github.com/asmeurer/pytest-flakes/pull/40/
  warnings.warn(
(...)

(The warnings seem to be shown only once per worker per plugin, but considering you have 32 workers with pytest-xdist and 4 plugins that haven't adapted to the changes yet, the output can be very verbose and difficult to navigate)

Expected behaviour

Pytest should not print the warnings:

  • Flake8Item is an Item subclass and should not be a collector...
  • BlackItem is an Item subclass and should not be a collector...

Environment information

$ .tox/py38/bin/python -V
Python 3.8.10


$ .tox/py38/bin/pip list
Package           Version
----------------- -------
attrs             21.4.0
black             22.1.0
build             0.7.0
check-manifest    0.47
click             8.0.3
flake8            4.0.1
iniconfig         1.1.1
mccabe            0.6.1
mypy-extensions   0.4.3
packaging         21.3
pathspec          0.9.0
pep517            0.12.0
peppercorn        0.6
pip               21.3.1
platformdirs      2.4.1
pluggy            1.0.0
py                1.11.0
pycodestyle       2.8.0
pyflakes          2.4.0
pyparsing         3.0.7
pytest            7.0.0
pytest-black      0.3.12
pytest-flake8     1.0.7
sampleproject     2.0.0
setuptools        60.5.0
toml              0.10.2
tomli             2.0.0
typing_extensions 4.0.1
wheel             0.37.1


$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.3 LTS
Release:        20.04
Codename:       focal


$ tox --version
3.24.4 imported from ~/.local/lib/python3.8/site-packages/tox/__init__.py

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions