Coverage for src/PythonMetaTrader5/result.py: 100%
21 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-12 12:29 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-12 12:29 +0000
1"""Result type used at every port and use-case boundary.
3Chosen over exceptions at boundaries because a trading call has many *expected*
4failures (market closed, no money, requote); an exception-based API lets callers
5forget them, while a Result forces the caller to look at the outcome.
6"""
8from __future__ import annotations
10from dataclasses import dataclass
11from typing import Generic, TypeVar, Union
13T = TypeVar("T")
14E = TypeVar("E")
17@dataclass(frozen=True, slots=True)
18class Ok(Generic[T]):
19 """A call that succeeded, carrying its ``value``."""
21 value: T
23 def is_ok(self) -> bool:
24 """Always ``True``; prefer ``match`` or ``isinstance`` to narrow the type."""
25 return True
27 def is_err(self) -> bool:
28 """Always ``False``."""
29 return False
32@dataclass(frozen=True, slots=True)
33class Err(Generic[E]):
34 """A call that failed in an expected way, carrying its ``error``."""
36 error: E
38 def is_ok(self) -> bool:
39 """Always ``False``."""
40 return False
42 def is_err(self) -> bool:
43 """Always ``True``; prefer ``match`` or ``isinstance`` to narrow the type."""
44 return True
47# Union (not PEP 604 `|`) because a generic alias built with `|` cannot be
48# subscripted at runtime on Python 3.10.
49Result = Union[Ok[T], Err[E]] # noqa: UP007
50"""Either ``Ok(value)`` or ``Err(error)``; every trading call returns one."""