Skip to content

Commit 263c044

Browse files
committed
Add the _hypothesis_globals module in setup.py, tweaks
1 parent f3ea914 commit 263c044

File tree

5 files changed

+20
-9
lines changed

5 files changed

+20
-9
lines changed

hypothesis-python/.coveragerc

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ branch = True
33
omit =
44
**/_hypothesis_ftz_detector.py
55
**/_hypothesis_pytestplugin.py
6+
**/_hypothesis_globals.py
67
**/extra/array_api.py
78
**/extra/cli.py
89
**/extra/django/*.py

hypothesis-python/setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def local_file(name):
124124
"Topic :: Software Development :: Testing",
125125
"Typing :: Typed",
126126
],
127-
py_modules=["_hypothesis_pytestplugin", "_hypothesis_ftz_detector"],
127+
py_modules=["_hypothesis_pytestplugin", "_hypothesis_ftz_detector", "_hypothesis_globals"],
128128
entry_points={
129129
"pytest11": ["hypothesispytest = _hypothesis_pytestplugin"],
130130
"console_scripts": ["hypothesis = hypothesis.extra.cli:main"],

hypothesis-python/src/hypothesis/database.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ def _prepare_for_io(self) -> None:
496496

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

502502
# Get all artifacts

hypothesis-python/tests/cover/test_sideeffect_warnings.py

+16-6
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,34 @@
2020

2121
IN_INITIALIZATION_ATTR = "in_initialization"
2222

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

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

2939

3040
@pytest.mark.parametrize(
31-
"sideeffect_script, warning_text",
41+
"sideeffect, warning_text",
3242
[
33-
("st.integers().is_empty", "lazy evaluation"),
34-
("st.deferred(st.integers).is_empty", "deferred evaluation"),
35-
("fs.storage_directory()", "accessing storage"),
43+
(lambda: st.integers().wrapped_strategy, "lazy evaluation"),
44+
(lambda: st.deferred(st.integers).wrapped_strategy, "deferred evaluation"),
45+
(fs.storage_directory, "accessing storage"),
3646
],
3747
)
38-
def test_sideeffect_warning(sideeffect_script, warning_text, extend_initialization):
48+
def test_sideeffect_warning(sideeffect, warning_text, extend_initialization):
3949
with pytest.warns(HypothesisSideeffectWarning, match=warning_text):
40-
exec(sideeffect_script)
50+
sideeffect()
4151

4252

4353
def test_sideeffect_delayed_warning(monkeypatch, extend_initialization):

hypothesis-python/tests/pytest/test_sideeffect_warnings.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def test_noop():
1717

1818
LAZY_STRATEGY = "integers()"
1919

20-
SIDEEFFECT_STATEMENT = f"st.{LAZY_STRATEGY}.is_empty"
20+
SIDEEFFECT_STATEMENT = f"st.{LAZY_STRATEGY}.wrapped_strategy"
2121

2222
SIDEEFFECT_SCRIPT = f"""
2323
from hypothesis import strategies as st

0 commit comments

Comments
 (0)