Skip to content

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

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

Open
Zac-HD opened this issue Nov 7, 2022 · 1 comment · May be fixed by #12338
Open

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

Zac-HD opened this issue Nov 7, 2022 · 1 comment · May be fixed by #12338
Labels
plugin: warnings related to the warnings builtin plugin type: enhancement new feature or API change, should be merged into features branch

Comments

@Zac-HD
Copy link
Member

Zac-HD commented Nov 7, 2022

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.

@Zac-HD Zac-HD added type: enhancement new feature or API change, should be merged into features branch plugin: warnings related to the warnings builtin plugin labels Nov 7, 2022
@olwenya
Copy link

olwenya commented May 18, 2024

I will work on this issue

@Zac-HD Zac-HD linked a pull request May 18, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
plugin: warnings related to the warnings builtin plugin type: enhancement new feature or API change, should be merged into features branch
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants