Skip to content

Commit

Permalink
Add the _hypothesis_globals module in setup.py, tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
jobh committed Jan 15, 2024
1 parent f3ea914 commit 263c044
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 9 deletions.
1 change: 1 addition & 0 deletions hypothesis-python/.coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ branch = True
omit =
**/_hypothesis_ftz_detector.py
**/_hypothesis_pytestplugin.py
**/_hypothesis_globals.py
**/extra/array_api.py
**/extra/cli.py
**/extra/django/*.py
Expand Down
2 changes: 1 addition & 1 deletion hypothesis-python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def local_file(name):
"Topic :: Software Development :: Testing",
"Typing :: Typed",
],
py_modules=["_hypothesis_pytestplugin", "_hypothesis_ftz_detector"],
py_modules=["_hypothesis_pytestplugin", "_hypothesis_ftz_detector", "_hypothesis_globals"],
entry_points={
"pytest11": ["hypothesispytest = _hypothesis_pytestplugin"],
"console_scripts": ["hypothesis = hypothesis.extra.cli:main"],
Expand Down
2 changes: 1 addition & 1 deletion hypothesis-python/src/hypothesis/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ def _prepare_for_io(self) -> None:

def _initialize_db(self) -> None:
# Create the cache directory if it doesn't exist
storage_directory() # trigger warning that we suppressed earlier with intent_to_write=False
storage_directory(self.path.name) # trigger warning that we suppressed earlier with intent_to_write=False
self.path.mkdir(exist_ok=True, parents=True)

# Get all artifacts
Expand Down
22 changes: 16 additions & 6 deletions hypothesis-python/tests/cover/test_sideeffect_warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,34 @@

IN_INITIALIZATION_ATTR = "in_initialization"

# These tests use the pytest plugin enabling infrastructure to restart the side-effect warnings,
# rather than trying to induce side-effects during import (and entrypoint loading) itself, which is
# hard to do. Manual verification of behaviour during initial import can be done by just injecting
# one of the side-effect-inducing statements below directly into hypothesis.entry_points.run().
# Manual validation can also be done by inspecting the relevant state during import and verify that
# it is the same as tested here
# (_hypothesis_globals.in_initialization > 0, hypothesis.configuration._first_postinit_what is None)


@pytest.fixture
def extend_initialization(monkeypatch):
assert getattr(_hypothesis_globals, IN_INITIALIZATION_ATTR) == 0
monkeypatch.setattr(_hypothesis_globals, IN_INITIALIZATION_ATTR, 1)
fs.notice_initialization_restarted(warn=False)
assert fs._first_postinit_what is None # validates state as given in comment above


@pytest.mark.parametrize(
"sideeffect_script, warning_text",
"sideeffect, warning_text",
[
("st.integers().is_empty", "lazy evaluation"),
("st.deferred(st.integers).is_empty", "deferred evaluation"),
("fs.storage_directory()", "accessing storage"),
(lambda: st.integers().wrapped_strategy, "lazy evaluation"),
(lambda: st.deferred(st.integers).wrapped_strategy, "deferred evaluation"),
(fs.storage_directory, "accessing storage"),
],
)
def test_sideeffect_warning(sideeffect_script, warning_text, extend_initialization):
def test_sideeffect_warning(sideeffect, warning_text, extend_initialization):
with pytest.warns(HypothesisSideeffectWarning, match=warning_text):
exec(sideeffect_script)
sideeffect()


def test_sideeffect_delayed_warning(monkeypatch, extend_initialization):
Expand Down
2 changes: 1 addition & 1 deletion hypothesis-python/tests/pytest/test_sideeffect_warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test_noop():

LAZY_STRATEGY = "integers()"

SIDEEFFECT_STATEMENT = f"st.{LAZY_STRATEGY}.is_empty"
SIDEEFFECT_STATEMENT = f"st.{LAZY_STRATEGY}.wrapped_strategy"

SIDEEFFECT_SCRIPT = f"""
from hypothesis import strategies as st
Expand Down

0 comments on commit 263c044

Please sign in to comment.