-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Conversation
Let's play it safe and target
FWIW, I vaguely recall I have tried to replace |
yeah the tricky bit here is in python3 when python2 doesn't have the same concept -- that said, this appears to already be broken for 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 |
Cool, I checked that this also fixes #2917 👍 |
@nicoddemus nice! mergeable? |
Hold on! |
return func(*args, **kwargs) | ||
|
||
|
||
class _DeprecatedCallContext(object): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
src/_pytest/recwarn.py
Outdated
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 |
There was a problem hiding this comment.
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
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! 😁 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Many thanks @asottile!
nice! |
Resolves #4102
I'm targetting
features
because it changes the exception type and message. Though it ~could be considered a bugfix by stretch and targetmaster
instead?Edit: Resolves #2917