Skip to content

Commit 092e91d

Browse files
committed
hypothesis: expect strategy to match the container.
1 parent 62388bc commit 092e91d

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

returns/contrib/hypothesis/laws.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
)
1717
from returns.primitives.laws import LAWS_ATTRIBUTE, Law, Lawful
1818

19+
Example_co = TypeVar('Example_co', covariant=True)
20+
1921

2022
@final
2123
class Settings(NamedTuple):
@@ -27,11 +29,11 @@ class Settings(NamedTuple):
2729

2830

2931
def check_all_laws(
30-
container_type: type[Lawful],
32+
container_type: type[Lawful[Example_co]],
3133
*,
3234
settings_kwargs: dict[str, Any] | None = None,
3335
use_init: bool = False,
34-
container_strategy: StrategyFactory | None = None,
36+
container_strategy: StrategyFactory[Example_co] | None = None,
3537
) -> None:
3638
"""
3739
Function to check all defined mathematical laws in a specified container.

returns/contrib/hypothesis/type_resolver.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22

33
from collections.abc import Callable, Iterator, Mapping
44
from contextlib import contextmanager
5-
from typing import TypeAlias
5+
from typing import TypeAlias, TypeVar
66

77
from hypothesis import strategies as st
88
from hypothesis.strategies._internal import types # noqa: PLC2701
99

10+
Example_co = TypeVar('Example_co', covariant=True)
11+
1012
StrategyFactory: TypeAlias = (
11-
st.SearchStrategy | Callable[[type], st.SearchStrategy]
13+
st.SearchStrategy[Example_co]
14+
| Callable[[type[Example_co]], st.SearchStrategy[Example_co]]
1215
)
1316

1417

@@ -45,8 +48,8 @@ def strategies_for_types(
4548

4649

4750
def look_up_strategy(
48-
type_: type[object],
49-
) -> StrategyFactory | None:
51+
type_: type[Example_co],
52+
) -> StrategyFactory[Example_co] | None:
5053
"""Return the strategy used by `hypothesis`."""
5154
return types._global_type_lookup.get(type_) # noqa: SLF001
5255

@@ -60,8 +63,8 @@ def _remove_strategy(
6063

6164

6265
def apply_strategy(
63-
strategy: StrategyFactory, type_: type[object]
64-
) -> StrategyFactory:
66+
strategy: StrategyFactory[Example_co], type_: type[Example_co]
67+
) -> StrategyFactory[Example_co]:
6568
"""Apply `strategy` to `type_`."""
6669
if isinstance(strategy, st.SearchStrategy):
6770
return strategy

0 commit comments

Comments
 (0)