Skip to content

Commit ecc2262

Browse files
committed
hypothesis: add docs and overloads.
1 parent 092e91d commit ecc2262

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ See [0Ver](https://0ver.org/).
2727
### Features
2828

2929
- Make `hypothesis` plugin test laws from user-defined interfaces too
30+
- Make `hypothesis` plugin accept user-defined strategies
3031

3132
### Bugfixes
3233

docs/pages/contrib/hypothesis_plugins.rst

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,27 @@ like ``Future``, ``ReaderFutureResult``, etc
140140
that have complex ``__init__`` signatures.
141141
And we don't want to mess with them.
142142

143+
You can also register a custom strategy to be used when running your
144+
container's laws:
145+
146+
.. code:: python
147+
148+
149+
from hypothesis import strategies as st
150+
151+
check_all_laws(Number, container_strategy=st.builds(Number, st.integers()))
152+
153+
The ``container_strategy`` will be used only when running the tests generated
154+
by the ``check_all_laws`` call above. It will have no effect on any other
155+
property tests that involve ``Number``. You cannot use this argument together
156+
with ``use_init``.
157+
158+
Warning::
159+
Avoid directly registering your container's strategy with ``hypothesis``
160+
using ``st.register_type_strategy``. Because of the way we emulate
161+
higher-kinded types, ``hypothesis`` may mistakenly use the strategy
162+
for other incompatible containers and cause spurious test failures.
163+
143164
Warning::
144165
Checking laws is not compatible with ``pytest-xdist``,
145166
because we use a lot of global mutable state there.

returns/contrib/hypothesis/laws.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import inspect
22
from collections.abc import Callable, Iterator, Mapping
33
from contextlib import ExitStack, contextmanager
4-
from typing import Any, NamedTuple, TypeGuard, TypeVar, final
4+
from typing import Any, NamedTuple, TypeGuard, TypeVar, final, overload
55

66
import pytest
77
from hypothesis import given
@@ -28,6 +28,24 @@ class Settings(NamedTuple):
2828
container_strategy: StrategyFactory | None
2929

3030

31+
@overload
32+
def check_all_laws(
33+
container_type: type[Lawful[Example_co]],
34+
*,
35+
settings_kwargs: dict[str, Any] | None = None,
36+
container_strategy: StrategyFactory[Example_co] | None = None,
37+
) -> None: ...
38+
39+
40+
@overload
41+
def check_all_laws(
42+
container_type: type[Lawful[Example_co]],
43+
*,
44+
settings_kwargs: dict[str, Any] | None = None,
45+
use_init: bool = False,
46+
) -> None: ...
47+
48+
3149
def check_all_laws(
3250
container_type: type[Lawful[Example_co]],
3351
*,

0 commit comments

Comments
 (0)