Skip to content

Commit

Permalink
Upstream Hypothesis plugin
Browse files Browse the repository at this point in the history
This registration was previously handled by pytest-trio (before Hypothesis supported entry-point plugins).

Upstreaming it means that we'll get the behavior we want for unittest, for functions people call by hand, and so on.
  • Loading branch information
Zac-HD committed Apr 8, 2024
1 parent 519c703 commit 5cff076
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ Homepage = "https://github.com/python-trio/trio"
Documentation = "https://trio.readthedocs.io/"
Changelog = "https://trio.readthedocs.io/en/latest/history.html"

[project.entry-points.hypothesis]
trio = "trio._core._run:_hypothesis_plugin_setup"

[tool.setuptools]
# This means, just install *everything* you see under trio/, even if it
# doesn't look like a source file, so long as it appears in MANIFEST.in:
Expand Down
15 changes: 12 additions & 3 deletions src/trio/_core/_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,23 @@ def _public(fn: FnT) -> FnT:


# When running under Hypothesis, we want examples to be reproducible and
# shrinkable. pytest-trio's Hypothesis integration monkeypatches this
# variable to True, and registers the Random instance _r for Hypothesis
# to manage for each test case, which together should make Trio's task
# shrinkable. We therefore register `_hypothesis_plugin_setup()` as a
# plugin, so that importing *Hypothesis* will make Trio's task
# scheduling loop deterministic. We have a test for that, of course.
# Before Hypothesis supported entry-point plugins this integration was
# handled by pytest-trio, but we want it to work in e.g. unittest too.
_ALLOW_DETERMINISTIC_SCHEDULING: Final = False
_r = random.Random()


def _hypothesis_plugin_setup() -> None:
from hypothesis import register_random

global _ALLOW_DETERMINISTIC_SCHEDULING
_ALLOW_DETERMINISTIC_SCHEDULING = True # type: ignore
register_random(_r)


def _count_context_run_tb_frames() -> int:
"""Count implementation dependent traceback frames from Context.run()
Expand Down

0 comments on commit 5cff076

Please sign in to comment.