Skip to content

wait_chain: reject empty strategy list with ValueError instead of IndexError#647

Merged
mergify[bot] merged 1 commit into
jd:mainfrom
HrachShah:fix/wait-chain-empty-strategies-raises
Jul 3, 2026
Merged

wait_chain: reject empty strategy list with ValueError instead of IndexError#647
mergify[bot] merged 1 commit into
jd:mainfrom
HrachShah:fix/wait-chain-empty-strategies-raises

Conversation

@HrachShah

Copy link
Copy Markdown
Contributor

Summary

wait_chain() accepts a variadic list of wait strategies and stores them in self.strategies. When called with no strategies, self.strategies ends up as (). The first call to __call__ then evaluates min(max(1, 1), 0) == 0, so self.strategies[wait_func_no - 1] turns into self.strategies[-1] and raises an IndexError from inside a running retry.

This is opaque for what is really a programmer error at construction time. An empty wait_chain has no useful behaviour, so failing early is friendlier than letting it crash later from a min(max(1, 1), 0) corner case.

Fix

tenacity/wait.py: validate in __init__ and raise ValueError('wait_chain() requires at least one strategy') so the failure happens at the point where the bad configuration is created, not later when the wait function is first evaluated.

Tests

tests/test_tenacity.py:

  • test_wait_chain_requires_at_least_one_strategy — asserts ValueError from the bare wait_chain() constructor and from the wrapped Retrying(wait=wait_chain()) path that previously surfaced the IndexError.

The change is one validation line plus the test. A release note is included in releasenotes/notes/.

…exError

Calling wait_chain() with no strategies stored an empty tuple in
self.strategies. The first call to __call__ then evaluated
min(max(1, 1), 0) == 0, so self.strategies[wait_func_no - 1]
turned into self.strategies[-1] and raised IndexError, which is
opaque for what is really a programmer error at construction time.

Validate in __init__ and raise ValueError('wait_chain() requires
at least one strategy') so the failure happens at the point where
the bad configuration is created, not later when the wait function
is first evaluated inside a running retry.
@mergify

mergify Bot commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Merge Queue Status

This pull request spent 16 seconds in the queue, including 2 seconds running CI.

Required conditions to merge

mergify Bot added a commit that referenced this pull request Jul 3, 2026
@mergify
mergify Bot merged commit b2cd027 into jd:main Jul 3, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants