See, this discuss thread for context. Based on CPython warnings.py@d8f40ead
.
Keep in mind, that the current implementation:
-
Assumes Python 3.11 or newer. (Uses the new
co_positions()
method) -
Is written in pure Python. (The C
_warning
extension loading is disabled) -
Is not well tested. (I haven't checked any
warnings
APIs apart fromwarnings.warn
) -
Is currently not backwards compatible. (I've added some new fields and parameters, which probably breaks monkey-patching)
You can see an example comparison between old and new warnings by running ./compare_warnings.sh
or ./compare_warnings_in_docker.sh
if you don't have Python 3.11 installed locally.
/long/path/to/code/that/may/end/up/wrapping/example.py:27: UserWarning: This is a regular warning.
warnings.warn("This is a regular warning.")
/long/path/to/code/that/may/end/up/wrapping/example.py:28: DeprecationWarning: old_baz is deprecated in version X, use new_baz instead.
bar(old_baz() + 1)
/long/path/to/code/that/may/end/up/wrapping/example.py:19: VeryImportantWarning: This is a very important warning, you need to fix it!
warnings.warn(
File "/long/path/to/code/that/may/end/up/wrapping/example.py", line 27, in foo
UserWarning: This is a regular warning.
File "/long/path/to/code/that/may/end/up/wrapping/example.py", line 28, in foo
bar(old_baz() + 1)
^^^^^^^^^
DeprecationWarning: old_baz is deprecated in version X, use new_baz instead.
File "/long/path/to/code/that/may/end/up/wrapping/example.py", line 19, in bar
VeryImportantWarning: This is a very important warning, you need to fix it!