-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Check all warnings returned by pytest.warns() #9702
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
Check all warnings returned by pytest.warns() #9702
Conversation
|
please review |
c8af877 to
3dad616
Compare
CodSpeed Performance ReportMerging #9702 will not alter performanceComparing Summary
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your awesome work here.
Looks like a few tests are still failing - perhaps we could still use context managers for each statement that warns, instead of catching all of the warnings in one place for each test. I think that might help iron out the current test failure as well!
tests/test_config.py
Outdated
| def test_config_class_is_deprecated(self): | ||
| with warnings.catch_warnings(): | ||
| # we need to explicitly ignore the other warning in pytest-8 | ||
| # TODO: rewrite it to use two nested pytest.warns() when pytest-7 is no longer supported | ||
| warnings.simplefilter('ignore') | ||
| with pytest.warns( | ||
| PydanticDeprecatedSince20, | ||
| match='Support for class-based `config` is deprecated, use ConfigDict instead.', | ||
| ): | ||
|
|
||
| class Config(BaseConfig): | ||
| pass | ||
| with pytest.warns(PydanticDeprecatedSince20) as all_warnings: | ||
|
|
||
| class Config(BaseConfig): | ||
| pass | ||
|
|
||
| assert len(all_warnings) == 2 | ||
| expected_warnings = [ | ||
| 'BaseConfig is deprecated. Use the `pydantic.ConfigDict` instead', | ||
| 'Support for class-based `config` is deprecated, use ConfigDict instead', | ||
| ] | ||
| assert [w.message.message for w in all_warnings] == expected_warnings | ||
|
|
||
| def test_config_class_attributes_are_deprecated(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like we're getting some failures for these two:
https://github.com/pydantic/pydantic/actions/runs/9590698692/job/26446460308?pr=9702
The changes look alright to me, though I haven't dug into the error cause.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would this be a case where the nested context managers could help?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In some ways, I'd rather we go with that approach so that the error messages are localized and make more sense when contextualized directly above the error-throwing statement.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I doubt this one can be nested -- since typing-extensions=4.6.1 only emits one warning rather two, there'd still be a failure when the outer block doesn't throw a warning.
3dad616 to
2c7052f
Compare
|
I am not sure what is causing the Windows CI failures. 😞 |
With pytest 8.x, pytest.warns() now emits all warnings, bump the required version of pytest and then assert that all of them are the expected warning, rather than filtering them out. Fixes pydantic#9597
2c7052f to
195f8fc
Compare
|
Ah, looks like the lockfile and pyproject.toml were out of sync. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your work on this.
I think there's still room to improve the readability of these tests if anyone is interested, though I think this is a good step forward in at least upgrading to pytest v8+
|
Here's a link to the pytest warnings docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html |
Change Summary
With pytest 8.x, pytest.warns() now emits all warnings, bump the required version of pytest and then assert that all of them are the expected warning, rather than filtering them out.
Related issue number
Fixes #9597
Checklist
Selected Reviewer: @sydney-runkle