railtheway 0.2.0__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.
- railtheway-0.2.0/.github/workflows/ci.yml +32 -0
- railtheway-0.2.0/.github/workflows/publish.yml +50 -0
- railtheway-0.2.0/.gitignore +5 -0
- railtheway-0.2.0/CHANGELOG.md +18 -0
- railtheway-0.2.0/LICENSE +21 -0
- railtheway-0.2.0/PKG-INFO +236 -0
- railtheway-0.2.0/README.md +217 -0
- railtheway-0.2.0/docs/examples.md +841 -0
- railtheway-0.2.0/pyproject.toml +36 -0
- railtheway-0.2.0/src/railtheway/__init__.py +253 -0
- railtheway-0.2.0/src/railtheway/py.typed +0 -0
- railtheway-0.2.0/tests/test_documentation.py +24 -0
- railtheway-0.2.0/tests/test_result.py +665 -0
- railtheway-0.2.0/uv.lock +49 -0
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main]
|
|
6
|
+
pull_request:
|
|
7
|
+
branches: [main]
|
|
8
|
+
|
|
9
|
+
permissions:
|
|
10
|
+
contents: read
|
|
11
|
+
|
|
12
|
+
jobs:
|
|
13
|
+
check:
|
|
14
|
+
strategy:
|
|
15
|
+
fail-fast: false
|
|
16
|
+
matrix:
|
|
17
|
+
os: [ubuntu-latest, windows-latest, macos-latest]
|
|
18
|
+
python: ["3.12", "3.13", "3.14"]
|
|
19
|
+
runs-on: ${{ matrix.os }}
|
|
20
|
+
steps:
|
|
21
|
+
- uses: actions/checkout@v4
|
|
22
|
+
- uses: actions/setup-python@v5
|
|
23
|
+
with:
|
|
24
|
+
python-version: ${{ matrix.python }}
|
|
25
|
+
- uses: astral-sh/setup-uv@v6
|
|
26
|
+
- run: uv sync --locked
|
|
27
|
+
- run: uv run --no-sync python -m unittest discover -s tests -v
|
|
28
|
+
- run: uv run --no-sync pyright --pythonversion ${{ matrix.python }}
|
|
29
|
+
- run: uv build
|
|
30
|
+
- name: Verify isolated wheel installation
|
|
31
|
+
run: |
|
|
32
|
+
uv run --no-sync python -c "import pathlib, subprocess; wheel = next(pathlib.Path('dist').glob('*.whl')).resolve(); subprocess.run(['uv', 'run', '--no-project', '--with', str(wheel), 'python', '-c', 'from railtheway import Result; assert Result.from_success(42).unwrap() == 42'], check=True)"
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
name: Publish
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
tags: ["v*"]
|
|
6
|
+
|
|
7
|
+
permissions:
|
|
8
|
+
contents: read
|
|
9
|
+
|
|
10
|
+
jobs:
|
|
11
|
+
build:
|
|
12
|
+
runs-on: ubuntu-latest
|
|
13
|
+
steps:
|
|
14
|
+
- uses: actions/checkout@v4
|
|
15
|
+
- uses: actions/setup-python@v5
|
|
16
|
+
with:
|
|
17
|
+
python-version: "3.12"
|
|
18
|
+
- uses: astral-sh/setup-uv@v6
|
|
19
|
+
- run: uv sync --locked
|
|
20
|
+
- name: Verify release tag
|
|
21
|
+
env:
|
|
22
|
+
RELEASE_TAG: ${{ github.ref_name }}
|
|
23
|
+
run: |
|
|
24
|
+
uv run --no-sync python -c "import os, tomllib; from pathlib import Path; version = tomllib.loads(Path('pyproject.toml').read_text())['project']['version']; tag = os.environ['RELEASE_TAG']; assert tag == 'v' + version, f'Tag {tag} does not match version {version}'"
|
|
25
|
+
- run: uv run --no-sync python -m unittest discover -s tests -v
|
|
26
|
+
- run: uv run --no-sync pyright
|
|
27
|
+
- run: uv build
|
|
28
|
+
- name: Verify isolated wheel installation
|
|
29
|
+
run: |
|
|
30
|
+
uv run --no-sync python -c "import pathlib, subprocess; wheel = next(pathlib.Path('dist').glob('*.whl')).resolve(); subprocess.run(['uv', 'run', '--no-project', '--with', str(wheel), 'python', '-c', 'from railtheway import Result; assert Result.from_success(42).unwrap() == 42'], check=True)"
|
|
31
|
+
- uses: actions/upload-artifact@v4
|
|
32
|
+
with:
|
|
33
|
+
name: distributions
|
|
34
|
+
path: dist/
|
|
35
|
+
if-no-files-found: error
|
|
36
|
+
|
|
37
|
+
pypi:
|
|
38
|
+
needs: build
|
|
39
|
+
runs-on: ubuntu-latest
|
|
40
|
+
environment:
|
|
41
|
+
name: pypi
|
|
42
|
+
url: https://pypi.org/project/railtheway/
|
|
43
|
+
permissions:
|
|
44
|
+
id-token: write
|
|
45
|
+
steps:
|
|
46
|
+
- uses: actions/download-artifact@v4
|
|
47
|
+
with:
|
|
48
|
+
name: distributions
|
|
49
|
+
path: dist/
|
|
50
|
+
- uses: pypa/gh-action-pypi-publish@release/v1
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## Unreleased
|
|
4
|
+
|
|
5
|
+
### Changed
|
|
6
|
+
|
|
7
|
+
- Rename the distribution and import package to `railtheway`.
|
|
8
|
+
- Version 0.2 separates sync and async decorators: `as_result` never awaits
|
|
9
|
+
return values. Migrate coroutine functions to `as_async_result`, which also
|
|
10
|
+
accepts awaitable-returning factories and async callable objects.
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
|
|
14
|
+
- Typed Success and Failure variants with original errors and optional traceback snapshots.
|
|
15
|
+
- Value and error transformations, chaining, recovery, and extraction methods.
|
|
16
|
+
- Explicit sync/async exception-to-Result decorators with selective exception handling.
|
|
17
|
+
- Traceback formatting with line limits and custom separators.
|
|
18
|
+
- Typed package distributions and MIT licensing.
|
railtheway-0.2.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 railtheway contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: railtheway
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: Typed success and failure results with optional exception tracebacks.
|
|
5
|
+
Project-URL: Homepage, https://github.com/smocgeorgemarian/railtheway
|
|
6
|
+
Project-URL: Repository, https://github.com/smocgeorgemarian/railtheway
|
|
7
|
+
Project-URL: Issues, https://github.com/smocgeorgemarian/railtheway/issues
|
|
8
|
+
Project-URL: Documentation, https://github.com/smocgeorgemarian/railtheway/blob/main/docs/examples.md
|
|
9
|
+
License-Expression: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
16
|
+
Classifier: Typing :: Typed
|
|
17
|
+
Requires-Python: >=3.12
|
|
18
|
+
Description-Content-Type: text/markdown
|
|
19
|
+
|
|
20
|
+
# railtheway
|
|
21
|
+
|
|
22
|
+
Typed success and failure results for Python 3.12+, with sync/async decorators
|
|
23
|
+
and optional structured tracebacks. No runtime dependencies.
|
|
24
|
+
|
|
25
|
+
## Setup
|
|
26
|
+
|
|
27
|
+
From the project directory:
|
|
28
|
+
|
|
29
|
+
```powershell
|
|
30
|
+
uv sync
|
|
31
|
+
uv run python
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
To install a built wheel into another environment:
|
|
35
|
+
|
|
36
|
+
```powershell
|
|
37
|
+
uv pip install --python .venv\Scripts\python.exe <path-to-wheel.whl>
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Quick start
|
|
41
|
+
|
|
42
|
+
```pycon
|
|
43
|
+
>>> from railtheway import as_result
|
|
44
|
+
>>> @as_result()
|
|
45
|
+
... def divide(a: float, b: float) -> float:
|
|
46
|
+
... return a / b
|
|
47
|
+
...
|
|
48
|
+
>>> divide(10, 2)
|
|
49
|
+
Success(value=5.0, is_success=True)
|
|
50
|
+
>>> divide(10, 0)
|
|
51
|
+
Failure(error=ZeroDivisionError('division by zero'), traceback=None, is_success=False)
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Capture only expected exceptions
|
|
55
|
+
|
|
56
|
+
```pycon
|
|
57
|
+
>>> @as_result(exceptions=(ValueError, OSError))
|
|
58
|
+
... def parse(text: str) -> int:
|
|
59
|
+
... return int(text)
|
|
60
|
+
...
|
|
61
|
+
>>> parse("invalid")
|
|
62
|
+
Failure(error=ValueError("invalid literal for int() with base 10: 'invalid'"), traceback=None, is_success=False)
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Only matching exception types and their subclasses become Failure; others
|
|
66
|
+
propagate. The default `(Exception,)` preserves existing behavior.
|
|
67
|
+
`exceptions=()` captures nothing. Interruptions and cancellation cannot be selected.
|
|
68
|
+
|
|
69
|
+
## Handle either outcome
|
|
70
|
+
|
|
71
|
+
```pycon
|
|
72
|
+
>>> from railtheway import Result, ResultValue
|
|
73
|
+
>>> def describe(result: ResultValue[int]) -> str:
|
|
74
|
+
... if result.is_success is True:
|
|
75
|
+
... return f"Value: {result.value}"
|
|
76
|
+
... return f"Error: {result.error_message}"
|
|
77
|
+
...
|
|
78
|
+
>>> describe(Result.from_success(42))
|
|
79
|
+
'Value: 42'
|
|
80
|
+
>>> describe(Result.from_error("Unavailable"))
|
|
81
|
+
'Error: Unavailable'
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
`ResultValue[T]` is `Success[T] | Failure`. Use `is True` or `is False` for
|
|
85
|
+
Pyright type narrowing; plain `if result.is_success:` works at runtime.
|
|
86
|
+
|
|
87
|
+
```pycon
|
|
88
|
+
>>> def describe_error_first(result: ResultValue[int]) -> str:
|
|
89
|
+
... if result.is_error is True:
|
|
90
|
+
... return f"Error: {result.error_message}"
|
|
91
|
+
... return f"Value: {result.value}"
|
|
92
|
+
...
|
|
93
|
+
>>> describe_error_first(Result.from_error("Unavailable"))
|
|
94
|
+
'Error: Unavailable'
|
|
95
|
+
>>> describe_error_first(Result.from_success(42))
|
|
96
|
+
'Value: 42'
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## Transform, chain, and extract
|
|
100
|
+
|
|
101
|
+
```pycon
|
|
102
|
+
>>> @as_result()
|
|
103
|
+
... def parse_number(text: str) -> int:
|
|
104
|
+
... return int(text)
|
|
105
|
+
...
|
|
106
|
+
>>> Result.from_success("42").and_then(parse_number).map(lambda n: n * 2).unwrap()
|
|
107
|
+
84
|
|
108
|
+
>>> Result.from_success("invalid").and_then(parse_number).map(lambda n: n * 2).unwrap_or(0)
|
|
109
|
+
0
|
|
110
|
+
>>> Result.from_success("42").map(parse_number)
|
|
111
|
+
Success(value=Success(value=42, is_success=True), is_success=True)
|
|
112
|
+
>>> Result.from_success("42").and_then(parse_number)
|
|
113
|
+
Success(value=42, is_success=True)
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
`map` wraps a callback's value; `and_then` returns its Result directly.
|
|
117
|
+
Both skip callbacks on Failure and propagate callback exceptions.
|
|
118
|
+
`unwrap()` raises `UnwrapError` on Failure; `unwrap_or(default)` provides a fallback.
|
|
119
|
+
Use `is_success` or its read-only complement `is_error` for branch-specific
|
|
120
|
+
attributes. Compare explicitly with `True` or `False` for Pyright narrowing.
|
|
121
|
+
|
|
122
|
+
## Inspect the original error
|
|
123
|
+
|
|
124
|
+
```pycon
|
|
125
|
+
>>> original = ValueError("Invalid value")
|
|
126
|
+
>>> failure = Result.from_error(original)
|
|
127
|
+
>>> failure.error is original
|
|
128
|
+
True
|
|
129
|
+
>>> failure.error
|
|
130
|
+
ValueError('Invalid value')
|
|
131
|
+
>>> failure.error_message
|
|
132
|
+
'Invalid value'
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
`.error` retains the original exception or string; `.error_message` computes
|
|
136
|
+
`str(error)` for display. The exception is never modified or stripped of its
|
|
137
|
+
original traceback.
|
|
138
|
+
|
|
139
|
+
## Transform errors and recover
|
|
140
|
+
|
|
141
|
+
```pycon
|
|
142
|
+
>>> failure = Result.from_error("missing")
|
|
143
|
+
>>> failure.map_error(lambda error: ValueError(f"Invalid config: {error}"))
|
|
144
|
+
Failure(error=ValueError('Invalid config: missing'), traceback=None, is_success=False)
|
|
145
|
+
>>> failure.or_else(lambda error: Result.from_success("default config"))
|
|
146
|
+
Success(value='default config', is_success=True)
|
|
147
|
+
>>> failure.unwrap_error()
|
|
148
|
+
'missing'
|
|
149
|
+
>>> Result.from_success(42).error_or("no error")
|
|
150
|
+
'no error'
|
|
151
|
+
>>> failure.unwrap_or_else(lambda error: f"fallback for {error}")
|
|
152
|
+
'fallback for missing'
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
`map_error` stays Failure; `or_else` can recover to Success. `unwrap_error()`
|
|
156
|
+
raises `UnwrapSuccessError` on Success. `error_or(default)` extracts an error or
|
|
157
|
+
a default; `unwrap_or_else(f)` computes a fallback only on Failure.
|
|
158
|
+
Error callbacks receive the original error and propagate their exceptions.
|
|
159
|
+
|
|
160
|
+
## Capture and format a traceback
|
|
161
|
+
|
|
162
|
+
```pycon
|
|
163
|
+
>>> @as_result(with_traceback=True)
|
|
164
|
+
... def fail() -> None:
|
|
165
|
+
... raise ValueError("Operation failed")
|
|
166
|
+
...
|
|
167
|
+
>>> result = fail()
|
|
168
|
+
>>> if result.is_success is False:
|
|
169
|
+
... result.format_traceback(tail=1)
|
|
170
|
+
...
|
|
171
|
+
'...\nValueError: Operation failed'
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
`with_traceback` controls an additional `TracebackException` snapshot for formatting
|
|
175
|
+
with `head`, `tail`, and `separator`. The snapshot does not retain live frames or
|
|
176
|
+
local variables, but the original exception in `.error` can retain both through
|
|
177
|
+
its own `__traceback__`, even with `with_traceback=False`.
|
|
178
|
+
|
|
179
|
+
## Async functions
|
|
180
|
+
|
|
181
|
+
```pycon
|
|
182
|
+
>>> import asyncio
|
|
183
|
+
>>> from railtheway import as_async_result
|
|
184
|
+
>>> @as_async_result()
|
|
185
|
+
... async def load_value() -> int:
|
|
186
|
+
... await asyncio.sleep(0)
|
|
187
|
+
... return 42
|
|
188
|
+
...
|
|
189
|
+
>>> asyncio.run(load_value())
|
|
190
|
+
Success(value=42, is_success=True)
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
Use `as_result` for a synchronous call and `as_async_result` to await a returned
|
|
194
|
+
awaitable. Both accept `exceptions` and `with_traceback`.
|
|
195
|
+
|
|
196
|
+
**Migration:** replace `@as_result(...)` with `@as_async_result(...)` on coroutine
|
|
197
|
+
functions. `as_result` no longer detects or awaits async functions; a returned
|
|
198
|
+
coroutine is stored as the Success value. Async generators remain synchronous
|
|
199
|
+
calls returning an iterator and are not handled by `as_async_result`.
|
|
200
|
+
|
|
201
|
+
See the [examples guide](docs/examples.md) for all supported cases, expected
|
|
202
|
+
results, formatting options, and decorator limitations.
|
|
203
|
+
|
|
204
|
+
## Development
|
|
205
|
+
|
|
206
|
+
Pyright is a development-only dependency; installing the library does not
|
|
207
|
+
install it. Strict type checking covers the source and tests.
|
|
208
|
+
|
|
209
|
+
The unit test command also runs the interactive examples in this README and
|
|
210
|
+
the examples guide. CI checks Python 3.12, 3.13, and 3.14 on Linux, Windows,
|
|
211
|
+
and macOS, including an isolated wheel installation.
|
|
212
|
+
|
|
213
|
+
```powershell
|
|
214
|
+
uv run python -m unittest discover -s tests -v
|
|
215
|
+
uv run pyright
|
|
216
|
+
uv build # Wheel and source distribution in dist.
|
|
217
|
+
uv build --wheel # Pure Python wheel; Python 3.12+ required.
|
|
218
|
+
uv build --sdist # Source distribution.
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
## License and releases
|
|
222
|
+
|
|
223
|
+
- [MIT licensed](LICENSE), including commercial use subject to the license terms.
|
|
224
|
+
- Changes are recorded in the [changelog](CHANGELOG.md).
|
|
225
|
+
- During `0.x`, breaking API changes require a minor version increment and a
|
|
226
|
+
changelog entry; patch versions contain compatible fixes.
|
|
227
|
+
- From `1.0`, breaking changes require a major version increment.
|
|
228
|
+
- Python 3.12 is the minimum supported version. Additional Python versions and
|
|
229
|
+
operating systems must be tested before claiming verified support.
|
|
230
|
+
- Release artifacts must pass tests, strict typing, documentation examples, and
|
|
231
|
+
clean-environment wheel installation before publication.
|
|
232
|
+
- Public releases use Trusted Publishing rather than stored API tokens.
|
|
233
|
+
- The `Publish` GitHub Actions workflow runs when a matching version tag is
|
|
234
|
+
pushed (for example, `v0.2.0`). Configure a pending Trusted Publisher on PyPI
|
|
235
|
+
for `smocgeorgemarian/railtheway`, workflow `publish.yml`, and environment
|
|
236
|
+
`pypi` before pushing the first tag.
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
# railtheway
|
|
2
|
+
|
|
3
|
+
Typed success and failure results for Python 3.12+, with sync/async decorators
|
|
4
|
+
and optional structured tracebacks. No runtime dependencies.
|
|
5
|
+
|
|
6
|
+
## Setup
|
|
7
|
+
|
|
8
|
+
From the project directory:
|
|
9
|
+
|
|
10
|
+
```powershell
|
|
11
|
+
uv sync
|
|
12
|
+
uv run python
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
To install a built wheel into another environment:
|
|
16
|
+
|
|
17
|
+
```powershell
|
|
18
|
+
uv pip install --python .venv\Scripts\python.exe <path-to-wheel.whl>
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Quick start
|
|
22
|
+
|
|
23
|
+
```pycon
|
|
24
|
+
>>> from railtheway import as_result
|
|
25
|
+
>>> @as_result()
|
|
26
|
+
... def divide(a: float, b: float) -> float:
|
|
27
|
+
... return a / b
|
|
28
|
+
...
|
|
29
|
+
>>> divide(10, 2)
|
|
30
|
+
Success(value=5.0, is_success=True)
|
|
31
|
+
>>> divide(10, 0)
|
|
32
|
+
Failure(error=ZeroDivisionError('division by zero'), traceback=None, is_success=False)
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Capture only expected exceptions
|
|
36
|
+
|
|
37
|
+
```pycon
|
|
38
|
+
>>> @as_result(exceptions=(ValueError, OSError))
|
|
39
|
+
... def parse(text: str) -> int:
|
|
40
|
+
... return int(text)
|
|
41
|
+
...
|
|
42
|
+
>>> parse("invalid")
|
|
43
|
+
Failure(error=ValueError("invalid literal for int() with base 10: 'invalid'"), traceback=None, is_success=False)
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Only matching exception types and their subclasses become Failure; others
|
|
47
|
+
propagate. The default `(Exception,)` preserves existing behavior.
|
|
48
|
+
`exceptions=()` captures nothing. Interruptions and cancellation cannot be selected.
|
|
49
|
+
|
|
50
|
+
## Handle either outcome
|
|
51
|
+
|
|
52
|
+
```pycon
|
|
53
|
+
>>> from railtheway import Result, ResultValue
|
|
54
|
+
>>> def describe(result: ResultValue[int]) -> str:
|
|
55
|
+
... if result.is_success is True:
|
|
56
|
+
... return f"Value: {result.value}"
|
|
57
|
+
... return f"Error: {result.error_message}"
|
|
58
|
+
...
|
|
59
|
+
>>> describe(Result.from_success(42))
|
|
60
|
+
'Value: 42'
|
|
61
|
+
>>> describe(Result.from_error("Unavailable"))
|
|
62
|
+
'Error: Unavailable'
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
`ResultValue[T]` is `Success[T] | Failure`. Use `is True` or `is False` for
|
|
66
|
+
Pyright type narrowing; plain `if result.is_success:` works at runtime.
|
|
67
|
+
|
|
68
|
+
```pycon
|
|
69
|
+
>>> def describe_error_first(result: ResultValue[int]) -> str:
|
|
70
|
+
... if result.is_error is True:
|
|
71
|
+
... return f"Error: {result.error_message}"
|
|
72
|
+
... return f"Value: {result.value}"
|
|
73
|
+
...
|
|
74
|
+
>>> describe_error_first(Result.from_error("Unavailable"))
|
|
75
|
+
'Error: Unavailable'
|
|
76
|
+
>>> describe_error_first(Result.from_success(42))
|
|
77
|
+
'Value: 42'
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## Transform, chain, and extract
|
|
81
|
+
|
|
82
|
+
```pycon
|
|
83
|
+
>>> @as_result()
|
|
84
|
+
... def parse_number(text: str) -> int:
|
|
85
|
+
... return int(text)
|
|
86
|
+
...
|
|
87
|
+
>>> Result.from_success("42").and_then(parse_number).map(lambda n: n * 2).unwrap()
|
|
88
|
+
84
|
|
89
|
+
>>> Result.from_success("invalid").and_then(parse_number).map(lambda n: n * 2).unwrap_or(0)
|
|
90
|
+
0
|
|
91
|
+
>>> Result.from_success("42").map(parse_number)
|
|
92
|
+
Success(value=Success(value=42, is_success=True), is_success=True)
|
|
93
|
+
>>> Result.from_success("42").and_then(parse_number)
|
|
94
|
+
Success(value=42, is_success=True)
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
`map` wraps a callback's value; `and_then` returns its Result directly.
|
|
98
|
+
Both skip callbacks on Failure and propagate callback exceptions.
|
|
99
|
+
`unwrap()` raises `UnwrapError` on Failure; `unwrap_or(default)` provides a fallback.
|
|
100
|
+
Use `is_success` or its read-only complement `is_error` for branch-specific
|
|
101
|
+
attributes. Compare explicitly with `True` or `False` for Pyright narrowing.
|
|
102
|
+
|
|
103
|
+
## Inspect the original error
|
|
104
|
+
|
|
105
|
+
```pycon
|
|
106
|
+
>>> original = ValueError("Invalid value")
|
|
107
|
+
>>> failure = Result.from_error(original)
|
|
108
|
+
>>> failure.error is original
|
|
109
|
+
True
|
|
110
|
+
>>> failure.error
|
|
111
|
+
ValueError('Invalid value')
|
|
112
|
+
>>> failure.error_message
|
|
113
|
+
'Invalid value'
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
`.error` retains the original exception or string; `.error_message` computes
|
|
117
|
+
`str(error)` for display. The exception is never modified or stripped of its
|
|
118
|
+
original traceback.
|
|
119
|
+
|
|
120
|
+
## Transform errors and recover
|
|
121
|
+
|
|
122
|
+
```pycon
|
|
123
|
+
>>> failure = Result.from_error("missing")
|
|
124
|
+
>>> failure.map_error(lambda error: ValueError(f"Invalid config: {error}"))
|
|
125
|
+
Failure(error=ValueError('Invalid config: missing'), traceback=None, is_success=False)
|
|
126
|
+
>>> failure.or_else(lambda error: Result.from_success("default config"))
|
|
127
|
+
Success(value='default config', is_success=True)
|
|
128
|
+
>>> failure.unwrap_error()
|
|
129
|
+
'missing'
|
|
130
|
+
>>> Result.from_success(42).error_or("no error")
|
|
131
|
+
'no error'
|
|
132
|
+
>>> failure.unwrap_or_else(lambda error: f"fallback for {error}")
|
|
133
|
+
'fallback for missing'
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
`map_error` stays Failure; `or_else` can recover to Success. `unwrap_error()`
|
|
137
|
+
raises `UnwrapSuccessError` on Success. `error_or(default)` extracts an error or
|
|
138
|
+
a default; `unwrap_or_else(f)` computes a fallback only on Failure.
|
|
139
|
+
Error callbacks receive the original error and propagate their exceptions.
|
|
140
|
+
|
|
141
|
+
## Capture and format a traceback
|
|
142
|
+
|
|
143
|
+
```pycon
|
|
144
|
+
>>> @as_result(with_traceback=True)
|
|
145
|
+
... def fail() -> None:
|
|
146
|
+
... raise ValueError("Operation failed")
|
|
147
|
+
...
|
|
148
|
+
>>> result = fail()
|
|
149
|
+
>>> if result.is_success is False:
|
|
150
|
+
... result.format_traceback(tail=1)
|
|
151
|
+
...
|
|
152
|
+
'...\nValueError: Operation failed'
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
`with_traceback` controls an additional `TracebackException` snapshot for formatting
|
|
156
|
+
with `head`, `tail`, and `separator`. The snapshot does not retain live frames or
|
|
157
|
+
local variables, but the original exception in `.error` can retain both through
|
|
158
|
+
its own `__traceback__`, even with `with_traceback=False`.
|
|
159
|
+
|
|
160
|
+
## Async functions
|
|
161
|
+
|
|
162
|
+
```pycon
|
|
163
|
+
>>> import asyncio
|
|
164
|
+
>>> from railtheway import as_async_result
|
|
165
|
+
>>> @as_async_result()
|
|
166
|
+
... async def load_value() -> int:
|
|
167
|
+
... await asyncio.sleep(0)
|
|
168
|
+
... return 42
|
|
169
|
+
...
|
|
170
|
+
>>> asyncio.run(load_value())
|
|
171
|
+
Success(value=42, is_success=True)
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
Use `as_result` for a synchronous call and `as_async_result` to await a returned
|
|
175
|
+
awaitable. Both accept `exceptions` and `with_traceback`.
|
|
176
|
+
|
|
177
|
+
**Migration:** replace `@as_result(...)` with `@as_async_result(...)` on coroutine
|
|
178
|
+
functions. `as_result` no longer detects or awaits async functions; a returned
|
|
179
|
+
coroutine is stored as the Success value. Async generators remain synchronous
|
|
180
|
+
calls returning an iterator and are not handled by `as_async_result`.
|
|
181
|
+
|
|
182
|
+
See the [examples guide](docs/examples.md) for all supported cases, expected
|
|
183
|
+
results, formatting options, and decorator limitations.
|
|
184
|
+
|
|
185
|
+
## Development
|
|
186
|
+
|
|
187
|
+
Pyright is a development-only dependency; installing the library does not
|
|
188
|
+
install it. Strict type checking covers the source and tests.
|
|
189
|
+
|
|
190
|
+
The unit test command also runs the interactive examples in this README and
|
|
191
|
+
the examples guide. CI checks Python 3.12, 3.13, and 3.14 on Linux, Windows,
|
|
192
|
+
and macOS, including an isolated wheel installation.
|
|
193
|
+
|
|
194
|
+
```powershell
|
|
195
|
+
uv run python -m unittest discover -s tests -v
|
|
196
|
+
uv run pyright
|
|
197
|
+
uv build # Wheel and source distribution in dist.
|
|
198
|
+
uv build --wheel # Pure Python wheel; Python 3.12+ required.
|
|
199
|
+
uv build --sdist # Source distribution.
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
## License and releases
|
|
203
|
+
|
|
204
|
+
- [MIT licensed](LICENSE), including commercial use subject to the license terms.
|
|
205
|
+
- Changes are recorded in the [changelog](CHANGELOG.md).
|
|
206
|
+
- During `0.x`, breaking API changes require a minor version increment and a
|
|
207
|
+
changelog entry; patch versions contain compatible fixes.
|
|
208
|
+
- From `1.0`, breaking changes require a major version increment.
|
|
209
|
+
- Python 3.12 is the minimum supported version. Additional Python versions and
|
|
210
|
+
operating systems must be tested before claiming verified support.
|
|
211
|
+
- Release artifacts must pass tests, strict typing, documentation examples, and
|
|
212
|
+
clean-environment wheel installation before publication.
|
|
213
|
+
- Public releases use Trusted Publishing rather than stored API tokens.
|
|
214
|
+
- The `Publish` GitHub Actions workflow runs when a matching version tag is
|
|
215
|
+
pushed (for example, `v0.2.0`). Configure a pending Trusted Publisher on PyPI
|
|
216
|
+
for `smocgeorgemarian/railtheway`, workflow `publish.yml`, and environment
|
|
217
|
+
`pypi` before pushing the first tag.
|