Skip to content

fix(key_points): validate data length in KeyPoints.__setitem__ - #2974

Merged
Borda merged 7 commits into
roboflow:developfrom
avanish-garg:fix/keypoints-setitem-length
Nov 24, 2026
Merged

Borda merged 7 commits into
roboflow:developfrom
avanish-garg:fix/keypoints-setitem-length

Conversation

@avanish-garg

Copy link
Copy Markdown
Contributor

Description

KeyPoints.__setitem__ now validates the length of the assigned value against len(key_points), like Detections.__setitem__, and raises ValueError at assignment time.

Motivation and Context

KeyPoints.__setitem__ stored any list or array regardless of length, while Detections.__setitem__ raises ValueError ("Shape of np.ndarray for key 'name' must be (N,)"). A wrong-length value then failed later and far from the assignment: IndexError in as_detections(), ValueError in KeyPoints.merge, IndexError when selecting. A too-short value could also silently mis-select.

kp = sv.KeyPoints(xy=xy, class_id=np.array([0, 1]))
kp["name"] = ["a", "b", "c"]  # accepted on develop; kp.as_detections() -> IndexError

Closes #2973

Changes Made

  • src/supervision/key_points/core.py: call the existing _validate_data({key: value}, len(self)) (the helper KeyPoints.__init__ and Detections.__setitem__ already use) before storing, and add a Raises section to the docstring. The TypeError for non-list, non-ndarray values is unchanged. Valid input is unchanged, including per-keypoint arrays of shape (N, K, ...).
  • tests/key_points/test_core.py: new TestKeyPointsSetItem (16 cases): wrong-length lists and arrays, a failed assignment leaves the data untouched, valid list / 1-d / (N, K) / (N, K, 3) values still work, scalar / str / tuple still raise TypeError, an empty KeyPoints.
  • Changelog entry under Unreleased.

Testing

  • pytest tests/key_points -q

    • on develop (the fix reverted): 8 failed, 460 passed, 1 skipped (DID NOT RAISE ValueError)
    • with this change: all pass
  • pytest tests/key_points tests/detection -q: 3125 passed, 3 skipped

  • pytest --doctest-modules src/supervision/key_points/core.py: 10 passed

  • ruff check, ruff format, docformatter and mypy on the changed files: clean.

  • Added/updated tests. tests/key_points and tests/detection pass locally; the full suite was not run because tests/metrics needs optional extras (faster_coco_eval/pycocotools) that are not installed here.

  • Updated docs (docstrings / mkdocs entry) for new or changed public API (Raises added to the __setitem__ docstring)

  • Added a changelog entry in docs/changelog.md under Unreleased

Additional Notes

  • Error messages are identical to Detections.__setitem__ because both use the same helper. A 0-dim array is not rejected by the helper, which matches Detections; I did not tighten that.
  • Does not touch __getitem__ / select. feat(dataset): add KeyPointsDataset with YOLO pose support #2745 concerns per-keypoint data slicing and touches the same files in different hunks; only the changelog line at the top of Unreleased may need a trivial rebase.

KeyPoints.__setitem__ stored any list or array regardless of its length,
so a wrong-length value only failed later, far from the assignment, as an
IndexError in as_detections() or when selecting, or a ValueError in merge.
Run the same _validate_data check that KeyPoints.__init__ and
Detections.__setitem__ already use, so the error is raised at assignment
time and the stored data is left untouched.
@codecov

codecov Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95%. Comparing base (06ac43f) to head (6d0d389).

Additional details and impacted files
@@           Coverage Diff           @@
##           develop   #2974   +/-   ##
=======================================
  Coverage       95%     95%           
=======================================
  Files           84      84           
  Lines        15606   15607    +1     
=======================================
+ Hits         14895   14897    +2     
+ Misses         711     710    -1     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Borda
Borda requested a balanced review from Copilot November 13, 2026 09:35
@Borda Borda added the bug Something isn't working label Nov 13, 2026
@Borda Borda changed the title fix(key_points): validate data length in KeyPoints.__setitem__ fix(key_points): validate data length in KeyPoints.__setitem__ Nov 13, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are reported for the change to reject mismatched KeyPoints metadata at assignment time.

No open issues 🎉

What changed in this PR

This PR addresses #2973 by aligning KeyPoints.__setitem__ with Detections.__setitem__: metadata with the wrong number of rows is rejected at assignment time rather than failing during later operations.

Changes:

  • Reuses shared validation before storing metadata, preserving valid multidimensional inputs.
  • Adds regression tests for length mismatches, unchanged data after rejection, supported types, and empty objects.
  • Documents the exceptions and behavior change.
File Description
tests/​key_points/​test_core.py Adds focused assignment-validation tests.
src/​supervision/​key_points/​core.py Validates metadata before storage and documents exceptions.
docs/​changelog.md Records the fix under Unreleased.

Review effort: Balanced

…titem-length

# Conflicts:
#	docs/changelog.md
#	src/supervision/key_points/core.py
Borda and others added 2 commits November 24, 2026 11:57
Changes:
- Integrate the current develop changes, including the 0.32.1 release, BGRA rendering, metrics, dataset, model, image and dependency updates.
- Preserve the KeyPoints assignment validation, tests and unreleased changelog entry.

Impact:
- Keeps the PR compatible with the current target while retaining its early metadata length validation.

Verification:
- Keypoint core tests: 358 passed, 6 skipped on the NumPy fallback backend.
- Reviewed the merge index against the target and checked staged whitespace.

Residual limits:
- The complete test suite was not run for this integration.
- Six optional tensor tests were skipped in the available environment.

---

Co-authored-by: Codex <[email protected]>
Changes:
- Document zero-dimensional metadata accepted by KeyPoints and its object-selection, merge and keypoint-aligned-field limitations.

Impact:
- Callers can distinguish shared scalar metadata from per-object and per-keypoint arrays without changing runtime behavior.

Verification:
- Full local test suite: 7,785 passed and 298 skipped.
- Pinned Ruff, format and mypy checks and git diff --check passed.
- Confirmed scalar selection and merge behavior and completed an independent source challenge.

Residual limits:
- The current Lychee link check has not run; the CLI is absent locally.
- The local suite skipped 298 optional or platform-dependent tests.

---

Co-authored-by: Codex <[email protected]>
@Borda
Borda merged commit 1e2ac13 into roboflow:develop Nov 24, 2026
36 of 37 checks passed
@Borda Borda mentioned this pull request Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

3 participants