You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
pytest.warns hides type errors in warnings.warn calls #10865
a detailed description of the bug or problem you are having
[ ] output of pip list from the virtual environment you are using
pytest and operating system versions
minimal example if possible
When using pytest.warns, incorrect values sent to warnings.warn are not reported obscuring bugs:
importpytestimportwarningsdeftest_example_one():
withpytest.warns(UserWarning):
warnings.warn(1)
# This test passes but should raise a `TypeError` as demonstrated in `example_two`deftest_example_two():
withpytest.raises(TypeError):
warnings.warn(1)
The issue appears to be related to filterwarnings, for example:
importwarnings# Does not raise a type errorwarnings.warn(1)
# Raises a type errorwithwarnings.catch_warnings():
warnings.filterwarnings("ignore", "test")
warnings.warn(1)
@RonnyPfannschmidt the problem here is that when using filterwarnings the TypeError is raised outside of pytest but not raised inside pytest.warns blocks. This means that an error that can occur at runtime for users is hidden during execution of the test suite. I agree that this mistake would also be caught by a type-checker, but many people do not use static analysis tools and it feels rough to obscure the error that the standard library would raise.
Activity
nicoddemus commentedon Apr 4, 2023
Hmm that's unfortunate, thanks for the report!
Should be simple to fix.
zanieb commentedon Apr 4, 2023
You're welcome! Certainly confused me for a minute :)
I'm happy to contribute a fix — want to just raise a type error if it's not a string? Looks like CPython allows
Warningtypes too https://github.com/python/cpython/blob/f513d5c80672c76acbdaf7d5b601f4bbe9fae56a/Lib/warnings.py#L299-L301zanieb commentedon Apr 4, 2023
While trying to find the logic where this
TypeErroris raised I've gotten a bit confused. Weirdly this is finebut the
TypeErroris definitely raised in pytest i.e. if I remove thepytest.raises:I feel like this caused me an error outside of pytest as well though
nicoddemus commentedon Apr 5, 2023
Are you sure? This works for me:
Which Python and pytest versions are you using?
zanieb commentedon Apr 16, 2023
The Python and pytest versions are included in the original post, but consider me very confused — I did not reproduce this with the minimal example and a test matrix at https://github.com/madkinsz/pytest-10865/actions/runs/4714285419/jobs/8360551859
It does however reproduce quite clearly when I'm working in
PrefectHQ/prefect. Here's the example running in our test suite PrefectHQ/prefect#9231zanieb commentedon Apr 16, 2023
The issue appears to be related to
filterwarnings, for example:with the addition of a warnings filter to pytest zanieb/pytest-10865@1c3e4e3 the type error is raised on all Python and 7.x pytest versions https://github.com/madkinsz/pytest-10865/actions/runs/4714336351
zanieb commentedon Apr 16, 2023
I've opened a CPython issue as this doesn't really seem like pytest's fault python/cpython#103577 but pytest probably shouldn't hide this error still.
zanieb commentedon Jun 14, 2023
@nicoddemus I don't expect there to be changes upstream for this. Should we validate that argument is of the correct type in pytest?
RonnyPfannschmidt commentedon Jun 14, 2023
as far as im concerned, type metadata in typeshed is provided, and my understandig is, that it would point to the error
zanieb commentedon Jun 14, 2023
@RonnyPfannschmidt the problem here is that when using
filterwarningstheTypeErroris raised outside of pytest but not raised insidepytest.warnsblocks. This means that an error that can occur at runtime for users is hidden during execution of the test suite. I agree that this mistake would also be caught by a type-checker, but many people do not use static analysis tools and it feels rough to obscure the error that the standard library would raise.Zac-HD commentedon Aug 22, 2023
Retagging as bug based on upstream discussion, where consensus looks like "this should always error" and CPython may change accordingly.
whysage commentedon Jan 12, 2024
Hi, please review
#11804
whysage commentedon Jan 24, 2024
Hi, any comments on pull request?
#11804
@zanieb @Zac-HD