Skip to content

Commit

Permalink
Expose the number of the current attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
hynek committed Jul 9, 2023
1 parent fc52447 commit 3c20fc4
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ API Reference
.. autofunction:: retry
.. autofunction:: retry_context
.. autoclass:: Attempt
:members: num


Configuration
Expand Down
7 changes: 7 additions & 0 deletions src/stamina/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ class Attempt:

_t_attempt: _t.AttemptManager

@property
def num(self):
"""
The number of the current attempt.
"""
return self._t_attempt.retry_state.attempt_number

def __enter__(self) -> None:
return self._t_attempt.__enter__() # type: ignore[no-any-return]

Expand Down
3 changes: 3 additions & 0 deletions tests/test_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ async def test_retry_block():
async for attempt in stamina.retry_context(on=ValueError, wait_max=0):
with attempt:
num_called += 1

assert num_called == attempt.num

if num_called < 2:
raise ValueError

Expand Down
3 changes: 3 additions & 0 deletions tests/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ def test_retry_block():
for attempt in stamina.retry_context(on=ValueError, wait_max=0):
with attempt:
i += 1

assert i == attempt.num

if i < 2:
raise ValueError

Expand Down

0 comments on commit 3c20fc4

Please sign in to comment.