Refactor asserts and todos#2313
Conversation
Updated README to improve formatting and structure.
Updated links, section headers, and example code paths in README.
@vikassaini77 mind check pls ^^ 🦝 |
There was a problem hiding this comment.
Pull request overview
This PR refactors multiple runtime validations by replacing assert statements with explicit TypeError/ValueError raises, updates README formatting/structure, and adds a basic allowlist check before executing subprocess commands in an example script.
Changes:
- Replaced
assert-based validations with explicit exceptions across image utilities, keypoint annotators, VLM parsing, NMS utilities, converters, and line-zone logic. - Updated
README.mdstructure (Table of Contents, heading capitalization, example placeholders, and minor HTML fixes). - Added a command allowlist guard before
subprocess.run()inexamples/time_in_zone/scripts/stream_from_file.py.
Holistic assessment (n/5):
- Code quality: 3/5
- Testing: 2/5
- Documentation: 2/5
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 9 comments.
Show a summary per file
| File | Description |
|---|---|
| src/supervision/utils/image.py | Replaces assert type/invariant checks with explicit exceptions in image helpers. |
| src/supervision/key_points/annotators.py | Converts assert scene type checks to TypeError in annotators. |
| src/supervision/detection/vlm.py | Replaces assert validations with exceptions in Florence-2 parsing. |
| src/supervision/detection/utils/iou_and_nms.py | Replaces assert validations with ValueError in IoU/NMS utilities. |
| src/supervision/detection/utils/converters.py | Replaces assert validations with ValueError in mask_to_rle. |
| src/supervision/detection/line_zone.py | Converts internal assert invariants into explicit ValueErrors. |
| src/supervision/detection/core.py | Replaces assert validations with exceptions in VLM dispatch + NMS/NMM paths. |
| README.md | Adds a Table of Contents and cleans up headings/examples/HTML. |
| examples/time_in_zone/scripts/stream_from_file.py | Adds an allowlist check before running subprocess commands. |
| if command[0] not in ["docker", "ffmpeg"]: | ||
| raise ValueError(f"Command {command[0]} not allowed") | ||
| process = subprocess.run(command) # noqa: S603 |
| ## 📑 Table of Contents | ||
|
|
||
| - [👋 Hello](#-hello) | ||
| - [💻 Install](#-install) | ||
| - [🔥 Quickstart](#-quickstart) | ||
| - [Models](#models) | ||
| - [Annotators](#annotators) | ||
| - [Datasets](#datasets) | ||
| - [🎬 Tutorials](#-tutorials) | ||
| - [💜 Built with Supervision](#-built-with-supervision) | ||
| - [📚 Documentation](#-documentation) | ||
| - [🏆 Contribution](#-contribution) |
| if not isinstance(result, str): | ||
| raise TypeError( | ||
| f"Expected string as <REGION_TO_CATEGORY> result, got {type(result)}" | ||
| ) |
| if not 0 <= iou_threshold <= 1: | ||
| raise ValueError( | ||
| "Value of `iou_threshold` must be in the closed range from 0 to 1, " | ||
| f"{iou_threshold} given." | ||
| ) |
| if not 0 <= iou_threshold <= 1: | ||
| raise ValueError( | ||
| "Value of `iou_threshold` must be in the closed range from 0 to 1, " | ||
| f"{iou_threshold} given." | ||
| ) |
| if self.confidence is None: | ||
| raise ValueError( | ||
| "Detections confidence must be given for NMS to be executed." | ||
| ) | ||
|
|
||
| if class_agnostic: | ||
| predictions = np.hstack((self.xyxy, self.confidence.reshape(-1, 1))) | ||
| else: | ||
| assert self.class_id is not None, ( | ||
| "Detections class_id must be given for NMS to be executed. If you" | ||
| " intended to perform class agnostic NMS set class_agnostic=True." | ||
| ) | ||
| if self.class_id is None: | ||
| raise ValueError( | ||
| "Detections class_id must be given for NMS to be executed. If you" | ||
| " intended to perform class agnostic NMS set class_agnostic=True." | ||
| ) |
| if self.confidence is None: | ||
| raise ValueError( | ||
| "Detections confidence must be given for NMM to be executed." | ||
| ) | ||
|
|
||
| if class_agnostic: | ||
| predictions = np.hstack((self.xyxy, self.confidence.reshape(-1, 1))) | ||
| else: | ||
| assert self.class_id is not None, ( | ||
| "Detections class_id must be given for NMM to be executed. If you" | ||
| " intended to perform class agnostic NMM set class_agnostic=True." | ||
| ) | ||
| if self.class_id is None: | ||
| raise ValueError( | ||
| "Detections class_id must be given for NMM to be executed. If you" | ||
| " intended to perform class agnostic NMM set class_agnostic=True." | ||
| ) |
| ... ) | ||
| >>> keep | ||
| array([ True, False]) | ||
| """ | ||
| assert 0 <= iou_threshold <= 1, ( | ||
| "Value of `iou_threshold` must be in the closed range from 0 to 1, " | ||
| f"{iou_threshold} given." | ||
| ) | ||
| if not 0 <= iou_threshold <= 1: |
| if mask.ndim != 2: | ||
| raise ValueError("Input mask must be 2D") | ||
| if mask.size == 0: | ||
| raise ValueError("Input mask cannot be empty") |
|
@vikassaini77 pls follow the contribution guidelines. In particular, it means you shall write PR description why it does and how it fixes or improves this package, then you have to sign the CLA (or provide a screenshot of what you did), otherwise, we cannot move forward, even I see based on your changes a value in your contribution 🦝 |
Before submitting
Description
Type of Change
Motivation and Context
Closes #(issue)
Changes Made
Testing
Google Colab (optional)
Colab link:
Screenshots/Videos (optional)
Additional Notes