Skip to content

Commit 40da087

Browse files
committed
hypothesis: rename attribute and parameter for type strategies.
1 parent ab6a362 commit 40da087

5 files changed

Lines changed: 29 additions & 24 deletions

File tree

docs/pages/contrib/hypothesis_plugins.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ You can also register strategies for other types:
146146
check_all_laws(
147147
Number,
148148
container_strategy=st.builds(Number, st.integers()),
149-
other_strategies={Foo: st.builds(Foo, st.text())},
149+
type_strategies={Foo: st.builds(Foo, st.text())},
150150
)
151151
152152
These custom strategies will be used only when running the tests generated by

returns/contrib/hypothesis/laws.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626
class Settings:
2727
"""Settings for the law tests.
2828
29+
This sets the context for each generated law test, by temporarily
30+
registering strategies for various types and passing any ``hypothesis``
31+
settings.
32+
2933
Any settings passed by the user will override the value from
3034
:func:`default_settings`.
3135
"""
@@ -40,10 +44,11 @@ class Settings:
4044
#: of a container using:
4145
#: :func:`returns.contrib.hypothesis.containers.strategy_from_container`.
4246
container_strategy: StrategyFactory | None
43-
#: Strategies for generating values of other types. This can be useful for
44-
#: overriding ``TypeVar``, ``Callable``, etc. in case you use certain
45-
#: types that ``hypothesis`` is unable to find.
46-
other_strategies: dict[type[object], StrategyFactory]
47+
#: Strategies for generating values of types other than the container and
48+
#: its lawful interfaces. This can be useful for overriding ``TypeVar``,
49+
#: ``Callable``, etc. in case you use certain types that ``hypothesis`` is
50+
#: unable to find.
51+
type_strategies: dict[type[object], StrategyFactory]
4752

4853
def __post_init__(self) -> None:
4954
"""Check that the settings are mutually compatible."""
@@ -61,7 +66,7 @@ def __or__(self, other: Self) -> Self:
6166
container_strategy=self.container_strategy
6267
if other.container_strategy is None
6368
else other.container_strategy,
64-
other_strategies=self.other_strategies | other.other_strategies,
69+
type_strategies=self.type_strategies | other.type_strategies,
6570
)
6671

6772

