Regular expressions in -W and PYTHONWARNINGS

It was an old wart that while filterwarnings() and simplefilter() accepted regular expressions for module name and warning message, the CLI -W option and the PYTHONWARNINGS environment variable only allowed to specify literal strings. For a long time, there was a discrepancy between behavior and documentation which was resolved by changing the documentation.

But from now, you could specify a regular expression surrounded by slashes (for example, -W 'ignore:/.*was never awaited$/:RuntimeWarning:/.*\.util/'). Technically, this is a breaking change, but I hope that very few warning messages start with a slash. If you have any suggestions or objections to the idea, implementation or documentation, you are welcome. Anyway, I hope this information is useful to you.

13 Likes

Why do we even need the slashes?

If we drop the slashes and unconditionally interpret literals as regexes then something like

PYTHONWARNINGS='ignore:setDaemon() is deprecated:DeprecationWarning:'

would stop working because () doesn’t match regex pattern ()?

3 Likes

I like the / slashes approach. It is sufficiently unique it won’t conflict with existing use cases.

1 Like