Skip to content

Commit

Permalink
Improve specificity of warning text
Browse files Browse the repository at this point in the history
  • Loading branch information
jobh committed Jan 17, 2024
1 parent 9e60665 commit 2ab04fb
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
24 changes: 19 additions & 5 deletions hypothesis-python/src/hypothesis/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# obtain one at https://mozilla.org/MPL/2.0/.

import os
import sys
import warnings
from pathlib import Path

Expand Down Expand Up @@ -45,7 +46,7 @@ def storage_directory(*names, intent_to_write=True):


def check_sideeffect_during_initialization(
what: str, *fmt_args: object, extra: str = ""
what: str, *fmt_args: object, is_restart: bool = False
) -> None:
"""Called from locations that should not be executed during initialization, for example
touching disk or materializing lazy/deferred strategies from plugins. If initialization
Expand All @@ -61,12 +62,25 @@ def check_sideeffect_during_initialization(
if _first_postinit_what is not None:
return
elif _hypothesis_globals.in_initialization > 0:
msg = what.format(*fmt_args)
if is_restart:
when = "between importing hypothesis and loading the hypothesis plugin"
elif "_hypothesis_pytestplugin" in sys.modules or os.getenv(
"HYPOTHESIS_EXTEND_INITIALIZATION"
):
when = "during pytest plugin or conftest initialization"
else:
when = "at import time"
# Note: -Werror is insufficient under pytest, as doesn't take effect until
# test session start.
msg = what.format(*fmt_args)
text = (
f"Slow code in plugin: avoid {msg} {when}! Set PYTHONWARNINGS=error "
"to get a traceback and show which plugin is responsible."
)
if is_restart:
text += " Additionally, set HYPOTHESIS_EXTEND_INITIALIZATION=1 to pinpoint the exact location."
warnings.warn(
f"Slow code in plugin: avoid {msg} at import time! Set PYTHONWARNINGS=error "
"to get a traceback and show which plugin is responsible." + extra,
text,
HypothesisSideeffectWarning,
stacklevel=3,
)
Expand All @@ -87,5 +101,5 @@ def notice_initialization_restarted(*, warn: bool = True) -> None:
check_sideeffect_during_initialization(
what,
*fmt_args,
extra=" Additionally, set HYPOTHESIS_EXTEND_INITIALIZATION=1 to pinpoint the exact location.",
is_restart=True,
)
3 changes: 2 additions & 1 deletion hypothesis-python/tests/cover/test_sideeffect_warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def test_sideeffect_delayed_warning(monkeypatch, _extend_initialization):
monkeypatch.setattr(_hypothesis_globals, IN_INITIALIZATION_ATTR, 0)
fs.check_sideeffect_during_initialization(what)
fs.check_sideeffect_during_initialization("ignored since not first")
with pytest.warns(HypothesisSideeffectWarning, match=what):
# The warning should identify itself as happening after import but before plugin load
with pytest.warns(HypothesisSideeffectWarning, match=what + ".*between import"):
monkeypatch.setattr(_hypothesis_globals, IN_INITIALIZATION_ATTR, 1)
fs.notice_initialization_restarted()
4 changes: 4 additions & 0 deletions hypothesis-python/tests/pytest/test_sideeffect_warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ def test_conftest_sideeffect_pinpoint_error(testdir, monkeypatch):
script = testdir.makepyfile(TEST_SCRIPT)
result = testdir.runpytest_subprocess(script)
assert "HypothesisSideeffectWarning" in "\n".join(result.errlines)
# Plugin is always loaded before conftest, so "during pytest plugin initialization"
assert "during pytest" in "\n".join(result.errlines)
assert SIDEEFFECT_STATEMENT in "\n".join(result.errlines)


Expand All @@ -62,4 +64,6 @@ def test_plugin_sideeffect_pinpoint_error(testdir, monkeypatch):
script = testdir.makepyfile(TEST_SCRIPT)
result = testdir.runpytest_subprocess(script, "-p", "sideeffect_plugin")
assert "HypothesisSideeffectWarning" in "\n".join(result.errlines)
# Plugin order unknown, but certainly not at import time
assert "at import time" not in "\n".join(result.errlines)
assert SIDEEFFECT_STATEMENT in "\n".join(result.errlines)

0 comments on commit 2ab04fb

Please sign in to comment.