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

ENH: Add all warnings check to the assert_produces_warnings, and separate messages for each warning. #57222

Merged
merged 18 commits into from
Apr 14, 2024

Conversation

Jorewin
Copy link
Contributor

@Jorewin Jorewin commented Feb 3, 2024

The function in question gets used in multiple places, that's why I felt it would be safer to preserve current behavior and API, and add an additional, optional parameter, which alters the behavior, enabling checking if all passed warning types get detected.

@rhshadrach rhshadrach added Testing pandas testing functions or related to the test suite Warnings Warnings that appear or should be added to pandas labels Feb 4, 2024
@Jorewin
Copy link
Contributor Author

Jorewin commented Feb 6, 2024

@rhshadrach Could you review this? Or direct me to someone who can? It's my first contribution here, so I don't know who to ask.

@rhshadrach
Copy link
Member

I should be able to get to this tomorrow.

Copy link
Member

@rhshadrach rhshadrach left a comment

Choose a reason for hiding this comment

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

Thanks for the PR!

doc/source/whatsnew/v3.0.0.rst Outdated Show resolved Hide resolved
pandas/_testing/_warnings.py Outdated Show resolved Hide resolved
type must get encountered. Otherwise, even one expected warning
results in success.

.. versionadded:: 3.0.0
Copy link
Member

Choose a reason for hiding this comment

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

This function isn't in the documentation; this line can be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

match=match,
check_stacklevel=check_stacklevel,
)
if type(expected_warning) == tuple and must_find_all_warnings:
Copy link
Member

Choose a reason for hiding this comment

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

Should use isinstance here I think, hopefully making the cast below unnecessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you, that helped to remove most of the casts.

pandas/_testing/_warnings.py Outdated Show resolved Hide resolved
@Jorewin Jorewin force-pushed the feature/assert-produces-warning-enh branch from b4d9fc0 to 9093962 Compare February 14, 2024 19:56
@Jorewin Jorewin force-pushed the feature/assert-produces-warning-enh branch from 3523b7b to c6d6983 Compare February 19, 2024 18:46
@Jorewin Jorewin force-pushed the feature/assert-produces-warning-enh branch from c6d6983 to 2242796 Compare February 19, 2024 19:28
# Conflicts:
#	pandas/tests/copy_view/test_chained_assignment_deprecation.py
#	pandas/tests/frame/methods/test_interpolate.py
#	pandas/tests/indexing/test_chaining_and_caching.py
@Jorewin
Copy link
Contributor Author

Jorewin commented Feb 19, 2024

@rhshadrach Could you take another look at this, when you have time? I addressed all change requests and resolved all merge conflicts. The docstring checks seem to still be failing, but it looks unrelated to the changes introduced in this PR.

# Conflicts:
#	pandas/tests/io/parser/common/test_inf.py
@Jorewin Jorewin requested a review from rhshadrach February 20, 2024 00:52
Copy link
Member

@rhshadrach rhshadrach left a comment

Choose a reason for hiding this comment

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

This is looking nice!

check_stacklevel=check_stacklevel,
)
else:
expected_warning = cast(type[Warning], expected_warning)
Copy link
Member

Choose a reason for hiding this comment

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

This can still be a tuple, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, you're right. I believe this was originally done, to suppress _assert_caught_expected_warning not accepting tuples. I changed that and fixed some issues that arose because of that now.

match = (
match
if isinstance(match, tuple)
else tuple(match for i in range(len(expected_warning)))
Copy link
Member

Choose a reason for hiding this comment

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

nit: Use (match,) * len(expected_warning) instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@Jorewin Jorewin requested a review from rhshadrach February 24, 2024 02:25
@Jorewin
Copy link
Contributor Author

Jorewin commented Feb 27, 2024

@rhshadrach I just applied a patch that fixed the python 3.9 unit test not passing. With this, this should be ready for review.

Copy link
Member

@rhshadrach rhshadrach left a comment

Choose a reason for hiding this comment

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

lgtm, @mroeschke - wouldn't mind a 2nd eye on this if you get the chance.

Copy link
Member

@mroeschke mroeschke left a comment

Choose a reason for hiding this comment

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

Could you add some tests to pandas/tests/util/test_assert_produces_warning.py? e.g. multiple warnings and matches, multiple warnings and maches don't match, etc

Copy link
Contributor

This pull request is stale because it has been open for thirty days with no activity. Please update and respond to this comment if you're still interested in working on this.

@github-actions github-actions bot added the Stale label Mar 30, 2024
@rhshadrach
Copy link
Member

@Jorewin - are you interested in finishing this up? If not, I can add some tests to get it over the finish line.

@rhshadrach rhshadrach removed the Stale label Mar 31, 2024
@rhshadrach rhshadrach self-assigned this Mar 31, 2024
@Jorewin
Copy link
Contributor Author

Jorewin commented Apr 2, 2024

@rhshadrach If you want to, then go ahead. I just didn't have much time lately.

@rhshadrach
Copy link
Member

@mroeschke - tests added for various combinations. Let me know if you see any others to add.

@mroeschke mroeschke added this to the 3.0 milestone Apr 14, 2024
@mroeschke mroeschke merged commit d8c7e85 into pandas-dev:main Apr 14, 2024
46 checks passed
@mroeschke
Copy link
Member

Thanks @Jorewin and @rhshadrach

pmhatre1 pushed a commit to pmhatre1/pandas-pmhatre1 that referenced this pull request May 7, 2024
…rate messages for each warning. (pandas-dev#57222)

* ENH: Add all warnings check to the `assert_produces_warnings`, and separate messages for each warning.

* Fix typing errors

* Fix typing errors

* Remove unnecessary documentation

* Change `assert_produces_warning behavior` to check for all warnings by default

* Refactor typing

* Fix tests expecting a Warning that is not raised

* Adjust `raises_chained_assignment_error` and its dependencies to the new API of `assert_produces_warning`

* Fix `_assert_caught_expected_warning` typing not including tuple of warnings

* fixup! Refactor typing

* fixup! Fix `_assert_caught_expected_warning` typing not including tuple of warnings

* fixup! Fix `_assert_caught_expected_warning` typing not including tuple of warnings

* Add tests

---------

Co-authored-by: Richard Shadrach <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Testing pandas testing functions or related to the test suite Warnings Warnings that appear or should be added to pandas
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TST: assert_produces_warning multiple warnings/messages
3 participants