Skip to content

Warning integration breaks warnings.filterwarnings #2430

Closed
psf/requests
#4056
@zzzeek

Description

@zzzeek

SQLAlchemy CI is now broken due to the unconditional inclusion of pytest-warnings in py.test 3.1.0. I need at least to know how to disable this plugin entirely.

In our test suite we make use of the warnings filter internally in order to propagate our own warnings to errors. It seems like warnings.filter() is now non-functional when this plugin is installed.

Demo test case:

import warnings

class MyWarning(Warning):
    pass

warnings.filterwarnings("error", category=MyWarning)

class TestWarnings(object):
    def test_my_warning(self):
        try:
            warnings.warn(MyWarning("warn!"))
            assert False
        except MyWarning:
            assert True


with py.test 3.0.7:


$ py.test test_warning.py 
======================================================== test session starts ========================================================
platform linux2 -- Python 2.7.13, pytest-3.0.7, py-1.4.33, pluggy-0.4.0
rootdir: /home/classic/Desktop/tmp, inifile:
plugins: xdist-1.15.0, cov-2.4.0
collected 1 items 

test_warning.py .

===================================================== 1 passed in 0.01 seconds ======================================================

with py.test 3.1.0:

$ py.test test_warning.py 
======================================================== test session starts ========================================================
platform linux2 -- Python 2.7.13, pytest-3.1.0, py-1.4.33, pluggy-0.4.0
rootdir: /home/classic/Desktop/tmp, inifile:
plugins: xdist-1.15.0, cov-2.4.0
collected 1 items 

test_warning.py F

============================================================= FAILURES ==============================================================
___________________________________________________ TestWarnings.test_my_warning ____________________________________________________

self = <test_warning.TestWarnings object at 0x7fecb08a5290>

    def test_my_warning(self):
        try:
            warnings.warn(MyWarning("warn!"))
>           assert False
E           assert False

test_warning.py:12: AssertionError
========================================================= warnings summary ==========================================================
test_warning.py::TestWarnings::()::test_my_warning
  /home/classic/Desktop/tmp/test_warning.py:11: MyWarning: warn!
    warnings.warn(MyWarning("warn!"))

-- Docs: http://doc.pytest.org/en/latest/warnings.html
=============================================== 1 failed, 1 warnings in 0.03 seconds ================================================

I have tried everything I can think of with the new -W flag, disable-warnings, no luck.

Please advise on the correct options to allow Python stdlib warnings.filter() to work again, thanks!

Activity

changed the title [-]forcing pytest-warnings breaks the world[/-] [+]Warning integration breaks warnings.filterwarnings[/+] on May 23, 2017
zzzeek

zzzeek commented on May 23, 2017

@zzzeek
Author

how do I get a helper to help triage my bugs and change nasty headlines to nice ones? :(

nicoddemus

nicoddemus commented on May 23, 2017

@nicoddemus
Member

SQLAlchemy CI is now broken due to the unconditional inclusion of pytest-warnings in py.test 3.1.0. I need at least to know how to disable this plugin entirely.

@zzzeek you can disable the warnings plugin by passing -p no:warnings; this will disable the plugin completely and you should get green builds again. You can choose to add this to your pytest.ini file:

[pytest]
addopts = -p no:warnings

As for the problem itself, I will take a more careful look later.

added
type: bugproblem that needs to be addressed
type: regressionindicates a problem that was introduced in a release which was working previously
on May 23, 2017
zzzeek

zzzeek commented on May 23, 2017

@zzzeek
Author

great, thanks!

RonnyPfannschmidt

RonnyPfannschmidt commented on May 23, 2017

@RonnyPfannschmidt
Member

i believe https://github.com/pytest-dev/pytest/blob/master/_pytest/warnings.py#L54 is gutting all prior setups

we should just call reset_warning instead when we do something like that and give people a opt-out to allow respecting in code specifications

nicoddemus

nicoddemus commented on May 24, 2017

@nicoddemus
Member

@zzzeek thanks for posting the example, it helped understand the issue.

Can I ask why you add that warnings filter? If the objective is to turn certain warnings into errors, you can now configure your pytest.ini to the same effect. Raising more awareness about warnings was the motivation for this new feature.

Also, do you think we should add a blurb to the warnings docs making it easy for users to find how to disable it in case they run into trouble?

@RonnyPfannschmidt

i believe https://github.com/pytest-dev/pytest/blob/master/_pytest/warnings.py#L54 is gutting all prior setups

Actually the implementation uses catch_warnings, which saves the previous filters and restores them after the call (see https://github.com/python/cpython/blob/master/Lib/warnings.py#L454). Unfortunately this has the effect of silencing errors configured manually by other warnings.simplefilter calls, which is the heart of this issue. Also, by default Python will install a number of default filters silencing DeprecationWarnings, ImportWarnings and others, which are precisely the kind of warnings we believe should be displayed in test suites.

@RonnyPfannschmidt

we should just call reset_warning instead when we do something like that

resetwarnings() will actually throw out all filters:

(https://docs.python.org/3/library/warnings.html#warnings.resetwarnings)

This discards the effect of all previous calls to filterwarnings(), including that of the -W command line options and calls to simplefilter().

Which is not what we want, I think.

I'm not sure how to proceed here, so I would like to hear some opinions.

RonnyPfannschmidt

RonnyPfannschmidt commented on May 24, 2017

@RonnyPfannschmidt
Member

@nicoddemus pushing a new once simple filter is invalidating all other filter s due to matching first
so its literally pushing a new filter to be there

i beleive that by pushing a unfiltered once for all warning setups literally breaks all expectations,
people should opt in into breaking filter changes, not have to opt out

in retrospect the warnings addition is implemented in a way that warrants a major release, and the way we should fix it warrants a major release as well because to fix this issue we need to do a breaking change, and we never adopted a process of publishing experiments that we may change our mind on

53 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    plugin: warningsrelated to the warnings builtin plugintype: bugproblem that needs to be addressedtype: regressionindicates a problem that was introduced in a release which was working previously

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Participants

      @zzzeek@RonnyPfannschmidt@The-Compiler@Themanwithoutaplan@nicoddemus

      Issue actions

        Warning integration breaks warnings.filterwarnings · Issue #2430 · pytest-dev/pytest