deprecated - How to print the calling method name and line number for Deprecation Warning message in python? -


my deprecate warning message in function. print out module name , line number of calling function can locate them. take open() example:

/path/to/file/group.py:180: deprecationwarning: 'u' mode deprecated  open(tmpname, 'ru') csvfile 

however, own warning prints this:

/path/to/file/models.py:735: deprecationwarning: deprecated. use course_id instead!  warnings.warn("deprecated. use course_id instead!", deprecationwarning) 

on line 735 of models.py warnings.warn() call located. there way make warning output parent caller's name , line number? thanks.

you can control caller want warning apply using stacklevel argument.

for instance, following code:

import warnings  def hello(s):     if isinstance(s, int):         deprecation('use of integers deprecated.')     else:         print(s)  def deprecation(msg):     warnings.warn(msg, deprecationwarning, stacklevel=3)  hello(1) 

will give following warning:

warning_test.py:12: deprecationwarning: use of integers deprecated.   hello(1) 

see: python documentation on warnings.warn


Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -