package documentation

Typesafe railway-oriented programming (ROP).

This package provides

Package fp Functional interface for trcks.
Module oop Object-oriented interface for trcks.
Module _typing Recent features from typing.

From __init__.py:

Type Alias AwaitableFailure collections.abc.Awaitable that returns a Failure when used in an await expression.
Type Alias AwaitableResult collections.abc.Awaitable that returns a Result when used in an await expression.
Type Alias AwaitableSuccess collections.abc.Awaitable that returns a Success when used in an await expression.
Type Alias Failure tuple of length 2 containing "failure" followed by a value of type _F_co.
Type Alias Result Discriminated union of the generic types _F_co and _S_co.
Type Alias Success tuple of length 2 containing "success" followed by a value of type _S_co.
Variable _F_co Undocumented
Variable _S_co Undocumented
AwaitableFailure: TypeAlias = (source)

collections.abc.Awaitable that returns a Failure when used in an await expression.

Value
Awaitable[Failure[_F_co]]
AwaitableResult: TypeAlias = (source)

collections.abc.Awaitable that returns a Result when used in an await expression.

Examples

Can be used to annotate the non-awaited return value of an async function:

>>> import asyncio
>>> async def divide_slowly(
...     a: float, b: float
... ) -> Result[ZeroDivisionError, float]:
...     await asyncio.sleep(0.001)
...     try:
...         return ("success", a / b)
...     except ZeroDivisionError as e:
...         return ("failure", e)
...
>>> async def main() -> None:
...     a_rslt: AwaitableResult[ZeroDivisionError, float] = (
...         divide_slowly(3.0, 0.0)
...     )
...     rslt: Result[ZeroDivisionError, float] = await a_rslt
...     print(rslt)
...
>>> asyncio.run(main())
('failure', ZeroDivisionError('float division by zero'))

Can also be used to annotate an async function:

>>> import asyncio
>>> from collections.abc import Callable
>>>
>>> async def divide_slowly(
...     a: float, b: float
... ) -> Result[ZeroDivisionError, float]:
...     await asyncio.sleep(0.001)
...     try:
...         return ("success", a / b)
...     except ZeroDivisionError as e:
...         return ("failure", e)
...
>>> copy_of_divide_slowly: Callable[
...     [float, float], AwaitableResult[ZeroDivisionError, float]
... ] = divide_slowly
Value
Awaitable[Result[_F_co, _S_co]]
AwaitableSuccess: TypeAlias = (source)

collections.abc.Awaitable that returns a Success when used in an await expression.

Value
Awaitable[Success[_S_co]]
Failure: TypeAlias = (source)

tuple of length 2 containing "failure" followed by a value of type _F_co.

Example

>>> failure: Failure[str] = ("failure", "File does not exist")

Note

This generic type is called "Left" in some functional programming languages and packages (e.g. Haskell and fp-ts).

Value
tuple[Literal['failure'], _F_co]
Result: TypeAlias = (source)

Discriminated union of the generic types _F_co and _S_co.

Can be used as a return type of a function instead of returning _S_co and raising _F_co.

Example

>>> def divide(a: float, b: float) -> Result[ZeroDivisionError, float]:
...     try:
...         return ("success", a/b)
...     except ZeroDivisionError as e:
...         return ("failure", e)
...
>>> divide(5.0, 2.0)
('success', 2.5)
>>> divide(3.5, 0.0)
('failure', ZeroDivisionError('float division by zero'))

Note

This generic type is called "Either" in some functional programming languages and packages (e.g. Haskell and fp-ts).

Value
Failure[_F_co] | Success[_S_co]
Success: TypeAlias = (source)

tuple of length 2 containing "success" followed by a value of type _S_co.

Example

>>> success: Success[int] = ("success", 42)

Note

This generic type is called "Right" in some functional programming languages and packages (e.g. Haskell and fp-ts).

Value
tuple[Literal['success'], _S_co]

Undocumented

Undocumented