Skip to content

Document pytest.warns that matching things with parenthesis needs to be escaped. #10595

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

Closed
BurnzZ opened this issue Dec 21, 2022 · 1 comment
Closed
Labels
plugin: warnings related to the warnings builtin plugin type: docs documentation improvement, missing or needing clarification

Comments

@BurnzZ
Copy link

BurnzZ commented Dec 21, 2022

There are some cases where we mention a function in a UserWarning with the parenthesis. A simple string matching wouldn't work despite the annotation being match: Optional[Union[str, Pattern[str]]].

Example on pytest==7.2.0:

>>> with pytest.warns(UserWarning, match="issue with foo() func"):
...     warnings.warn("issue with foo() func")
Traceback (most recent call last):
  ...
Failed: DID NOT WARN. No warnings of type (<class 'UserWarning'>,) matching the regex were emitted.
 Regex: issue with foo() func
 Emitted warnings: [UserWarning('issue with foo() func')]

>>> with pytest.warns(UserWarning, match="issue with foo\(\) func"):
...     warnings.warn("issue with foo() func")
@The-Compiler
Copy link
Member

The docs indeed are rather confusing about this, surprisingly! Both the how-to and the reference say:

[...] the keyword argument match to assert that the warning matches a text or regex:

and show a rather confusing example:

with warns(UserWarning, match='must be 0 or None'):
    warnings.warn("value must be 0 or None", UserWarning)

with warns(UserWarning, match=r'must be \d+$'):
    warnings.warn("value must be 42", UserWarning)

This seems to have been fixed for pytest.raises in 8b34d98 for #5208, looks like we should do a very similar change for pytest.warns as well.

@The-Compiler The-Compiler added type: docs documentation improvement, missing or needing clarification plugin: warnings related to the warnings builtin plugin labels Dec 21, 2022
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: docs documentation improvement, missing or needing clarification
Projects
None yet
Development

No branches or pull requests

2 participants