@@ -31,7 +31,16 @@ class _Settings:
3131 container_strategy : StrategyFactory | None
3232 other_strategies : dict [type [object ], StrategyFactory ]
3333
34+ def __post_init__ (self ) -> None :
35+ """Check that the settings are mutually compatible."""
36+ if self .use_init and self .container_strategy is not None :
37+ raise AssertionError (
38+ 'Expected only one of `use_init` and'
39+ ' `container_strategy` to be truthy'
40+ )
41+
3442 def __or__ (self , other : Self ) -> Self :
43+ """Merge the two settings, preferring values from `other`."""
3544 return _Settings (
3645 settings_kwargs = self .settings_kwargs | other .settings_kwargs ,
3746 use_init = self .use_init | other .use_init ,
@@ -41,20 +50,23 @@ def __or__(self, other: Self) -> Self:
4150 other_strategies = self .other_strategies | other .other_strategies ,
4251 )
4352
44- def __post_init__ (self ) -> None :
45- """Check that the settings are mutually compatible."""
46- if self .use_init and self .container_strategy is not None :
47- raise AssertionError (
48- 'Expected only one of `use_init` and'
49- ' `container_strategy` to be truthy'
50- )
51-
5253
53- def _default_settings (container_type : type [Lawful ]) -> _Settings :
54+ def default_settings (container_type : type [Lawful ]) -> _Settings :
5455 """Return default settings for creating law tests.
5556
56- We use special strategies for `TypeVar` and `Callable` by default, but
57- they can be overriden by the user if needed.
57+ We use some special strategies by default, but
58+ they can be overriden by the user if needed:
59+
60+ + `TypeVar`: We need to make sure that the values generated behave
61+ sensibly when tested for equality.
62+
63+ + `collections.abc.Callable`: We need to generate pure functions, which
64+ are not the default.
65+
66+ Note that this is `collections.abc.Callable`, NOT `typing.Callable`. This
67+ is because, at runtime, `typing.get_origin(Callable[[int], str])` is
68+ `collections.abc.Callable`. So, this is the type we should register with
69+ `hypothesis`.
5870 """
5971 return _Settings (
6072 settings_kwargs = {},
@@ -120,7 +132,7 @@ def check_all_laws(
120132 - https://mmhaskell.com/blog/2017/3/13/obey-the-type-laws
121133
122134 """
123- settings = _Settings (
135+ settings = default_settings ( container_type ) | _Settings (
124136 settings_kwargs or {},
125137 use_init ,
126138 container_strategy ,
@@ -255,13 +267,9 @@ def _types_to_strategies(
255267 container_type : type [Lawful ],
256268 settings : _Settings ,
257269) -> dict [type [object ], StrategyFactory ]:
258- """Return a mapping from type to `hypothesis` strategy.
259-
260- We override the default settings with the user-provided `settings`.
261- """
262- merged_settings = _default_settings (container_type ) | settings
263- return merged_settings .other_strategies | _container_mapping (
264- container_type , merged_settings
270+ """Return a mapping from type to `hypothesis` strategy."""
271+ return settings .other_strategies | _container_mapping (
272+ container_type , settings
265273 )
266274
267275
0 commit comments