supervision icon indicating copy to clipboard operation
supervision copied to clipboard

[Bug]: KeyPoints.__setitem__ accepts data of the wrong length, Detections.__setitem__ raises ValueError

Open avanish-garg opened this issue 3 weeks ago • 0 comments

Search before asking

  • [x] I have searched the issues and discussions and found no similar bug report.

Bug

What's the bug? KeyPoints.__setitem__ stores any list or array regardless of its length. Detections.__setitem__ validates it and raises ValueError (it has a test and a docstring Raises section). The wrong-length data then fails later, far from the assignment: IndexError in as_detections(), ValueError in KeyPoints.merge, IndexError on selection, and a too-short value can silently mis-select.

Expected behavior: kp["name"] = ["a", "b", "c"] on a KeyPoints with 2 skeletons raises ValueError at assignment time, like Detections does.

Actual behavior:

Detections  : ValueError: Shape of np.ndarray for key 'name' must be (2,)
KeyPoints   : stored data = ['a' 'b' 'c'] (len 3 for 2 skeletons)
kp[[1]]['name']       = ['b']  <- fine by accident
kp[[0]]['name']       = ['a']
kp[bool mask]         = ['a']
KeyPoints.merge       = ValueError: Shape of np.ndarray for key 'name' must be (2,)
kp.as_detections()    = IndexError: boolean index did not match indexed array along axis 0; size of axis is 3 but size of corresponding boolean axis is 2
short value stored: ['only-one'] len 1 vs 2
kp2[[1]]['name'] = IndexError index 1 is out of bounds for axis 0 with size 1

### Environment

- Supervision: 0.32.0.dev (`develop` at 5210466)
- Python: 3.12.14
- OS: macOS (Apple Silicon)

### Minimal Reproducible Example

```python
import numpy as np
import supervision as sv

xy = np.array([[[10, 10], [20, 20]], [[30, 30], [40, 40]]], dtype=np.float32)

det = sv.Detections(xyxy=np.array([[0, 0, 1, 1], [2, 2, 3, 3]], dtype=np.float32))
try:
    det["name"] = ["a", "b", "c"]
except ValueError as e:
    print("Detections:", e)  # raises at assignment

kp = sv.KeyPoints(xy=xy, class_id=np.array([0, 1]))
kp["name"] = ["a", "b", "c"]  # accepted: 3 names for 2 skeletons
kp.as_detections()  # IndexError far from the assignment

Are you willing to submit a PR?

  • [x] Yes I'd like to help by submitting a PR!

avanish-garg avatar Nov 13 '26 06:11 avanish-garg