In Django I have a package that issues a depreciation warning (django.views.generic.simple). It would be useful if this warning described where the import was being made from, so the coder can go in and change the file without having to step through code to find it.
So the general case is
#file1.py
import file2.py
#file2.py
import warnings
warnings.warn(
'Package deprecated: imported from %s' % __importer__,
DeprecationWarning
)
Where __importer__ is an imaginary attribute containing “file1.py”, or some such reference.
Is there a way to do this?
Yes, this is done by using the
stacklevelargument towarnings.warn. See the example in the documentation for more info.