I'm issuing lots of warnings in a validator, and I'd like to suppress everything in stdout except the message that is supplied to warnings.warn()
.
I.e., now I see this:
./file.py:123: UserWarning: My looong warning message
some Python code
I'd like to see this:
My looong warning message
Edit 2: Overriding warnings.showwarning()
turned out to work:
def _warning(
message,
category = UserWarning,
filename = '',
lineno = -1):
print(message)
...
warnings.showwarning = _warning
warnings.warn('foo')
Use the logging module instead of
warnings
.There is always monkeypatching:
Monkeypatch
warnings.showwarning()
with your own custom function.Here's what I'm doing to omit just the source code line. This is by and large as suggested by the documentation, but it was a bit of a struggle to figure out what exactly to change. (In particular, I tried in various ways to keep the source line out of
showwarnings
but couldn't get it to work the way I wanted.)Just passing
line=None
would cause Python to usefilename
andlineno
to figure out a value forline
automagically, but passing an empty string instead fixes that.