# StopIteration: Caught StopIteration from side_effect; consider using side_effect with an iterable

- **ID:** `python/unittest-mock-side-effect-iteration`
- **Domain:** python
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

Setting mock.side_effect to an iterable (e.g., a generator) that raises StopIteration when exhausted, and the test calls the mock more times than the number of items in the iterable.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 3.9 | active | — | — |

## Workarounds

1. **** (85% success)
   ```
   Ensure the number of mock calls matches the length of the side_effect iterable, or use side_effect with a function that handles arbitrary calls.
   ```
2. **** (80% success)
   ```
   Set side_effect to a list with enough items to cover all expected calls, and use a sentinel value for extra calls if needed.
   ```

## Dead Ends

- **** — Using a list instead of a generator for side_effect still raises StopIteration if the list is exhausted, but the error may be more predictable. (50% fail)
- **** — Setting side_effect to a function that returns a default value after exhaustion may mask the fact that the mock is called too many times. (40% fail)
