Allow using warnings.warn()
with a Warning
#11959
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
warnigns.warn()
expects its first argument to be astr
or aWarning
(see alsoWarningMessage
in typeshed) and using a different type can cause difficult to diagnose errors (as was reported in #10865):Recently #11804 tried to help address this problem by making
pytest.warns()
check the type that was passed towarnings.warn()
, but the checks are now too strict:Both uses are explicitly permitted by Python. Furthermore, in the second case the
IndexError
is completely unexpected.With my patch
pytest.warns()
allowswarnings.warn()
to be used with aWarning
instance, except if aUserWarning
is created by passing it arguments and the first argument is not astr
. This is because if an invalid type was used inwarnings.warn()
then Python creates aUserWarning
anyways and it becomes impossible forpytest
to figure out if that was done automatically or not. However, even with that shortcomingpytest.warns()
still behaves much better than it does right now.Closes #11954
Here is a quick checklist that should be present in PRs.