Skip to content

closes: 10865 Fix muted exception #11804

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

Merged
merged 6 commits into from
Feb 8, 2024
Merged

Conversation

whysage
Copy link
Contributor

@whysage whysage commented Jan 12, 2024

closes #10865

Copy link
Member

@Zac-HD Zac-HD 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 volunteering to take this on, @whysage! The implementation looks good; I've just suggested a tweak to the message and a test approach which I hope will get us full coverage of the diff.

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 we could simplify this test by running it inline, something like:

import warnings

import pytest

def test_raise_type_error_on_non_string_warning():
    with pytest.raises(TypeError, match="Warning message must be str"):
        with pytest.warns(UserWarning):
            warnings.warn(1)

    # Check that we get the same behavior with the stdlib, at least if filtering
    # (see https://github.com/python/cpython/issues/103577 for details)
    with pytest.raises(TypeError):
        with warnings.catch_warnings():
            warnings.simplefilter("always")
            warnings.warn(1)

Copy link
Member

Choose a reason for hiding this comment

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

Agreed, also:

  • Please put this test in test_recwarn.py instead of creating a new file.
  • Add a -> None type annotation.
  • Add a docstring like Check pytest.warns validates warning messages are strings (#10865).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks, updated

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.

Thanks @whysage!

Left a few comments as well. 👍

@@ -0,0 +1 @@
Fix a TypeError in a warning arguments call muted by warnings filter.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Fix a TypeError in a warning arguments call muted by warnings filter.
:func:`pytest.warns` now validates that warning object's ``message`` is of type `str` -- currently in Python it is possible to pass other types than `str` when creating `Warning` instances, however this causes an exception when :func:`warnings.filterwarnings` is used to filter those warnings. See `CPython #103577 <https://github.com/python/cpython/issues/103577>`__ for a discussion.
While this can be considered a bug in CPython, we decided to put guards in pytest as the error message produced without this check in place is confusing.

However I'm not sure this should be a bugfix, I would argue this is more of an improvement.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks, updated

@@ -330,3 +330,11 @@ def found_str():
module=w.__module__,
source=w.source,
)
# Check warnings has valid argument type
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
# Check warnings has valid argument type
# Check warnings has valid argument type (#10865).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks, updated

Copy link
Member

Choose a reason for hiding this comment

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

Agreed, also:

  • Please put this test in test_recwarn.py instead of creating a new file.
  • Add a -> None type annotation.
  • Add a docstring like Check pytest.warns validates warning messages are strings (#10865).

@whysage
Copy link
Contributor Author

whysage commented Jan 28, 2024

Thanks for volunteering to take this on, @whysage! The implementation looks good; I've just suggested a tweak to the message and a test approach which I hope will get us full coverage of the diff.

HI!
Thanks for the review.
I updated the code and still no luck with coverage
line 336 here https://app.codecov.io/gh/pytest-dev/pytest/pull/11804
is marked as not covered, but it is definitely entered by the test
def test_raise_type_error_on_non_string_warning

Any ideas how to fix this?

@whysage whysage force-pushed the feature/issue-10865 branch from 0fdd065 to 269b674 Compare January 28, 2024 14:25
@whysage whysage force-pushed the feature/issue-10865 branch from 0270181 to 79f934f Compare February 5, 2024 17:03
@whysage
Copy link
Contributor Author

whysage commented Feb 5, 2024

Hi all!
Updated to fix codecov issue
please review
@nicoddemus
@Zac-HD

Copy link
Member

@Zac-HD Zac-HD left a comment

Choose a reason for hiding this comment

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

Looks great! Thanks for bearing with us through the process @whysage, we really appreciate your contribution 🥰

@Zac-HD Zac-HD dismissed nicoddemus’s stale review February 8, 2024 00:47

Comments addressed.

@Zac-HD Zac-HD merged commit 9454fc3 into pytest-dev:main Feb 8, 2024
@zanieb
Copy link

zanieb commented Feb 8, 2024

Thank you!

flying-sheep pushed a commit to flying-sheep/pytest that referenced this pull request Apr 9, 2024
* feat: 10865

* feat: 10865 refactor code and tests

* feat: 10865 add test skip for pypy

* feat: 10865 add test with valid warning

* feat: 10865 fix v2 for codecov

* feat: 10865 fix conflict
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

pytest.warns hides type errors in warnings.warn calls
4 participants