Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement pytest.deprecated_call with pytest.warns #4104

Merged
merged 3 commits into from
Oct 11, 2018
Merged

Implement pytest.deprecated_call with pytest.warns #4104

merged 3 commits into from
Oct 11, 2018

Conversation

asottile
Copy link
Member

@asottile asottile commented Oct 10, 2018

Resolves #4102

I'm targetting features because it changes the exception type and message. Though it ~could be considered a bugfix by stretch and target master instead?

Edit: Resolves #2917

@nicoddemus
Copy link
Member

Let's play it safe and target features, it seems 3.9 is not far anyway. 😁

py27 tests have failed though, can you take a look at them?

FWIW, I vaguely recall I have tried to replace deprecated_call with warnings.catch_warnings in the past as a quick experiment, but I encountered some roadblock (which I don't remember right now) and gave up. But this is a vague memory, so not sure if it serves for anything, just thought I would mention. 🤷‍♂️

@asottile
Copy link
Member Author

yeah the tricky bit here is in python3 when catch_warnings.__enter__ is invoked, it increments the "warnings filter version" which in turn makes previously-warned warnings display again.

python2 doesn't have the same concept -- that said, this appears to already be broken for pytest.warns:

import pytest
import warnings

def f():
    warnings.warn(UserWarning('hi'))


def test():
    f()
    with pytest.warns(UserWarning):
        f()
$ ./pytest/venv2/bin/pytest  t.py
============================= test session starts ==============================
platform linux2 -- Python 2.7.15rc1, pytest-3.8.3.dev91+gaeb92acc, py-1.6.0, pluggy-0.7.1
hypothesis profile 'default' -> database=DirectoryBasedExampleDatabase('/tmp/.hypothesis/examples')
rootdir: /tmp, inifile:
plugins: hypothesis-3.75.4
collected 1 item                                                               

t.py F                                                                   [100%]

=================================== FAILURES ===================================
_____________________________________ test _____________________________________

    def test():
        f()
        with pytest.warns(UserWarning):
>           f()
E           Failed: DID NOT WARN. No warnings of type (<type 'exceptions.UserWarning'>,) was emitted. The list of emitted warnings is: [].

t.py:12: Failed
=============================== warnings summary ===============================
/tmp/t.py:5: UserWarning: hi
  warnings.warn(UserWarning('hi'))

-- Docs: https://docs.pytest.org/en/latest/warnings.html
===================== 1 failed, 1 warnings in 0.06 seconds =====================

I think I can monkeypatch my way out of this though and also fix the dupe warns problem for pytest.warns also

@coveralls
Copy link

coveralls commented Oct 10, 2018

Coverage Status

Coverage increased (+0.004%) to 93.787% when pulling bf265a4 on asottile:deprecated_call_match into 943bbdd on pytest-dev:features.

@nicoddemus
Copy link
Member

Cool, I checked that this also fixes #2917 👍

@asottile
Copy link
Member Author

@nicoddemus nice! mergeable?

@nicoddemus
Copy link
Member

Hold on!

return func(*args, **kwargs)


class _DeprecatedCallContext(object):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

img

return self

def __exit__(self, *exc_info):
if not self._entered:
__tracebackhide__ = True
raise RuntimeError("Cannot exit %r without entering first" % self)
# see above where `self.mp` is assigned
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: self.mp -> self._warn

btw we probably can name it something like self._saved_warn or something like that while we are at it

@nicoddemus
Copy link
Member

I found some minor things (see bf265a4), but instead of commenting in the PR I just went ahead and updated the PR myself as I had already checked out your branch for testing, hope you don't mind! 😁

Copy link
Member

@nicoddemus nicoddemus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many thanks @asottile!

@asottile
Copy link
Member Author

nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants