Repository navigation
fix(key_points): validate data length in KeyPoints.__setitem__ - #2974
Merged
Borda merged 7 commits intoNov 24, 2026
Merged
Conversation
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.
# Conflicts: # docs/changelog.md
Codecov Report✅ All modified and coverable lines are covered by tests. 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:
|
KeyPoints.__setitem__
Contributor
There was a problem hiding this comment.
🟢 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
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
approved these changes
Nov 24, 2026
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
KeyPoints.__setitem__now validates the length of the assigned value againstlen(key_points), likeDetections.__setitem__, and raisesValueErrorat assignment time.Motivation and Context
KeyPoints.__setitem__stored any list or array regardless of length, whileDetections.__setitem__raisesValueError("Shape of np.ndarray for key 'name' must be (N,)"). A wrong-length value then failed later and far from the assignment:IndexErrorinas_detections(),ValueErrorinKeyPoints.merge,IndexErrorwhen selecting. A too-short value could also silently mis-select.Closes #2973
Changes Made
src/supervision/key_points/core.py: call the existing_validate_data({key: value}, len(self))(the helperKeyPoints.__init__andDetections.__setitem__already use) before storing, and add aRaisessection to the docstring. TheTypeErrorfor 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: newTestKeyPointsSetItem(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/tuplestill raiseTypeError, an emptyKeyPoints.Testing
pytest tests/key_points -qdevelop(the fix reverted): 8 failed, 460 passed, 1 skipped (DID NOT RAISE ValueError)pytest tests/key_points tests/detection -q: 3125 passed, 3 skippedpytest --doctest-modules src/supervision/key_points/core.py: 10 passedruff check, ruff format, docformatter and mypy on the changed files: clean.
Added/updated tests.
tests/key_pointsandtests/detectionpass locally; the full suite was not run becausetests/metricsneeds optional extras (faster_coco_eval/pycocotools) that are not installed here.Updated docs (docstrings / mkdocs entry) for new or changed public API (
Raisesadded to the__setitem__docstring)Added a changelog entry in
docs/changelog.mdunderUnreleasedAdditional Notes
Detections.__setitem__because both use the same helper. A 0-dim array is not rejected by the helper, which matchesDetections; I did not tighten that.__getitem__/select. feat(dataset): addKeyPointsDatasetwith YOLO pose support #2745 concerns per-keypointdataslicing and touches the same files in different hunks; only the changelog line at the top of Unreleased may need a trivial rebase.