pytest icon indicating copy to clipboard operation
pytest copied to clipboard

Possible missing warning filters in pytest_configure

Open akhilramkee opened this issue 3 years ago • 4 comments

Is there a reason why we don't apply any other warning filters passed in pytest.ini. This seems like a possible bug,

https://github.com/pytest-dev/pytest/blob/c2b1d5b368a1fc829c6c06c700ae2ba9cda55794/src/_pytest/config/init.py#L1010

Output

user@system: ~/workspace/pytest-issue-reproducer/filter_config_warning$ pytest
/u/user/workspace/pytest-issue-reproducer/filter_config_warning/conftest.py:5: UserWarning: Some warning
  warnings.warn("Some warning", category=UserWarning)
================================================================================================================================== test session starts ===================================================================================================================================
platform linux -- Python 3.9.7, pytest-7.0.1, pluggy-1.0.0
benchmark: 3.4.1 (defaults: timer=time.perf_counter disable_gc=False min_rounds=5 min_time=0.000005 max_time=1.0 calibration_precision=10 warmup=False warmup_iterations=100000)
rootdir: /u/user/workspace/pytest-issue-reproducer/filter_config_warning, configfile: pytest.ini
plugins: html-3.1.1, typeguard-2.12.1, json-report-1.4.1, tavern-1.20.0, flaky-3.7.0, allure-pytest-2.9.45, Faker-10.0.0, mock-3.6.1, cov-3.0.0, pyfakefs-4.5.3, xdist-2.5.0, json-0.4.0, benchmark-3.4.1, memray-1.2.0, anyio-3.4.0, subtests-0.8.0, nbval-0.9.6, hypothesis-6.32.1, forked-1.4.0, metadata-1.11.0
collected 1 item

tests/test_dummy.py .                                                                                                                                                                                                                                                              [100%]

=================================================================================================================================== 1 passed in 0.35s ====================================================================================================================================

Warning is thrown before test session starts

/u/user/workspace/pytest-issue-reproducer/filter_config_warning/conftest.py:5: UserWarning: Some warning
  warnings.warn("Some warning", category=UserWarning)

Reproducer

conftest.py

import pytest
import warnings

def pytest_configure(config: pytest.Config):
    warnings.warn("Some warning", category=UserWarning)

Content -> pytest.ini

[pytest]
filterwarnings =
        ignore::UserWarning

Warning gets filtered out in testcases as expected.

Possible fix:

  • Applying filters after warnings.simplefilter('default') using apply_warning_filters

https://github.com/pytest-dev/pytest/blob/e54c6a1362589b32a2e63bb780192b86216ecec8/src/_pytest/config/init.py#L1709

akhilramkee avatar Jul 13 '22 10:07 akhilramkee

@nicoddemus , any views on this ?

akhilramkee avatar Jul 18 '22 16:07 akhilramkee

Hey @akhilramkee, sorry for not answering earlier.

I think this is mostly an oversight, can't think of a reason not to apply the filters at that stage.

nicoddemus avatar Jul 18 '22 16:07 nicoddemus

Thanks for replying in short notice! Would it be fine if I send in a PR to fix this?

akhilramkee avatar Jul 18 '22 16:07 akhilramkee

Hey @akhilramkee - sorry about the long delay! PRs always welcome 🙂

Zac-HD avatar Feb 17 '24 20:02 Zac-HD