-
-
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
Warns #897
Warns #897
Conversation
@@ -120,6 +120,9 @@ | |||
2.7.3 (compared to 2.7.2) | |||
----------------------------- | |||
|
|||
- Add 'warns' to assert that warnings are thrown (like 'raises'). |
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.
I think this should be in the changelog for 2.8
.
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.
Oh yeah, sorry, I rebased and forgot to move it.
(RuntimeWarning, SyntaxWarning), | ||
lambda: warnings.warn('w3', UserWarning))) | ||
|
||
@pytest.mark.skipif('sys.version < "2.5"') |
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.
This is not necessary as pytest supports 2.6+
only. 😄
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.
Oh, sweet! Again, just copying from raises.py
.
Thanks for the PR @hunse! 😄 |
Hmmm could you write some docs for |
ed55cb0
to
684139b
Compare
Okay, doc is added. |
warnings.warn("another warning", RuntimeWarning) | ||
|
||
assert len(record) == 1 # check that only one warning was raised | ||
assert record[0].message.message == "another warning" |
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.
message.message
? Is this a typo?
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.
No, that's actually the way you have to do it, because of how warnings.WarningMessage
is set up. WarningMessage.message
returns the actual exception, not the message, so you have to call message
again. Though your question made me realize that in Python3, warnings no longer have a message
attribute, just an args
one, so I've changed things to work in Python3 as well.
Nice @hunse, much appreciated! If nobody has any more comments, I will merge this tomorrow. 😄 |
3e6a12c
to
8dc6102
Compare
Okay, changed the docs around and added the link to |
Hmm good point. I see two options:
After your points I'm thinking the latter. What do you think? |
Yeah, I'm thinking the same thing. I was also wondering if it would be good to have the same interface for warnings recorded by |
Though it might be nice to have something after |
I think so, but don't know how difficult that would be to implement.
Agreed. How about a one liner after |
Okay, |
warnings.warn_explicit = oldwarn_explicit | ||
warnings.warn = oldwarn | ||
if not l: | ||
__tracebackhide__ = True |
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.
Keep __tracebackhide__
in the body of the function, this way when it fails the traceback will point to the point of the call, not to the internal details of this function.
@hunse excellent work, thanks for the nice refactoring! 😄 |
About the docs, I think it makes sense to add |
Agreed. |
warnings.simplefilter('default') | ||
def reset_filters(): | ||
warnings.filters[:] = oldfilters | ||
request.addfinalizer(reset_filters) | ||
wrec = WarningsRecorder() |
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.
Minor: this fits nicely as a yield_fixture:
@pytest.yield_fixture
def recwarn():
wrec = WarningsRecorder()
with wrec:
warnings.simplefilter('default')
yield wrec
(should've seen it before, sorry! 😅)
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.
Oh wow, that's cool. I didn't even know that was possible!
return iter(self._list) | ||
|
||
def __len__(self): | ||
return len(self._list) |
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.
@nicoddemus: how would I add documentation for these kinds of methods in recwarn.txt
? There seems like there should be a clearer way than just adding them as .. method::
.
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.
You can use auto
directives from sphinx... you can see them in action here.
Works in a similar manner to `raises`, but for warnings instead of exceptions. Also refactored `recwarn.py` so that all the warning recording and checking use the same core code.
@hunse could you fix the flakes error? After this I think we can merge this. 😄 |
I have no idea why it's getting that error. The Travis build says it ran on bd3877b, which is not the most recent update. Can you force it to rerun? There's no reason that it should not be able to import |
Oh I didn't even saw the error when I commented... it seems like travis misinterpreted some rebase done in the branch. I tried re-submitting the job, but it insists on running against commit bd3877b... Anyway, I just tested it and locally and everything passes, so we can ignore that error. 😄 |
Thanks a lot for the work @hunse! Cheers, |
Hm - it seems the same error shows up on Travis now that it's merged to master. |
Oh dear... I will take a look at this later then. No idea why flakes is acting up (if anybody wants to take a shot a it, please be my guest); |
The other thing that is very confusing (and makes it very annoying to debug) is that this error doesn't seem to appear when running |
Travis uses tox for the tests, so it should use a recent pyflakes version. Maybe try running |
I can actually reproduce this when doing @hpk42 - can you force an update? Can we somehow make tox only use devpi for the needed packages (pylib I guess?) and use PyPI directly for everything else? |
…lakes issue See pytest-dev#897 for discussion
As a workaround, I moved |
Seems like this PR broke issue #1037 |
`deprecated_call` now looks for PendingDeprecationWarnings, as it did previously but was broken by pytest-dev#897. Fixes pytest-dev#1037. Also added a test so this does not happen again.
`deprecated_call` used to accept any warning. As of pytest-dev#897, it is now specific to DeprecationWarnings, and another commit in this PR extends this to PendingDeprecationWarnings. This commit makes sure this stays the case.
`deprecated_call` used to accept any warning. As of pytest-dev#897, it is now specific to DeprecationWarnings, and another commit in this PR extends this to PendingDeprecationWarnings. This commit makes sure this stays the case.
The
recwarn
function argument is useful, but sometimes I just want to make sure that a warning of a particular type is thrown, and this takes a number of lines withrecwarn
. I added awarns
fixture to ensure that warnings are thrown. Similar toraises
, but for warnings.