This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: warnings: -W option and PYTHONWARNINGS now use the message as a regex
Type: Stage: resolved
Components: Library (Lib) Versions: Python 3.10
process
Status: closed Resolution: duplicate
Dependencies: Superseder: -W option and PYTHONWARNINGS env variable does not accept module regexes
View: 34624
Assigned To: Nosy List: vstinner, xtreak
Priority: normal Keywords: patch

Created on 2021-04-16 13:06 by vstinner, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 25439 merged vstinner, 2021-04-16 13:18
Messages (7)
msg391190 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-04-16 13:06
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:

* -W command line option: can be used multiple times
* PYTHONWARNINGS environment variable: can contain multiple options separated by commas

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.
msg391193 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2021-04-16 13:17
See also https://bugs.python.org/issue34624
msg391194 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-04-16 13:19
Sadly, the current -W option documentation is wrong :-( I started by writing PR 25439 to fix the documentation, before changing its behavior.
msg391198 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-04-16 13:56
> 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 :-(

https://docs.python.org/dev/using/cmdline.html#cmdoption-W
msg391233 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-04-16 17:12
New changeset 62ec63864822de1dd1933225584e8503ac40c1f9 by Victor Stinner in branch 'master':
bpo-43862: Enhance -W cmdline option documentation (GH-25439)
https://github.com/python/cpython/commit/62ec63864822de1dd1933225584e8503ac40c1f9
msg391588 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-04-22 09:24
I also created a thread on python-dev:
https://mail.python.org/archives/list/python-dev@python.org/thread/JMKLA4RUJLTORBPPTM4BWL76VNNMKYVG/
msg408497 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-12-13 23:28
I mark this issue as a duplicate of bpo-34624.
History
Date User Action Args
2022-04-11 14:59:44adminsetgithub: 88028
2021-12-13 23:28:26vstinnersetstatus: open -> closed
superseder: -W option and PYTHONWARNINGS env variable does not accept module regexes
messages: + msg408497

resolution: duplicate
stage: patch review -> resolved
2021-04-22 09:24:43vstinnersetmessages: + msg391588
2021-04-16 17:12:21vstinnersetmessages: + msg391233
2021-04-16 13:56:00vstinnersetmessages: + msg391198
2021-04-16 13:19:32vstinnersetmessages: + msg391194
2021-04-16 13:18:52vstinnersetkeywords: + patch
stage: patch review
pull_requests: + pull_request24169
2021-04-16 13:17:08xtreaksetnosy: + xtreak
messages: + msg391193
2021-04-16 13:06:47vstinnercreate