Skip to content
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

Fixed invalid regex handling in filterwarnings #13124

Open
wants to merge 16 commits into
base: main
Choose a base branch
from

Conversation

virendrapatil24
Copy link
Contributor

@virendrapatil24 virendrapatil24 commented Jan 11, 2025

closes #13119

  • Updated the parse_warning_filter method to handle invalid regex pattern provided to message/module args.
  • Added a test case in testing/test_warnings.py to validate the new behavior.
  • Created a changelog fragment to document this improvement.

Copy link
Member

@The-Compiler The-Compiler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Definitely much better. New output (with my small suggestion added):

ERROR: while parsing the following warning configuration:

  ignore::DeprecationWarning:*

This error occurred:

Invalid regex '*': nothing to repeat at position 0

src/_pytest/config/__init__.py Outdated Show resolved Hide resolved
@psf-chronographer psf-chronographer bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Jan 11, 2025
@virendrapatil24 virendrapatil24 marked this pull request as ready for review January 11, 2025 14:45
Copy link
Member

@nicoddemus nicoddemus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

testing/test_warnings.py Outdated Show resolved Hide resolved
result = pytester.runpytest_subprocess()
result.stderr.fnmatch_lines(
[
"*Invalid regex '*': nothing to repeat at position 0*",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be more thorough to test on the whole "while parsing ..." output?

@graingert
Copy link
Member

graingert commented Jan 21, 2025

it looks like there's no tests for "invalid lineno (negative AND not an integer)", too many fields, invalid action or resolving the warning category

perhaps you'd be interested in adding these tests? Should be very similar to test_invalid_regex_in_filterwarning perhaps in this PR or perhaps in a followup?

@virendrapatil24
Copy link
Contributor Author

Thanks for the review!

I’m a bit confused about what else is required for this PR. Could you summarize the key changes you’d like to see here? From my understanding, the missing tests you mentioned might be better suited for a follow-up rather than this ticket. Let me know your thoughts!

@graingert
Copy link
Member

graingert commented Jan 28, 2025

Thanks for the review!

I’m a bit confused about what else is required for this PR. Could you summarize the key changes you’d like to see here? From my understanding, the missing tests you mentioned might be better suited for a follow-up rather than this ticket. Let me know your thoughts!

So yeah could you:

  • Assert on the exit code of pytester
  • Assert on the full output starting with "while parsing the following warning configuration: ..." in the stderr.fnmatch_lines assertion

@virendrapatil24
Copy link
Contributor Author

Thanks for the review!
I’m a bit confused about what else is required for this PR. Could you summarize the key changes you’d like to see here? From my understanding, the missing tests you mentioned might be better suited for a follow-up rather than this ticket. Let me know your thoughts!

So yeah could you:

  • Assert on the exit code of pytester
  • Assert on the full output starting with "while parsing the following warning configuration: ..." in the stderr.fnmatch_lines assertion

sure will make those asserts, thanks for clarifying.

"",
"This error occurred:",
"",
"Invalid regex '*': nothing to repeat at position 0",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you need to escape this star or it will treat it as a wildcard/fnmatch syntax. I'm not sure how to escape it though

Suggested change
"Invalid regex '*': nothing to repeat at position 0",
r"Invalid regex '\*': nothing to repeat at position 0",

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense, I will check what I can do here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can escape * using [*]
ref: https://docs.python.org/3/library/fnmatch.html

testing/test_warnings.py Outdated Show resolved Hide resolved
@graingert graingert enabled auto-merge (squash) January 29, 2025 10:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bot:chronographer:provided (automation) changelog entry is part of PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

config option filterwarnings ignore::DeprecationWarning:* crashes with regex exception: PatternError
4 participants