rust-ok 0.1.0__tar.gz → 0.2.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {rust_ok-0.1.0 → rust_ok-0.2.1}/PKG-INFO +2 -2
- {rust_ok-0.1.0 → rust_ok-0.2.1}/pyproject.toml +57 -7
- {rust_ok-0.1.0 → rust_ok-0.2.1}/src/rust_ok/__init__.py +7 -2
- {rust_ok-0.1.0 → rust_ok-0.2.1}/src/rust_ok/err.py +30 -21
- rust_ok-0.2.1/src/rust_ok/ok.py +101 -0
- {rust_ok-0.1.0 → rust_ok-0.2.1}/src/rust_ok/result.py +21 -18
- {rust_ok-0.1.0 → rust_ok-0.2.1}/src/rust_ok/trace.py +1 -1
- rust_ok-0.1.0/src/rust_ok/ok.py +0 -94
- {rust_ok-0.1.0 → rust_ok-0.2.1}/LICENSE.txt +0 -0
- {rust_ok-0.1.0 → rust_ok-0.2.1}/README.md +0 -0
- {rust_ok-0.1.0 → rust_ok-0.2.1}/src/rust_ok/exceptions.py +0 -0
- {rust_ok-0.1.0 → rust_ok-0.2.1}/src/rust_ok/guards.py +0 -0
- {rust_ok-0.1.0 → rust_ok-0.2.1}/src/rust_ok/py.typed +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.3
|
|
2
2
|
Name: rust-ok
|
|
3
|
-
Version: 0.1
|
|
3
|
+
Version: 0.2.1
|
|
4
4
|
Summary: Rust-inspired Result/Ok/Err primitives for Python
|
|
5
5
|
Keywords: result,error-handling,rust,functional,typing
|
|
6
6
|
Author: pesap
|
|
@@ -33,7 +33,7 @@ License: BSD 3-Clause License
|
|
|
33
33
|
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
34
34
|
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
35
35
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
36
|
-
Classifier: Development Status ::
|
|
36
|
+
Classifier: Development Status :: 4 - Beta
|
|
37
37
|
Classifier: Intended Audience :: Developers
|
|
38
38
|
Classifier: License :: OSI Approved :: BSD License
|
|
39
39
|
Classifier: Natural Language :: English
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "rust-ok"
|
|
3
|
-
version = "0.1
|
|
3
|
+
version = "0.2.1"
|
|
4
4
|
description = "Rust-inspired Result/Ok/Err primitives for Python"
|
|
5
5
|
readme = "README.md"
|
|
6
6
|
authors = [
|
|
@@ -16,7 +16,7 @@ keywords = [
|
|
|
16
16
|
"typing",
|
|
17
17
|
]
|
|
18
18
|
classifiers = [
|
|
19
|
-
"Development Status ::
|
|
19
|
+
"Development Status :: 4 - Beta",
|
|
20
20
|
"Intended Audience :: Developers",
|
|
21
21
|
"License :: OSI Approved :: BSD License",
|
|
22
22
|
"Natural Language :: English",
|
|
@@ -29,23 +29,73 @@ classifiers = [
|
|
|
29
29
|
]
|
|
30
30
|
dependencies = []
|
|
31
31
|
|
|
32
|
+
[build-system]
|
|
33
|
+
requires = ["uv_build>=0.9.10,<0.10.0"]
|
|
34
|
+
build-backend = "uv_build"
|
|
35
|
+
|
|
32
36
|
[dependency-groups]
|
|
33
37
|
dev = [
|
|
34
|
-
"mypy>=1.
|
|
38
|
+
"mypy>=1.19.1",
|
|
35
39
|
"pre-commit>=4.5.0,<5.0.0",
|
|
40
|
+
"prek>=0.2.29",
|
|
36
41
|
"pytest>=8.3.0,<9.0.0",
|
|
37
42
|
"pytest-cov>=5.0.0,<6.0.0",
|
|
38
43
|
"ruff>=0.6.8,<0.7.0",
|
|
44
|
+
"ty>=0.0.10",
|
|
45
|
+
]
|
|
46
|
+
|
|
47
|
+
[tool.ruff]
|
|
48
|
+
line-length = 110
|
|
49
|
+
extend-exclude = [
|
|
50
|
+
".venv",
|
|
51
|
+
"venv",
|
|
52
|
+
"build",
|
|
53
|
+
"dist",
|
|
54
|
+
"*.egg-info",
|
|
55
|
+
]
|
|
56
|
+
|
|
57
|
+
[tool.ruff.lint]
|
|
58
|
+
select = [
|
|
59
|
+
"E", # pycodestyle errors
|
|
60
|
+
"W", # pycodestyle warnings
|
|
61
|
+
"F", # pyflakes
|
|
62
|
+
"I", # isort
|
|
63
|
+
"N", # pep8-naming
|
|
64
|
+
"UP", # pyupgrade
|
|
65
|
+
"B", # flake8-bugbear
|
|
66
|
+
"C4", # flake8-comprehensions
|
|
67
|
+
"SIM", # flake8-simplify
|
|
68
|
+
"PIE", # flake8-pie
|
|
69
|
+
"PERF", # perflint (performance)
|
|
70
|
+
"RUF", # ruff-specific rules
|
|
39
71
|
]
|
|
40
72
|
|
|
73
|
+
[tool.ruff.lint.mccabe]
|
|
74
|
+
max-complexity = 10
|
|
75
|
+
|
|
41
76
|
[tool.pytest.ini_options]
|
|
42
77
|
pythonpath = ["src"]
|
|
43
78
|
testpaths = ["tests"]
|
|
44
79
|
addopts = [
|
|
45
80
|
"--cov=rust_ok",
|
|
46
|
-
"--cov-report=term-missing",
|
|
81
|
+
"--cov-report=term-missing:skip-covered",
|
|
82
|
+
"--cov-report=html",
|
|
83
|
+
"--cov-report=json",
|
|
84
|
+
"-v",
|
|
47
85
|
]
|
|
48
86
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
87
|
+
|
|
88
|
+
[tool.coverage.run]
|
|
89
|
+
source = ["src/rust_ok"]
|
|
90
|
+
omit = [
|
|
91
|
+
"*/tests/*",
|
|
92
|
+
"*/__pycache__/*",
|
|
93
|
+
"*/site-packages/*",
|
|
94
|
+
]
|
|
95
|
+
|
|
96
|
+
[tool.coverage.report]
|
|
97
|
+
precision = 2
|
|
98
|
+
fail_under = 99
|
|
99
|
+
|
|
100
|
+
[tool.coverage.html]
|
|
101
|
+
directory = "htmlcov"
|
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
"""Public API for rust-ok."""
|
|
2
2
|
|
|
3
|
-
from .
|
|
3
|
+
from importlib.metadata import version
|
|
4
|
+
|
|
5
|
+
__version__ = version("rust_ok")
|
|
6
|
+
|
|
4
7
|
from .err import Err
|
|
5
|
-
from .
|
|
8
|
+
from .exceptions import IsNotError, RustOkError, UnwrapError
|
|
6
9
|
from .guards import is_err, is_ok
|
|
10
|
+
from .ok import Ok
|
|
7
11
|
from .result import Result
|
|
8
12
|
from .trace import format_exception_chain, iter_causes
|
|
9
13
|
|
|
@@ -18,4 +22,5 @@ __all__ = [
|
|
|
18
22
|
"iter_causes",
|
|
19
23
|
"is_err",
|
|
20
24
|
"is_ok",
|
|
25
|
+
"__version__",
|
|
21
26
|
]
|
|
@@ -2,24 +2,31 @@
|
|
|
2
2
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
|
-
from
|
|
5
|
+
from collections.abc import Callable
|
|
6
|
+
from typing import Any, Never, TypeVar, cast, overload
|
|
6
7
|
|
|
7
8
|
from .exceptions import UnwrapError
|
|
8
9
|
from .result import Result
|
|
9
10
|
|
|
10
|
-
|
|
11
|
-
|
|
11
|
+
T_co = TypeVar("T_co", covariant=True)
|
|
12
|
+
E_co = TypeVar("E_co", covariant=True)
|
|
12
13
|
U = TypeVar("U")
|
|
13
14
|
F = TypeVar("F")
|
|
14
15
|
|
|
15
16
|
|
|
16
|
-
class Err(Result[
|
|
17
|
+
class Err(Result[T_co, E_co]):
|
|
17
18
|
"""Error result containing an error value."""
|
|
18
19
|
|
|
19
20
|
__slots__ = ("_error_value",)
|
|
20
21
|
__match_args__ = ("error",)
|
|
21
22
|
|
|
22
|
-
|
|
23
|
+
@overload
|
|
24
|
+
def __init__(self: Err[Never, E_co], error: E_co) -> None: ...
|
|
25
|
+
|
|
26
|
+
@overload
|
|
27
|
+
def __init__(self: Err[Any, Any], error: Any) -> None: ...
|
|
28
|
+
|
|
29
|
+
def __init__(self, error: E_co | Any) -> None:
|
|
23
30
|
self._error_value = error
|
|
24
31
|
|
|
25
32
|
def __repr__(self) -> str:
|
|
@@ -39,19 +46,19 @@ class Err(Result[T, E]):
|
|
|
39
46
|
def __bool__(self) -> bool:
|
|
40
47
|
return False
|
|
41
48
|
|
|
42
|
-
def unwrap(self) ->
|
|
49
|
+
def unwrap(self) -> T_co:
|
|
43
50
|
raise UnwrapError(f"Called unwrap on Err: {self._error_value}")
|
|
44
51
|
|
|
45
|
-
def unwrap_err(self) ->
|
|
52
|
+
def unwrap_err(self) -> E_co:
|
|
46
53
|
return self._error_value
|
|
47
54
|
|
|
48
|
-
def unwrap_or(self, default:
|
|
55
|
+
def unwrap_or(self, default: T_co | Any) -> T_co:
|
|
49
56
|
return default
|
|
50
57
|
|
|
51
|
-
def unwrap_or_else(self, func: Callable[[
|
|
58
|
+
def unwrap_or_else(self, func: Callable[[Any], T_co]) -> T_co:
|
|
52
59
|
return func(self._error_value)
|
|
53
60
|
|
|
54
|
-
def expect(self, msg: str) ->
|
|
61
|
+
def expect(self, msg: str) -> T_co:
|
|
55
62
|
raise UnwrapError(f"{msg}: {self._error_value}")
|
|
56
63
|
|
|
57
64
|
def is_ok(self) -> bool:
|
|
@@ -60,29 +67,31 @@ class Err(Result[T, E]):
|
|
|
60
67
|
def is_err(self) -> bool:
|
|
61
68
|
return True
|
|
62
69
|
|
|
63
|
-
def map(self, func: Callable[[
|
|
64
|
-
|
|
70
|
+
def map(self, func: Callable[[Any], U]) -> Result[U, E_co]:
|
|
71
|
+
"""Return Err; func is never called."""
|
|
72
|
+
return cast(Result[U, E_co], Err(self._error_value))
|
|
65
73
|
|
|
66
|
-
def map_err(self, func: Callable[[
|
|
67
|
-
return Err(func(self._error_value))
|
|
74
|
+
def map_err(self, func: Callable[[Any], F]) -> Result[T_co, F]:
|
|
75
|
+
return cast(Result[T_co, F], Err(func(self._error_value)))
|
|
68
76
|
|
|
69
|
-
def and_then(self, func: Callable[[
|
|
70
|
-
|
|
77
|
+
def and_then(self, func: Callable[[Any], Result[U, E_co]]) -> Result[U, E_co]:
|
|
78
|
+
"""Return Err; func is never called."""
|
|
79
|
+
return cast(Result[U, E_co], Err(self._error_value))
|
|
71
80
|
|
|
72
|
-
def or_else(self, func: Callable[[
|
|
81
|
+
def or_else(self, func: Callable[[Any], Result[T_co, F]]) -> Result[T_co, F]:
|
|
73
82
|
return func(self._error_value)
|
|
74
83
|
|
|
75
|
-
def ok(self) ->
|
|
84
|
+
def ok(self) -> T_co | None:
|
|
76
85
|
return None
|
|
77
86
|
|
|
78
|
-
def err(self) ->
|
|
87
|
+
def err(self) -> E_co:
|
|
79
88
|
return self._error_value
|
|
80
89
|
|
|
81
90
|
def unwrap_or_raise(
|
|
82
91
|
self,
|
|
83
92
|
exc_type: type[BaseException] = Exception,
|
|
84
|
-
context:
|
|
85
|
-
) ->
|
|
93
|
+
context: str | None = None,
|
|
94
|
+
) -> T_co:
|
|
86
95
|
payload = self._error_value
|
|
87
96
|
msg = context if context is not None else str(payload)
|
|
88
97
|
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
"""Implementation of the Ok variant."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Callable
|
|
6
|
+
from typing import Any, Literal, Never, TypeVar, cast, overload
|
|
7
|
+
|
|
8
|
+
from .exceptions import IsNotError, UnwrapError
|
|
9
|
+
from .result import Result
|
|
10
|
+
|
|
11
|
+
T_co = TypeVar("T_co", covariant=True)
|
|
12
|
+
E_co = TypeVar("E_co", covariant=True)
|
|
13
|
+
U = TypeVar("U")
|
|
14
|
+
F = TypeVar("F")
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class Ok(Result[T_co, E_co]):
|
|
18
|
+
"""Success result containing a value."""
|
|
19
|
+
|
|
20
|
+
__slots__ = ("value",)
|
|
21
|
+
__match_args__ = ("value",)
|
|
22
|
+
|
|
23
|
+
value: T_co
|
|
24
|
+
|
|
25
|
+
@overload
|
|
26
|
+
def __init__(self: Ok[T_co, Never], value: T_co) -> None: ...
|
|
27
|
+
|
|
28
|
+
@overload
|
|
29
|
+
def __init__(self: Ok[Any, Any]) -> None: ...
|
|
30
|
+
|
|
31
|
+
@overload
|
|
32
|
+
def __init__(self: Ok[None, Any], value: Literal[None]) -> None: ...
|
|
33
|
+
|
|
34
|
+
def __init__(self, value: T_co | None = None) -> None:
|
|
35
|
+
self.value = value # type: ignore[assignment]
|
|
36
|
+
|
|
37
|
+
def __repr__(self) -> str:
|
|
38
|
+
return f"Ok({self.value!r})"
|
|
39
|
+
|
|
40
|
+
def __str__(self) -> str:
|
|
41
|
+
return f"Ok({self.value})"
|
|
42
|
+
|
|
43
|
+
def __eq__(self, other: object) -> bool:
|
|
44
|
+
if isinstance(other, Ok):
|
|
45
|
+
return bool(self.value == other.value)
|
|
46
|
+
return False
|
|
47
|
+
|
|
48
|
+
def __hash__(self) -> int:
|
|
49
|
+
return hash(("Ok", self.value))
|
|
50
|
+
|
|
51
|
+
def __bool__(self) -> bool:
|
|
52
|
+
return True
|
|
53
|
+
|
|
54
|
+
def unwrap(self) -> T_co:
|
|
55
|
+
return self.value
|
|
56
|
+
|
|
57
|
+
def unwrap_err(self) -> E_co:
|
|
58
|
+
raise UnwrapError("Called unwrap_err on Ok")
|
|
59
|
+
|
|
60
|
+
def unwrap_or(self, default: T_co | Any) -> T_co:
|
|
61
|
+
return self.value
|
|
62
|
+
|
|
63
|
+
def unwrap_or_else(self, func: Callable[[Any], T_co]) -> T_co:
|
|
64
|
+
"""Return value; func is never called in Ok."""
|
|
65
|
+
return self.value
|
|
66
|
+
|
|
67
|
+
def expect(self, msg: str) -> T_co:
|
|
68
|
+
return self.value
|
|
69
|
+
|
|
70
|
+
def is_ok(self) -> bool:
|
|
71
|
+
return True
|
|
72
|
+
|
|
73
|
+
def is_err(self) -> bool:
|
|
74
|
+
return False
|
|
75
|
+
|
|
76
|
+
def map(self, func: Callable[[Any], U]) -> Result[U, E_co]:
|
|
77
|
+
return Ok(func(self.value))
|
|
78
|
+
|
|
79
|
+
def map_err(self, func: Callable[[Any], F]) -> Result[T_co, F]:
|
|
80
|
+
"""Phantom type: func is never called in Ok."""
|
|
81
|
+
return cast(Result[T_co, F], Ok(self.value))
|
|
82
|
+
|
|
83
|
+
def and_then(self, func: Callable[[Any], Result[U, E_co]]) -> Result[U, E_co]:
|
|
84
|
+
return func(self.value)
|
|
85
|
+
|
|
86
|
+
def or_else(self, func: Callable[[Any], Result[T_co, F]]) -> Result[T_co, F]:
|
|
87
|
+
"""Phantom type: func is never called in Ok."""
|
|
88
|
+
return cast(Result[T_co, F], Ok(self.value))
|
|
89
|
+
|
|
90
|
+
def ok(self) -> T_co:
|
|
91
|
+
return self.value
|
|
92
|
+
|
|
93
|
+
def err(self) -> E_co:
|
|
94
|
+
raise IsNotError
|
|
95
|
+
|
|
96
|
+
def unwrap_or_raise(
|
|
97
|
+
self,
|
|
98
|
+
exc_type: type[BaseException] = Exception,
|
|
99
|
+
context: str | None = None,
|
|
100
|
+
) -> T_co:
|
|
101
|
+
return self.value
|
|
@@ -2,36 +2,39 @@
|
|
|
2
2
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
|
-
from
|
|
5
|
+
from collections.abc import Callable
|
|
6
|
+
from typing import Any, Generic, TypeVar
|
|
6
7
|
|
|
7
|
-
|
|
8
|
-
|
|
8
|
+
# Covariant type variables for Result base type
|
|
9
|
+
T_co = TypeVar("T_co", covariant=True)
|
|
10
|
+
E_co = TypeVar("E_co", covariant=True)
|
|
11
|
+
# TypeVars for transformation method return types
|
|
9
12
|
U = TypeVar("U")
|
|
10
13
|
F = TypeVar("F")
|
|
11
14
|
|
|
12
15
|
|
|
13
|
-
class Result(Generic[
|
|
16
|
+
class Result(Generic[T_co, E_co]):
|
|
14
17
|
"""Base type for Ok/Err results."""
|
|
15
18
|
|
|
16
19
|
__slots__ = ()
|
|
17
20
|
|
|
18
|
-
def unwrap(self) ->
|
|
21
|
+
def unwrap(self) -> T_co:
|
|
19
22
|
"""Return the contained value if successful, else raise in subclass."""
|
|
20
23
|
raise NotImplementedError # pragma: no cover
|
|
21
24
|
|
|
22
|
-
def unwrap_err(self) ->
|
|
25
|
+
def unwrap_err(self) -> E_co:
|
|
23
26
|
"""Return the contained error if Err, else raise in subclass."""
|
|
24
27
|
raise NotImplementedError # pragma: no cover
|
|
25
28
|
|
|
26
|
-
def unwrap_or(self, default:
|
|
29
|
+
def unwrap_or(self, default: T_co | Any) -> T_co:
|
|
27
30
|
"""Return the contained value if Ok, otherwise return the default."""
|
|
28
31
|
raise NotImplementedError # pragma: no cover
|
|
29
32
|
|
|
30
|
-
def unwrap_or_else(self, func: Callable[[
|
|
33
|
+
def unwrap_or_else(self, func: Callable[[Any], T_co]) -> T_co:
|
|
31
34
|
"""Return the contained value if Ok, otherwise compute a default."""
|
|
32
35
|
raise NotImplementedError # pragma: no cover
|
|
33
36
|
|
|
34
|
-
def expect(self, msg: str) ->
|
|
37
|
+
def expect(self, msg: str) -> T_co:
|
|
35
38
|
"""Return the contained value if Ok, otherwise raise with custom message."""
|
|
36
39
|
raise NotImplementedError # pragma: no cover
|
|
37
40
|
|
|
@@ -47,39 +50,39 @@ class Result(Generic[T, E]):
|
|
|
47
50
|
|
|
48
51
|
return isinstance(self, Err)
|
|
49
52
|
|
|
50
|
-
def map(self, func: Callable[[
|
|
53
|
+
def map(self, func: Callable[[Any], U]) -> Result[U, E_co]:
|
|
51
54
|
"""Apply func to the contained value if Ok, returning a new Result."""
|
|
52
55
|
raise NotImplementedError # pragma: no cover
|
|
53
56
|
|
|
54
|
-
def map_err(self, func: Callable[[
|
|
57
|
+
def map_err(self, func: Callable[[Any], F]) -> Result[T_co, F]:
|
|
55
58
|
"""Apply func to the error if Err, returning a new Result."""
|
|
56
59
|
raise NotImplementedError # pragma: no cover
|
|
57
60
|
|
|
58
|
-
def and_then(self, func: Callable[[
|
|
61
|
+
def and_then(self, func: Callable[[Any], Result[U, E_co]]) -> Result[U, E_co]:
|
|
59
62
|
"""Chain another computation on the contained value if Ok."""
|
|
60
63
|
raise NotImplementedError # pragma: no cover
|
|
61
64
|
|
|
62
|
-
def or_else(self, func: Callable[[
|
|
65
|
+
def or_else(self, func: Callable[[Any], Result[T_co, F]]) -> Result[T_co, F]:
|
|
63
66
|
"""Handle the error by calling func if Err, returning a new Result."""
|
|
64
67
|
raise NotImplementedError # pragma: no cover
|
|
65
68
|
|
|
66
|
-
def ok(self) ->
|
|
69
|
+
def ok(self) -> T_co | None:
|
|
67
70
|
"""Return the success value if Ok, otherwise None."""
|
|
68
71
|
raise NotImplementedError # pragma: no cover
|
|
69
72
|
|
|
70
|
-
def err(self) ->
|
|
73
|
+
def err(self) -> E_co:
|
|
71
74
|
"""Return the error value if Err, otherwise raise in subclass."""
|
|
72
75
|
raise NotImplementedError # pragma: no cover
|
|
73
76
|
|
|
74
77
|
@property
|
|
75
|
-
def error(self) ->
|
|
78
|
+
def error(self) -> E_co | None:
|
|
76
79
|
"""Return the error value if Err, otherwise None."""
|
|
77
80
|
return self.err()
|
|
78
81
|
|
|
79
82
|
def unwrap_or_raise(
|
|
80
83
|
self,
|
|
81
84
|
exc_type: type[BaseException] = Exception,
|
|
82
|
-
context:
|
|
83
|
-
) ->
|
|
85
|
+
context: str | None = None,
|
|
86
|
+
) -> T_co:
|
|
84
87
|
"""Return the Ok value or raise `exc_type`."""
|
|
85
88
|
raise NotImplementedError # pragma: no cover
|
rust_ok-0.1.0/src/rust_ok/ok.py
DELETED
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
"""Implementation of the Ok variant."""
|
|
2
|
-
|
|
3
|
-
from __future__ import annotations
|
|
4
|
-
|
|
5
|
-
from typing import Callable, Literal, Optional, TypeVar, cast, overload
|
|
6
|
-
|
|
7
|
-
from .exceptions import IsNotError, UnwrapError
|
|
8
|
-
from .result import Result
|
|
9
|
-
|
|
10
|
-
T = TypeVar("T")
|
|
11
|
-
E = TypeVar("E")
|
|
12
|
-
U = TypeVar("U")
|
|
13
|
-
F = TypeVar("F")
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
class Ok(Result[T, E]):
|
|
17
|
-
"""Success result containing a value."""
|
|
18
|
-
|
|
19
|
-
__slots__ = ("value",)
|
|
20
|
-
__match_args__ = ("value",)
|
|
21
|
-
|
|
22
|
-
value: T
|
|
23
|
-
|
|
24
|
-
@overload
|
|
25
|
-
def __init__(self: "Ok[None, E]", value: Literal[None] = None) -> None: ...
|
|
26
|
-
|
|
27
|
-
@overload
|
|
28
|
-
def __init__(self: "Ok[T, E]", value: T) -> None: ...
|
|
29
|
-
|
|
30
|
-
def __init__(self, value: Optional[T] = None) -> None:
|
|
31
|
-
self.value = cast(T, value)
|
|
32
|
-
|
|
33
|
-
def __repr__(self) -> str:
|
|
34
|
-
return f"Ok({self.value!r})"
|
|
35
|
-
|
|
36
|
-
def __str__(self) -> str:
|
|
37
|
-
return f"Ok({self.value})"
|
|
38
|
-
|
|
39
|
-
def __eq__(self, other: object) -> bool:
|
|
40
|
-
if isinstance(other, Ok):
|
|
41
|
-
return bool(self.value == other.value)
|
|
42
|
-
return False
|
|
43
|
-
|
|
44
|
-
def __hash__(self) -> int:
|
|
45
|
-
return hash(("Ok", self.value))
|
|
46
|
-
|
|
47
|
-
def __bool__(self) -> bool:
|
|
48
|
-
return True
|
|
49
|
-
|
|
50
|
-
def unwrap(self) -> T:
|
|
51
|
-
return self.value
|
|
52
|
-
|
|
53
|
-
def unwrap_err(self) -> E:
|
|
54
|
-
raise UnwrapError("Called unwrap_err on Ok")
|
|
55
|
-
|
|
56
|
-
def unwrap_or(self, default: T) -> T:
|
|
57
|
-
return self.value
|
|
58
|
-
|
|
59
|
-
def unwrap_or_else(self, func: Callable[[E], T]) -> T:
|
|
60
|
-
return self.value
|
|
61
|
-
|
|
62
|
-
def expect(self, msg: str) -> T:
|
|
63
|
-
return self.value
|
|
64
|
-
|
|
65
|
-
def is_ok(self) -> bool:
|
|
66
|
-
return True
|
|
67
|
-
|
|
68
|
-
def is_err(self) -> bool:
|
|
69
|
-
return False
|
|
70
|
-
|
|
71
|
-
def map(self, func: Callable[[T], U]) -> Result[U, E]:
|
|
72
|
-
return Ok(func(self.value))
|
|
73
|
-
|
|
74
|
-
def map_err(self, func: Callable[[E], F]) -> Result[T, F]:
|
|
75
|
-
return Ok(self.value)
|
|
76
|
-
|
|
77
|
-
def and_then(self, func: Callable[[T], Result[U, E]]) -> Result[U, E]:
|
|
78
|
-
return func(self.value)
|
|
79
|
-
|
|
80
|
-
def or_else(self, func: Callable[[E], Result[T, F]]) -> Result[T, F]:
|
|
81
|
-
return Ok(self.value)
|
|
82
|
-
|
|
83
|
-
def ok(self) -> T:
|
|
84
|
-
return self.value
|
|
85
|
-
|
|
86
|
-
def err(self) -> E:
|
|
87
|
-
raise IsNotError
|
|
88
|
-
|
|
89
|
-
def unwrap_or_raise(
|
|
90
|
-
self,
|
|
91
|
-
exc_type: type[BaseException] = Exception,
|
|
92
|
-
context: str | None = None,
|
|
93
|
-
) -> T:
|
|
94
|
-
return self.value
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|