@@ -86,7 +91,7 @@ def default_settings(container_type: type[Lawful]) -> Settings:
8691
settings_kwargs={},
8792
use_init=False,
8893
container_strategy=None,
89-
other_strategies={
94+
type_strategies={
9095
TypeVar: type_vars_factory, # type: ignore[dict-item]
9196
Callable: pure_functions_factory, # type: ignore[dict-item]
9297
},
@@ -99,7 +104,7 @@ def check_all_laws(
99104
*,
100105
container_strategy: StrategyFactory[Example_co],
101106
settings_kwargs: dict[str, Any] | None = None,
102-
other_strategies: dict[type[object], StrategyFactory] | None = None,
107+
type_strategies: dict[type[object], StrategyFactory] | None = None,
103108
) -> None: ...
104109

105110

@@ -118,7 +123,7 @@ def check_all_laws(
118123
settings_kwargs: dict[str, Any] | None = None,
119124
use_init: bool = False,
120125
container_strategy: StrategyFactory[Example_co] | None = None,
121-
other_strategies: dict[type[object], StrategyFactory] | None = None,
126+
type_strategies: dict[type[object], StrategyFactory] | None = None,
122127
) -> None:
123128
"""
124129
Function to check all defined mathematical laws in a specified container.
@@ -150,7 +155,7 @@ def check_all_laws(
150155
settings_kwargs or {},
151156
use_init,
152157
container_strategy,
153-
other_strategies=other_strategies or {},
158+
type_strategies=type_strategies or {},
154159
)
155160

156161
for interface, laws in container_type.laws().items():
@@ -282,7 +287,7 @@ def _types_to_strategies(
282287
settings: Settings,
283288
) -> dict[type[object], StrategyFactory]:
284289
"""Return a mapping from type to `hypothesis` strategy."""
285-
return settings.other_strategies | _container_mapping(
290+
return settings.type_strategies | _container_mapping(
286291
container_type, settings
287292
)
288293

tests/test_contrib/test_hypothesis/test_laws/test_custom_strategy_for_callable.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,12 @@ def _callable_factory(thing: type[object]) -> StrategyFactory[Callable]:
150150
raise NotImplementedError
151151

152152

153-
other_strategies: dict[type[object], StrategyFactory] = {
153+
type_strategies: dict[type[object], StrategyFactory] = {
154154
Callable: _callable_factory # type: ignore[dict-item]
155155
}
156156

157157
check_all_laws(
158158
_Wrapper,
159159
container_strategy=st.builds(_Wrapper, st.integers()),
160-
other_strategies=other_strategies,
160+
type_strategies=type_strategies,
161161
)

tests/test_contrib/test_hypothesis/test_type_resolution.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,13 @@ def test_merge_settings() -> None:
120120
settings_kwargs={'a': 1, 'b': 2},
121121
use_init=False,
122122
container_strategy=st.integers(),
123-
other_strategies={int: st.integers(max_value=10), str: st.text('abc')},
123+
type_strategies={int: st.integers(max_value=10), str: st.text('abc')},
124124
)
125125
settings2 = Settings(
126126
settings_kwargs={'a': 1, 'c': 3},
127127
use_init=False,
128128
container_strategy=st.integers(max_value=20),
129-
other_strategies={int: st.integers(max_value=30), bool: st.booleans()},
129+
type_strategies={int: st.integers(max_value=30), bool: st.booleans()},
130130
)
131131

132132
result = settings1 | settings2
@@ -135,7 +135,7 @@ def test_merge_settings() -> None:
135135
settings_kwargs={'a': 1, 'b': 2, 'c': 3},
136136
use_init=False,
137137
container_strategy=st.integers(max_value=20),
138-
other_strategies={
138+
type_strategies={
139139
int: st.integers(max_value=30),
140140
bool: st.booleans(),
141141
str: st.text('abc'),
@@ -153,13 +153,13 @@ def test_merge_use_init() -> None:
153153
settings_kwargs={},
154154
use_init=False,
155155
container_strategy=None,
156-
other_strategies={},
156+
type_strategies={},
157157
)
158158
settings2 = Settings(
159159
settings_kwargs={},
160160
use_init=True,
161161
container_strategy=None,
162-
other_strategies={},
162+
type_strategies={},
163163
)
164164

165165
result = settings1 | settings2
@@ -168,7 +168,7 @@ def test_merge_use_init() -> None:
168168
settings_kwargs={},
169169
use_init=True,
170170
container_strategy=None,
171-
other_strategies={},
171+
type_strategies={},
172172
)
173173

174174

@@ -234,7 +234,7 @@ def test_types_to_strategies_overrides() -> None: # noqa: WPS210
234234
settings_kwargs={},
235235
use_init=False,
236236
container_strategy=st.builds(container_type, st.integers()),
237-
other_strategies={
237+
type_strategies={
238238
TypeVar: st.text(),
239239
callable_type: st.functions(returns=st.booleans()),
240240
# This strategy does not get used, because we use

typesafety/test_contrib/test_hypothesis/test_laws/test_check_all_laws.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
out: |
4949
main:8: error: No overload variant of "check_all_laws" matches argument types "Type[Result[_ValueType_co, _ErrorType_co]]", "bool", "SearchStrategy[Success[Any]]" [call-overload]
5050
main:8: note: Possible overload variants:
51-
main:8: note: def [Example_co] check_all_laws(container_type: Type[Lawful[Example_co]], *, container_strategy: Union[SearchStrategy[Example_co], Callable[[Type[Example_co]], SearchStrategy[Example_co]]], settings_kwargs: Optional[Dict[str, Any]] = ..., other_strategies: Optional[Dict[Type[object], Union[SearchStrategy[Any], Callable[[Type[Any]], SearchStrategy[Any]]]]] = ...) -> None
51+
main:8: note: def [Example_co] check_all_laws(container_type: Type[Lawful[Example_co]], *, container_strategy: Union[SearchStrategy[Example_co], Callable[[Type[Example_co]], SearchStrategy[Example_co]]], settings_kwargs: Optional[Dict[str, Any]] = ..., type_strategies: Optional[Dict[Type[object], Union[SearchStrategy[Any], Callable[[Type[Any]], SearchStrategy[Any]]]]] = ...) -> None
5252
main:8: note: def [Example_co] check_all_laws(container_type: Type[Lawful[Example_co]], *, settings_kwargs: Optional[Dict[str, Any]] = ..., use_init: bool = ...) -> None
5353
5454
- case: test_all_laws_requires_container_strategy
@@ -64,11 +64,11 @@
6464
from returns.contrib.hypothesis.laws import check_all_laws
6565
from returns.result import Result, Success
6666
67-
check_all_laws(Result, container_strategy=st.builds(Success, st.integers()), other_strategies={int: st.integers()})
68-
check_all_laws(Result, other_strategies={int: st.integers()})
67+
check_all_laws(Result, container_strategy=st.builds(Success, st.integers()), type_strategies={int: st.integers()})
68+
check_all_laws(Result, type_strategies={int: st.integers()})
6969
7070
out: |
7171
main:6: error: No overload variant of "check_all_laws" matches argument types "Type[Result[_ValueType_co, _ErrorType_co]]", "Dict[Type[int], SearchStrategy[int]]" [call-overload]
7272
main:6: note: Possible overload variants:
73-
main:6: note: def [Example_co] check_all_laws(container_type: Type[Lawful[Example_co]], *, container_strategy: Union[SearchStrategy[Example_co], Callable[[Type[Example_co]], SearchStrategy[Example_co]]], settings_kwargs: Optional[Dict[str, Any]] = ..., other_strategies: Optional[Dict[Type[object], Union[SearchStrategy[Any], Callable[[Type[Any]], SearchStrategy[Any]]]]] = ...) -> None
73+
main:6: note: def [Example_co] check_all_laws(container_type: Type[Lawful[Example_co]], *, container_strategy: Union[SearchStrategy[Example_co], Callable[[Type[Example_co]], SearchStrategy[Example_co]]], settings_kwargs: Optional[Dict[str, Any]] = ..., type_strategies: Optional[Dict[Type[object], Union[SearchStrategy[Any], Callable[[Type[Any]], SearchStrategy[Any]]]]] = ...) -> None
7474
main:6: note: def [Example_co] check_all_laws(container_type: Type[Lawful[Example_co]], *, settings_kwargs: Optional[Dict[str, Any]] = ..., use_init: bool = ...) -> None

0 commit comments

Comments
 (0)