PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, April 28, 2022

[FIXED] How does -W interact with DeprecationWarning and PendingDeprecationWarning?

 April 28, 2022     deprecated, deprecation-warning, python, python-3.x, warnings     No comments   

Issue

How does one use DeprecationWarning and PendingDeprecationWarning to inform developers/tests (but not end users) of an upcoming deprecation along with the python foo.py -W controls?

The following snippet is an isolated repro of what doesn't work as I expect it to, but also I can't figure out how to make -W work at all.

test_deprecation.py

from warnings import warn

def warningfunction():
    warn("this is deprecated", DeprecationWarning, 2)

def pendingfunction():
    warn("pending", PendingDeprecationWarning, 2)

def test_warning():
    warningfunction()

def test_pending():
    pendingfunction()


if __name__ == '__main__':
    warningfunction()
    pendingfunction()

Running as pytest test_deprecation.py -vv, both warnings come as expected. This is good at least!

Running as python test_deprecation.py, get only the first warning (because it's in main). This is what I would expect as well!

Running as python test_deprecation.py -Wa, still only get the first warning. This is the part I don't expect. Isn't -Wa supposed to turn on all warnings?

Running as python test_deprecation.py -Wi (ignore warnings), still only get the first warning. Also not expected. Isn't -Wi supposed to ignore all warnings?

-Wdefault  # Warn once per call location
-Werror    # Convert to exceptions
-Walways   # Warn every time
-Wmodule   # Warn once per calling module
-Wonce     # Warn once per Python process
-Wignore   # Never warn 

The action names can be abbreviated as desired (e.g. -Wi, -Wd, -Wa, -We) and the interpreter will resolve them to the appropriate action name.

from https://docs.python.org/3/using/cmdline.html#cmdoption-w

(python3.8 btw)

echo $PYTHONWARNINGS is empty


Solution

You're passing -Wa and -Wi as arguments to the script. You need to put them before the script name to treat them as options for the interpreter itself.



Answered By - user2357112
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing