Skip to content

Conversation

bwhitt7
Copy link
Contributor

@bwhitt7 bwhitt7 commented Aug 22, 2025

This PR is related to #29552, with this being the third PR. This PR has the goal of making NumPy's testing suite more thread safe. This introduces changes to the setup methods for 2 more testing files in numpy/_core.

test_overrides.py includes unittest.mock.Mock, a thread-unsafe functionality that is caught by pytest-run-parallel, so it is not completely thread-safe yet, but all of its setup methods have been replaced.

@@ -747,27 +747,29 @@ def test_bitwise_xor(self):


class TestBoolArray:
def setup_method(self):
def _create_arrays(self):
Copy link

@djeada djeada Aug 24, 2025

Choose a reason for hiding this comment

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

since we already use pytest wouldn't be better to turn it to a fixture?

also more descriptive names, like bool_arrays are usually preferred

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can definitely rename it to something more descriptive.

In the tracking issue #29552, we decided that fixtures may not be the best solution. Part of it is them being slightly harder to read compared to these "setup" methods, another part is that fixtures aren't guaranteed to be thread safe. At most, fixtures are only created once per test, so if you run a test on multiple threads, any mutations to the fixture value will be shared between threads.


def test_logical_and_or_xor(self):
assert_array_equal(self.t | self.t, self.t)
Copy link

Choose a reason for hiding this comment

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

here as well as in many other tests, there are repeated checks with slightly different data

i think that more standard way would be to use fixtures:

@pytest.mark.parametrize("op,a,b,expected", [
    (lambda x, y: x | y, t, t, t),
    (lambda x, y: x | y, f, f, f),
    (lambda x, y: x | y, t, f, t),
    # ...
])

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 this would be fine to do but is orthogonal to what Britney is working on so isn't necessary to merge this

Copy link
Member

@ngoldbaum ngoldbaum left a comment

Choose a reason for hiding this comment

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

Looks good, thanks @bwhitt7!

@ngoldbaum ngoldbaum merged commit 47430c7 into numpy:main Aug 25, 2025
85 of 86 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants