Skip to content

Commit 62388bc

Browse files
committed
hypothesis: accept container strategy in check_all_laws.
I'm calling it `container_strategy` to leave room for future extensions that would allow configuring the pure function strategy, etc.
1 parent 2b0abee commit 62388bc

File tree

4 files changed

+30
-33
lines changed

4 files changed

+30
-33
lines changed

returns/contrib/hypothesis/laws.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@ class Settings(NamedTuple):
2323

2424
settings_kwargs: dict[str, Any]
2525
use_init: bool
26-
strategy: StrategyFactory | None = None
26+
container_strategy: StrategyFactory | None
2727

2828

2929
def check_all_laws(
3030
container_type: type[Lawful],
3131
*,
3232
settings_kwargs: dict[str, Any] | None = None,
3333
use_init: bool = False,
34+
container_strategy: StrategyFactory | None = None,
3435
) -> None:
3536
"""
3637
Function to check all defined mathematical laws in a specified container.
@@ -61,6 +62,7 @@ def check_all_laws(
6162
settings = Settings(
6263
settings_kwargs or {},
6364
use_init,
65+
container_strategy,
6466
)
6567

6668
for interface, laws in container_type.laws().items():
@@ -227,8 +229,8 @@ def _strategy_for_container(
227229
) -> StrategyFactory:
228230
return (
229231
strategy_from_container(container_type, use_init=settings.use_init)
230-
if settings.strategy is None
231-
else settings.strategy
232+
if settings.container_strategy is None
233+
else settings.container_strategy
232234
)
233235

234236

returns/contrib/hypothesis/type_resolver.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def strategies_for_types(
2020
Temporarily register strategies with `hypothesis`.
2121
2222
Within this context, `hypothesis` will generate data for `MyType`
23-
using `mapping[MyType]`, if possible. Otherwise, it will continue to
23+
using `mapping[MyType]`, if available. Otherwise, it will continue to
2424
use the globally registered strategy for `MyType`.
2525
2626
NOTE: This manually adds and removes strategies from an internal data
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from hypothesis import strategies as st
2+
from test_hypothesis.test_laws import test_custom_type_applicative
3+
4+
from returns.contrib.hypothesis.laws import check_all_laws
5+
6+
container_type = test_custom_type_applicative._Wrapper # noqa: SLF001
7+
8+
check_all_laws(
9+
container_type,
10+
container_strategy=st.builds(container_type, st.integers()),
11+
)

tests/test_contrib/test_hypothesis/test_type_resolution.py

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,10 @@ def test_register_container_with_no_strategy() -> None:
131131
container_type = Result
132132

133133
with register_container(
134-
container_type, settings=Settings(settings_kwargs={}, use_init=False)
134+
container_type,
135+
settings=Settings(
136+
settings_kwargs={}, use_init=False, container_strategy=None
137+
),
135138
):
136139
strategy_factory = look_up_strategy(container_type)
137140

@@ -151,7 +154,9 @@ def test_register_container_with_strategy() -> None:
151154
}),
152155
register_container(
153156
container_type,
154-
settings=Settings(settings_kwargs={}, use_init=False),
157+
settings=Settings(
158+
settings_kwargs={}, use_init=False, container_strategy=None
159+
),
155160
),
156161
):
157162
strategy_factory = look_up_strategy(container_type)
@@ -171,7 +176,7 @@ def test_register_container_with_setting() -> None:
171176
settings=Settings(
172177
settings_kwargs={},
173178
use_init=False,
174-
strategy=st.builds(Success, st.integers()),
179+
container_strategy=st.builds(Success, st.integers()),
175180
),
176181
):
177182
strategy_factory = look_up_strategy(container_type)
@@ -186,19 +191,14 @@ def test_interface_strategies() -> None:
186191
"""Check that ancestor interfaces get resolved to the concrete container."""
187192
container_type = test_custom_type_applicative._Wrapper # noqa: SLF001
188193

189-
strategy_factories_before = _interface_factories(container_type)
190-
191194
with interface_strategies(
192-
container_type, settings=Settings(settings_kwargs={}, use_init=False)
195+
container_type,
196+
settings=Settings(
197+
settings_kwargs={}, use_init=False, container_strategy=None
198+
),
193199
):
194200
strategy_factories_inside = _interface_factories(container_type)
195201

196-
strategy_factories_after = _interface_factories(container_type)
197-
198-
assert _strategy_strings(strategy_factories_before, container_type) == [
199-
'None',
200-
'None',
201-
]
202202
assert _strategy_strings(strategy_factories_inside, container_type) == [
203203
"builds(from_value, shared(sampled_from([<class 'NoneType'>,"
204204
" <class 'bool'>, <class 'int'>, <class 'float'>, <class 'str'>,"
@@ -207,42 +207,26 @@ def test_interface_strategies() -> None:
207207
" <class 'bool'>, <class 'int'>, <class 'float'>, <class 'str'>,"
208208
" <class 'bytes'>]), key='typevar=~_FirstType').flatmap(from_type))",
209209
]
210-
assert _strategy_strings(strategy_factories_after, container_type) == [
211-
'None',
212-
'None',
213-
]
214210

215211

216212
def test_interface_strategies_with_settings() -> None:
217213
"""Check that we prefer the strategy in the settings."""
218214
container_type = test_custom_type_applicative._Wrapper # noqa: SLF001
219215

220-
strategy_factories_before = _interface_factories(container_type)
221-
222216
with interface_strategies(
223217
container_type,
224218
settings=Settings(
225219
settings_kwargs={},
226220
use_init=False,
227-
strategy=st.builds(container_type, st.integers()),
221+
container_strategy=st.builds(container_type, st.integers()),
228222
),
229223
):
230224
strategy_factories_inside = _interface_factories(container_type)
231225

232-
strategy_factories_after = _interface_factories(container_type)
233-
234-
assert _strategy_strings(strategy_factories_before, container_type) == [
235-
'None',
236-
'None',
237-
]
238226
assert _strategy_strings(strategy_factories_inside, container_type) == [
239227
'builds(_Wrapper, integers())',
240228
'builds(_Wrapper, integers())',
241229
]
242-
assert _strategy_strings(strategy_factories_after, container_type) == [
243-
'None',
244-
'None',
245-
]
246230

247231

248232
def _interface_factories(type_: type[Lawful]) -> list[StrategyFactory | None]:

0 commit comments

Comments
 (0)