This is a bit hacky, but you can defuse the warnings.warn method as follows:
import traceback import warnings def g(): warnings.warn("foo", Warning) def f(): g() warnings.warn("bar", Warning) _old_warn = warnings.warn def warn(*args, **kwargs): tb = traceback.extract_stack() _old_warn(*args, **kwargs) print("".join(traceback.format_list(tb)[:-1])) warnings.warn = warn f() print("DONE")
This is the conclusion:
/tmp/test.py:14: Warning: foo _old_warn(*args, **kwargs) File "/tmp/test.py", line 17, in <module> f() File "/tmp/test.py", line 8, in f g() File "/tmp/test.py", line 5, in g warnings.warn("foo", Warning) /tmp/test.py:14: Warning: bar _old_warn(*args, **kwargs) File "/tmp/test.py", line 17, in <module> f() File "/tmp/test.py", line 9, in f warnings.warn("bar", Warning) DONE
See that calling the original warnings.warn function warnings.warn not report the line you want, the trace pin is really correct (you can print the warning yourself).
source share