python-trio / trio

Trio – a friendly Python library for async concurrency and I/O
https://trio.readthedocs.io
Other
5.98k stars 325 forks source link

Move `pytest-trio`'s hook for deterministic Hypothesis tests upstream into Trio #2981

Closed Zac-HD closed 2 months ago

Zac-HD commented 3 months ago

pytest-trio does two things for property-based tests with Hypothesis:

  1. Inserts the async-to-sync wrapper logic. pytest-trio is clearly the right place to do that.
  2. Registers Trio's internal Random instance so that the scheduler is deterministic and Hypothesis can replay failing examples (see https://github.com/python-trio/pytest-trio/pull/73 and https://github.com/python-trio/trio/pull/890/).

At the time, pytest-trio was the right place to do (2), but not anymore: since https://github.com/HypothesisWorks/hypothesis/pull/2556, it's possible for Trio to ship a Hypothesis plugin which handles the registration if-and-only-if Hypothesis is imported. Here are the docs.

This is useful because it works even for users without pytest-trio installed or enabled (c.f. https://github.com/HypothesisWorks/hypothesis/issues/3940), e.g. because they're using unittest or running a test function by hand in some debugging loop or the test function calls trio.run() internally, and it will allow pytest-trio to avoid importing Hypothesis if it wasn't otherwise going to be used (which is nice for performance).