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
a detailed description of the bug or problem you are having
I tried to apply a custom format to some warnings generated during tests using the method described in a "Python module of the week"-post. This requires overriding the warnings.formatwarning method as is intended according to the package documentation.
Somehow this approach didn't work at all (custom formatting was not applied). During further investigation, I also tried to modify warnings.showwarning, which finally worked in changing the formatting.
BUT this now breaks pytest's detection of warnings (the warning with modified warnings.showwarning is not counted and doesn't show up in the warnings summary).
output of pip list from the virtual environment you are using
importsysimportwarningsdefformatwarning_without_trace(message, category, filename, lineno, line=None) ->str:
category=category.__name__returnf'{filename}:{lineno}: {category}: {message}\n'# same as original, but with custom formatwarningdefshowwarning_without_trace(message, category, filename, lineno, file=None, line=None): # noqa: PLR0913iffileisNone:
file=sys.stderriffileisNone:
# sys.stderr is None when run with pythonw.exe:# warnings get lostreturntext=formatwarning_without_trace(message, category, filename, lineno, None)
try: # noqa: SIM105file.write(text)
exceptOSError:
# the file (probably stderr) is invalid - this warning gets lost.passwarnings.warn(message=message, category=category)
# warning shows normally, is counted / appears in the summarydeftest_warning_warn():
warnings.warn('standard warning.warn')
# warning is counted / appears in the summary, but doesn't use the custom formattingdeftest_warning_formatwarning():
warnings_formatwarning=warnings.formatwarningwarnings.formatwarning=showwarning_without_tracewarnings.warn('custom warning.formatwarning')
warnings.formatwarning=warnings_formatwarning# warning uses custom formatting, but is not counted / is missing in the summarydeftest_warning_showwarning():
warnings_showwarning=warnings.showwarningwarnings.showwarning=showwarning_without_tracewarnings.warn('custom warning.showwarning')
warnings.showwarning=warnings_showwarning
Overriding formatwarning doesn't work because pytest doesn't format the warnings during the test, it stores them as "raw" data and only calls formatwarning in the pytest_warning_recorded hook. If you correctly override formatwarning around that hook (perhaps with a hookwrapper), that should work just fine.
As for overriding showwarning, this is basically equivalent to:
I tried to apply a custom format to some warnings generated during tests using the method described in a "Python module of the week"-post. This requires overriding the
warnings.formatwarning
method as is intended according to the package documentation.Somehow this approach didn't work at all (custom formatting was not applied). During further investigation, I also tried to modify
warnings.showwarning
, which finally worked in changing the formatting.BUT this now breaks
pytest
's detection of warnings (the warning with modifiedwarnings.showwarning
is not counted and doesn't show up in the warnings summary).pip list
from the virtual environment you are usingproduces
The text was updated successfully, but these errors were encountered: