Skip to content

Commit 9a015ef

Browse files
committed
Expose the number of the current attempt
1 parent 16ea4b3 commit 9a015ef

File tree

5 files changed

+16
-2
lines changed

5 files changed

+16
-2
lines changed

docs/api.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ API Reference
66
.. autofunction:: retry
77
.. autofunction:: retry_context
88
.. autoclass:: Attempt
9+
:members: num
910

1011

1112
Configuration

src/stamina/_core.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ class Attempt:
8181

8282
_t_attempt: _t.AttemptManager
8383

84+
@property
85+
def num(self) -> int:
86+
"""
87+
The number of the current attempt.
88+
"""
89+
return self._t_attempt.retry_state.attempt_number # type: ignore[no-any-return]
90+
8491
def __enter__(self) -> None:
8592
return self._t_attempt.__enter__() # type: ignore[no-any-return]
8693

tests/test_async.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@ async def test_retry_block():
151151
async for attempt in stamina.retry_context(on=ValueError, wait_max=0):
152152
with attempt:
153153
num_called += 1
154+
155+
assert num_called == attempt.num
156+
154157
if num_called < 2:
155158
raise ValueError
156159

tests/test_sync.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ def test_retry_block():
124124
for attempt in stamina.retry_context(on=ValueError, wait_max=0):
125125
with attempt:
126126
i += 1
127+
128+
assert i == attempt.num
129+
127130
if i < 2:
128131
raise ValueError
129132

tests/typing/api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def hook(
9898

9999
for attempt in retry_context(on=ValueError, timeout=13):
100100
with attempt:
101-
...
101+
x: int = attempt.num
102102

103103
for attempt in retry_context(
104104
on=ValueError, timeout=dt.timedelta(seconds=13.0)
@@ -116,4 +116,4 @@ async def f() -> None:
116116
wait_jitter=one_sec,
117117
):
118118
with attempt:
119-
...
119+
pass

0 commit comments

Comments
 (0)