-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Catch any warning on warns with no arg passed #8677
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
Conversation
I had a closer look at the failing test and I see now what is happening.
Instead, I kept the Let me know your thoughts and if this needs better documenting! |
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.
overall this looks nice - i do wonder about the case with None used in test_on_rm_rf_error
on_rm_rf_error(os.open, str(fn), exc_info4, start_path=tmp_path) | ||
assert fn.is_file() | ||
assert not [x.message for x in warninfo] | ||
with warnings.catch_warnings(): |
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.
should this test use a mix of warns/catch_warnings to begin with?
imho there should be s single context manager which ensures no warning happens
but if it shows tricky to spell different quickly its fine to make that a follow-up task
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.
That would be nice, but I am not sure if it is possible or how to do it.
As I understand we want to discourage folks from using None
, but we are maintaining the functionality (and this test) only for backwards-compatibility. I would also be inclined to say that this test case could be removed(!), so maybe not super important to worry about it?
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.
should this test use a mix of warns/catch_warnings to begin with?
It's a weird test, but I think that Olga's edits are good - we'll remove/change it in 7.0 when the warns(None)
deprecation is turned into an error.
imho there should be s single context manager which ensures no warning happens
but if it shows tricky to spell different quickly its fine to make that a follow-up task
IMO this is probably served with with warnings.catch_warnings():
(in the rare times that -Werror
isn't enough); and if not it's a separate issue rather than aditional work on this PR.
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.
Thanks Olga! Just a few fiddly changes to the type annotations so that they reflect the non-deprecated behaviour, and adding a comment so that later readers know when our new test should be removed. Then we're done, and I'll be happy to merge!
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.
Thanks @olgarithms for the PR!
Left a small suggestion on the message. 👍
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.
Last thing, then I'm happy to merge.
@@ -83,7 +84,7 @@ def deprecated_call( | |||
|
|||
@overload | |||
def warns( | |||
expected_warning: Optional[Union[Type[Warning], Tuple[Type[Warning], ...]]], | |||
expected_warning: Union[Type[Warning], Tuple[Type[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.
expected_warning: Union[Type[Warning], Tuple[Type[Warning], ...]] = ..., | |
expected_warning: Union[Type[Warning], Tuple[Type[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.
I had to add this again, as mypy was complaining that there is no overload of warns
that allows for no arguments to be passed. The ...
signifies the default argument that is passed in here.
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.
🎉
Congratulations and thanks again, Olga!
I am not sure if it is due to just this change or in collaboration with another change, but the suggested replacement is not equivalent as with pytest.warns(None) as record:
fig.canvas.draw()
assert len(record) == 0, "\n".join(str(e.message) for e in record) Which is probably a hold over from our migration from nose -> pytest a while back (and I'm about to open a PR to fix this and another case on the Matplotlib side), but it would be good if the deprecation message included a note about this difference (assuming it was intentional). |
Ah, I see - we should add some stdlib links to the relevant docs:
I didn't intend to break anything, but nor did I consider that people might be using |
https://xkcd.com/1172/ With enough users every bug is a feature and every bit of implementation detail in relied on by someone 🤷🏻♀️ . I run default branches of things to help catch these things early! I use pytest almost every day (it is the 8th most common command line tool I run behind things like |
Thanks for submitting a PR, your contribution is really appreciated!
Here is a quick checklist that should be present in PRs.
If this change fixes an issue, please:
pytest.warns(None)
#8645Unless your change is trivial or a small documentation fix (e.g., a typo or reword of a small section) please:
Create a new changelog file in the
changelog
folder, with a name like<ISSUE NUMBER>.<TYPE>.rst
. See changelog/README.rst for details.Write sentences in the past or present tense, examples:
Also make sure to end the sentence with a
.
.Add yourself to
AUTHORS
in alphabetical order.