Source code for stdlibx.result.types
1from __future__ import annotations
2
3from typing import Callable, Literal, Protocol, TypeVar, runtime_checkable
4
5from typing_extensions import TypeAlias
6
7T = TypeVar("T")
8E = TypeVar("E")
9U = TypeVar("U")
10
11Operation: TypeAlias = Callable[[T], U]
12
13
[docs]
14@runtime_checkable
15class Ok(Protocol[T]):
16 __match_args__ = ("value",)
17 value: T
18
[docs]
19 def is_ok(self) -> Literal[True]: ...
[docs]
20 def is_err(self) -> Literal[False]: ...
[docs]
21 def apply(self, operation: Operation[Result[T, E], U]) -> U: ...
22
23
[docs]
24@runtime_checkable
25class Error(Protocol[E]):
26 __match_args__ = ("error",)
27 error: E
28
[docs]
29 def is_ok(self) -> Literal[False]: ...
[docs]
30 def is_err(self) -> Literal[True]: ...
[docs]
31 def apply(self, operation: Operation[Result[T, E], U]) -> U: ...
32
33
34Result: TypeAlias = Ok[T] | Error[E]