File: _entrypoint.py

package info (click to toggle)
python-returns 0.26.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,652 kB
  • sloc: python: 11,000; makefile: 18
file content (64 lines) | stat: -rw-r--r-- 1,831 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""
Used to register all our types as hypothesis strategies.

See: https://hypothesis.readthedocs.io/en/latest/strategies.html

But, beware that we only register concrete types here,
interfaces won't be registered!

"""

from __future__ import annotations

from collections.abc import Callable, Sequence
from typing import TYPE_CHECKING, Any, TypeVar

if TYPE_CHECKING:
    from returns.primitives.laws import Lawful

_Inst = TypeVar('_Inst', bound='Lawful')


def _setup_hook() -> None:
    from hypothesis import strategies as st  # noqa: PLC0415

    from returns.context import (  # noqa: PLC0415
        RequiresContext,
        RequiresContextFutureResult,
        RequiresContextIOResult,
        RequiresContextResult,
    )
    from returns.future import Future, FutureResult  # noqa: PLC0415
    from returns.io import IO, IOResult  # noqa: PLC0415
    from returns.maybe import Maybe  # noqa: PLC0415
    from returns.result import Result  # noqa: PLC0415

    def factory(
        container_type: type[_Inst],
    ) -> Callable[[Any], st.SearchStrategy[_Inst]]:
        def decorator(thing: Any) -> st.SearchStrategy[_Inst]:
            from returns.contrib.hypothesis.containers import (  # noqa: PLC0415
                strategy_from_container,
            )

            return strategy_from_container(container_type)(thing)

        return decorator

    #: Our types that we register in hypothesis
    #: to be working with ``st.from_type``
    registered_types: Sequence[type[Lawful]] = (
        Result,
        Maybe,
        IO,
        IOResult,
        Future,
        FutureResult,
        RequiresContext,
        RequiresContextResult,
        RequiresContextIOResult,
        RequiresContextFutureResult,
    )

    for type_ in registered_types:
        st.register_type_strategy(type_, factory(type_))