Skip to content

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

Open
@Zac-HD

Description

@Zac-HD

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    plugin: warningsrelated to the warnings builtin plugintype: enhancementnew feature or API change, should be merged into features branch

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions