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

Fix issue #13047: Handle numpy booleans in pytest.approx #13132

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

Conversation

malangsiddharth
Copy link

This pull request addresses the issue where np.False_ == pytest.approx(False) returns False instead of True after updating to pytest 8.3.4. The issue was introduced by PR #9354, which enforced strict equality for boolean values in pytest.approx.

Changes:
Modify the eq method in the ApproxScalar class to handle numpy booleans appropriately.
This ensures that comparisons between numpy booleans and regular booleans work as expected.

Issue Link: #13047

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 @malangsiddharth the intent of the patch is good.

Can you please fix the tests? We cannot import numpy at the global level, so I suggest writing a helper function like:

def is_bool(val: object)-> True:
    """Check if `val` is a native bool or numpy bool."""
    if isinstance(val, bool):
        return True
    try:
        import numpy as np
    except ImportError:
        return False
    else:
        return isinstance(val, np.bool_)

And use this instead of the inline isinstance check.

In addition, we need a specific test for this to ensure it does not regress in the future.

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.

2 participants