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

Calling st.register_type_strategy imports numpy #3154

Closed
rsokl opened this issue Nov 17, 2021 · 1 comment · Fixed by #3159
Closed

Calling st.register_type_strategy imports numpy #3154

rsokl opened this issue Nov 17, 2021 · 1 comment · Fixed by #3159
Assignees
Labels
enhancement it's not broken, but we want it to be better

Comments

@rsokl
Copy link
Contributor

rsokl commented Nov 17, 2021

This is the "offending" block:

try: # pragma: no cover
import numpy as np
from hypothesis.extra.numpy import array_dtypes, array_shapes, arrays, scalar_dtypes
_global_type_lookup[np.dtype] = array_dtypes()
_global_type_lookup[np.ndarray] = arrays(scalar_dtypes(), array_shapes(max_dims=2))
except ImportError:
pass

This test

SHOULD_NOT_IMPORT_NUMPY = """
import sys
from hypothesis import given, strategies as st
@given(st.integers() | st.floats() | st.sampled_from(["a", "b"]))
def test_no_numpy_import(x):
assert "numpy" not in sys.modules
"""
def test_hypothesis_is_not_the_first_to_import_numpy(testdir):
# We only import numpy if the user did so first.
result = testdir.runpytest(testdir.makepyfile(SHOULD_NOT_IMPORT_NUMPY))
result.assert_outcomes(passed=1, failed=0)

can be made to fail by modifying the src-string to be:

SHOULD_NOT_IMPORT_NUMPY = """
import sys
from hypothesis import given, strategies as st

@given(st.integers() | st.floats() | st.sampled_from(["a", "b"]) | st.just(st.register_type_strategy(int, st.integers())))
def test_no_numpy_import(x):
    assert "numpy" not in sys.modules
"""

This was causing a headache for me when figuring out why numpy was being imported the instant I ran pytest... this was because I happened to have installed numpy + hypothesis + pydantic (which uses st.register_type_strategy via our entrypoint).

This could be a big headache for testers over at, say, PyTorch, who want to test that they don't cause numpy to be imported.

@rsokl rsokl added the enhancement it's not broken, but we want it to be better label Nov 17, 2021
@Zac-HD
Copy link
Member

Zac-HD commented Nov 19, 2021

If I expected this to require a nasty patch - i.e. worse than our numpy special case in register_random - I wouldn't consider it worth fixing.

That's because once we make our pytest plugin lazily load Hypothesis (#3140), this will only trigger if you deliberately import hypothesis and somehow call st.register_type_strategy() in the test that Numpy is not imported, which usually happens in a fresh process.

Other libraries will never get this special treatment, but Numpy is already exceptional in our code and the ecosystem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement it's not broken, but we want it to be better
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants