Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get warnings.warn to issue a warning and not ignore the line?

I'm trying to raise a DeprecationWarning, with a code snippet based on the example shown in the docs. http://docs.python.org/2/library/warnings.html#warnings.warn

Official

def deprecation(message):     warnings.warn(message, DeprecationWarning, stacklevel=2) 

Mine

import warnings warnings.warn("This is a warnings.", DeprecationWarning, stacklevel=2) is None  # returns True 

I've tried removing the stacklevel argument, setting it to negative, 0, 2 and 20000. The warning is always silently swallowed. It doesn't issue a warning or raise an exception. It just ignores the line and returns None. The docs doesn't mention the criteria for ignoring. Giving a message, makes warnings.warn correctly issue a Userwarning.

What can be causing this and how do I get warn to actually warn?

like image 484
Damgaard Avatar asked Jan 06 '14 22:01

Damgaard


People also ask

How do I enable warnings in Python?

The warn() function defined in the ' warning ' module is used to show warning messages. The warning module is actually a subclass of Exception which is a built-in class in Python. print ( 'Geeks !' )

How do I raise a Python warning without stopping?

I use the following simple function to check if the user passed a non-zero number to it. If so, the program should warn them, but continue as per normal. It should work like the code below, but should use class Warning() , Error() or Exception() instead of printing the warning out manually.

How do you filter warnings in Python?

Use the filterwarnings() Function to Suppress Warnings in Python. The warnings module handles warnings in Python. We can show warnings raised by the user with the warn() function. We can use the filterwarnings() function to perform actions on specific warnings.

What is DeprecationWarning?

DeprecationWarning errors are logged by the Node. js runtime when your code (or one of the dependencies in your code) calls a deprecated API. These warnings usually include a DEP deprecation code. They are logged using console.


1 Answers

From the docs:

By default, Python installs several warning filters, which can be overridden by the command-line options passed to -W and calls to filterwarnings().

  • DeprecationWarning and PendingDeprecationWarning, and ImportWarning are ignored.
  • BytesWarning is ignored unless the -b option is given once or twice; in this case this warning is either printed (-b) or turned into an exception (-bb).

By default, DeprecationWarning is ignored. You can change the filters using the following:

warnings.simplefilter('always', DeprecationWarning) 

Now your warnings should be printed:

>>> import warnings >>> warnings.simplefilter('always', DeprecationWarning) >>> warnings.warn('test', DeprecationWarning) /home/guest/.env/bin/ipython:1: DeprecationWarning: test   #!/home/guest/.env/bin/python 
like image 144
Maciej Gol Avatar answered Oct 01 '22 12:10

Maciej Gol