pytest-dev / pytest

The pytest framework makes it easy to write small tests, yet scales to support complex functional testing
https://pytest.org
MIT License
12.13k stars 2.69k forks source link

Warn on impossible-to-match message pattern in filterwarnings - likely user error #10478

Open Zac-HD opened 2 years ago

Zac-HD commented 2 years ago

Suppose that you have something like the following in your Pytest config:

filterwarnings =
    ignore::The foo.bar() function is deprecated

This looks perfectly reasonable, but somehow the warning still occurs! Unfortunately - and this got me recently - the message argument is a regex pattern, and in that context the parens () are an empty group... meaning that it won't match a string which contains the literal parentheses.

I think we should use the re._parse (sre_parse on older Pythons) module to check whether filterwarnings patterns contain an empty match. Since there's no use for capture groups, it's very likely that the pattern was intended as a literal string or prefix. This is pretty easy to get wrong if you're copy-pasting the message into your warnings config! We can even suggest the re.escape()d version as a fix.

olwenya commented 6 months ago

I will work on this issue