New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
warnings: -W option and PYTHONWARNINGS now use the message as a regex #88028
Comments
I propose to change the -W command line option and the PYTHONWARNINGS environment variable to use the message as a regular expression in Python 3.10. Or does anyone have a reason to keep the current behavior as it is? -- Python provides two ways to specify warnings filters:
While the Python API warnings.filterwarnings(action, message="", ...) uses the message as a regular expression, -W and PYTHONWARNINGS require to match *exactly* the *whole* message. For example, if you only want to ignore the new distutils deprecation warning, you must write exactly: $ ./python -X dev -W 'ignore:The distutils package is deprecated and slated for removal in Python 3.12. Use setuptools or check PEP 632 for potential alternatives:DeprecationWarning' -c 'import distutils' I use -X dev to show DeprecationWarning, or you can also use -Wdefault if you prefer. If the deprecation warning changes in Python or if you have a single typo, the warning is not ignored. Example with a typo ("3.13" rather than "3.12"): $ ./python -X dev -W 'ignore:The distutils package is deprecated and slated for removal in Python 3.13. Use setuptools or check PEP 632 for potential alternatives:DeprecationWarning' -c 'import distutils'
<string>:1: DeprecationWarning: The distutils package is deprecated and slated for removal in Python 3.12. Use setuptools or check PEP 632 for potential alternatives The PYTHONWARNINGS has another limitation: you cannot specify a message if it contains a comma (","). Hopefully, Python doesn't raise warnings containing comma, right? Well... Just one example: Lib/distutils/sysconfig.py:554: warnings.warn('SO is deprecated, use EXT_SUFFIX', DeprecationWarning, 2) You cannot only ignore the message: $ PYTHONWARNINGS='ignore:SO is deprecated, use EXT_SUFFIX:DeprecationWarning' ./python -c 'import sys; print(sys.warnoptions); print(len(sys.warnoptions))'
Invalid -W option ignored: invalid action: 'use EXT_SUFFIX'
['ignore:SO is deprecated', ' use EXT_SUFFIX:DeprecationWarning']
2 You can only try to use "module" and "lineno" parameters of a warning filter, which are more fragile and hard to use to use. |
See also https://bugs.python.org/issue34624 |
Sadly, the current -W option documentation is wrong :-( I started by writing PR 25439 to fix the documentation, before changing its behavior. |
Hum, "wrong" may be too strong. I would say that it is at least misleading. It starts with "file:line: category: message". I understood that it was the format the option, whereas it is not. Moreover, the full format is simply... not documented at all! Only the short -Waction format :-( |
I also created a thread on python-dev: |
I mark this issue as a duplicate of bpo-34624. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: