File: asserts.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 (54 lines) | stat: -rw-r--r-- 1,436 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
def assert_equal(
    first,
    second,
    *,
    deps=None,
    backend: str = 'asyncio',
) -> None:
    """
    Custom ``assert`` function to compare two any containers.

    The important note here is that
    this ``assert`` should probably used in tests.
    Not real application code.

    It will call all ``Reader`` based containers and ``await``
    all ``Future`` based ones.

    It also works recursively.
    For example, ``ReaderFutureResult`` will be called and then awaited.

    You can specify different dependencies to call your containers.
    And different backends to ``await`` then using ``anyio``.

    By the way, ``anyio`` should be installed separately.
    """
    assert _convert(
        first,
        deps=deps,
        backend=backend,
    ) == _convert(
        second,
        deps=deps,
        backend=backend,
    ), f'{first} == {second}'


def _convert(container, *, deps, backend: str):
    from returns.interfaces.specific import future, reader  # noqa: PLC0415

    if isinstance(container, future.AwaitableFutureN):
        import anyio  # noqa: PLC0415

        return _convert(
            anyio.run(container.awaitable, backend=backend),
            deps=deps,
            backend=backend,
        )
    if isinstance(container, reader.Contextable):
        return _convert(
            container(deps),
            deps=deps,
            backend=backend,
        )
    return container