Files
Kadir Can Ozden 0f74af5c33 Fix WeakSequence.__getitem__ catching KeyError instead of IndexError
### Description

`WeakSequence.__getitem__` catches `KeyError` but the internal `_storage` is a `list`, which raises `IndexError` for out-of-range access. This means the `except KeyError` handler never executes, and the custom error message is never shown.

### Current behavior

```python
def __getitem__(self, index):
    try:
        obj = self._storage[index]  # _storage is a list
    except KeyError:  # lists don't raise KeyError
        raise IndexError("Index %s out of range" % index)
    else:
        return obj()
```

On an out-of-range index, the raw `IndexError` from list access propagates directly (e.g., `list index out of range`) instead of the intended custom message.

### Fix

Changed `except KeyError` to `except IndexError` so the handler actually catches the exception raised by list indexing.

Closes: #13136
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/13136
Pull-request-sha: ceb8f9fe46

Change-Id: Ia36c2b9b0f3aec97624bcd2a9e49f43b9ed786d9
2026-02-23 22:12:46 +01:00
..
2023-12-20 22:54:28 +01:00
2025-03-05 16:03:16 -05:00
2024-06-11 09:16:26 -04:00