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

1"""Result type used at every port and use-case boundary. 

2 

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""" 

7 

8from __future__ import annotations 

9 

10from dataclasses import dataclass 

11from typing import Generic, TypeVar, Union 

12 

13T = TypeVar("T") 

14E = TypeVar("E") 

15 

16 

17@dataclass(frozen=True, slots=True) 

18class Ok(Generic[T]): 

19 """A call that succeeded, carrying its ``value``.""" 

20 

21 value: T 

22 

23 def is_ok(self) -> bool: 

24 """Always ``True``; prefer ``match`` or ``isinstance`` to narrow the type.""" 

25 return True 

26 

27 def is_err(self) -> bool: 

28 """Always ``False``.""" 

29 return False 

30 

31 

32@dataclass(frozen=True, slots=True) 

33class Err(Generic[E]): 

34 """A call that failed in an expected way, carrying its ``error``.""" 

35 

36 error: E 

37 

38 def is_ok(self) -> bool: 

39 """Always ``False``.""" 

40 return False 

41 

42 def is_err(self) -> bool: 

43 """Always ``True``; prefer ``match`` or ``isinstance`` to narrow the type.""" 

44 return True 

45 

46 

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."""