-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
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
filterwarnings
causes warnings.warn
to raise TypeError
on non-string messages
#103577
Comments
I'm happy to help contribute a fix if ya'll agree this behavior isn't ideal :) |
Are there any real-life use-cases where using Docs also say the same: https://docs.python.org/3/library/warnings.html#warnings.warn In you case you can use: >>> with warnings.catch_warnings():
... warnings.filterwarnings("ignore", "test")
... warnings.warn(Warning(1))
...
<stdin>:3: Warning: 1 I don't think that allowing more types in is a good thing. |
Hey @sobolevn — I agree that it doesn't make sense to allow more types but it is very surprising for the behavior to change when you enable filtering. This means that you can have a library that works totally fine until warning filtering is used then suddenly a type error will be thrown. In practice, I encountered this working on Before we added the |
Yes, this is an eventual side-effect of dynamic typing. It all works untill it does not :) Typechecker should catch errors like this. I don't think that anything should be done in this case from CPython's side. Do you agree? Maybe you have ideas of better docs wordings? |
I hesitantly agree :) it's a sneaky problem since the error message / traceback does not include any context about filtering. Perhaps:
|
IMO we should change from "errors if you called |
@Zac-HD I'm happy with that too — seems like a breaking change but a reasonable one |
Bug report
When using
filterwarnings
, aTypeError
is raised when a non-string value is passed towarnings.warn
which differs from the default behavior ofwarnings.warn
.The traceback does not demonstrate that this is a detail of warning filtering:
Filtering warnings should not change the accepted types of
warnings.warn
.Reproduction example at https://github.com/madkinsz/cpython-103577
Originally raised at pytest-dev/pytest#10865
Your environment
Reproduced Ubuntu with Python 3.7-3.12: https://github.com/madkinsz/cpython-103577/actions/runs/4714383423/jobs/8360711980
Also reproduced manually on macOS with Python 3.10/3.11
The text was updated successfully, but these errors were encountered: