Skip to content

Quality: assert used for input validation instead of proper error handling#1276

Open
kumburovicbranko682-boop wants to merge 1 commit into
nv-legate:mainfrom
kumburovicbranko682-boop:contribai/improve/quality/assert-used-for-input-validation-instead
Open

Quality: assert used for input validation instead of proper error handling#1276
kumburovicbranko682-boop wants to merge 1 commit into
nv-legate:mainfrom
kumburovicbranko682-boop:contribai/improve/quality/assert-used-for-input-validation-instead

Conversation

@kumburovicbranko682-boop

Copy link
Copy Markdown

✨ Code Quality

Problem

The assert unary_red_code in _EQUIVALENT_NON_NAN_OPS on line 42 is used to validate a function argument, not an internal invariant. When Python runs with the -O (optimize) flag, assert statements are stripped entirely. If a caller passes an invalid unary_red_code, the function will silently fall through to _EQUIVALENT_NON_NAN_OPS[unary_red_code] and raise a bare KeyError with no context about what went wrong or which inputs were invalid. For library code, argument validation should use explicit if/raise so it is never stripped by the interpreter. This also contradicts the project's own style guide ("Errors: try/except with raise").

Severity: medium
File: cupynumeric/_module/_unary_red_utils.py

Solution

Replace the assert with a proper guard:

Changes

  • cupynumeric/_module/_unary_red_utils.py (modified)

Testing

  • Existing tests pass
  • Manual review completed
  • No new warnings/errors introduced


🤖 About this PR

This pull request was generated by ContribAI, an AI agent
that helps improve open source projects. The change was:

  1. Discovered by automated code analysis
  2. Generated by AI with context-aware code generation
  3. Self-reviewed by AI quality checks

If you have questions or feedback about this PR, please comment below.
We appreciate your time reviewing this contribution!

Closes #1275

…ndling

The `assert unary_red_code in _EQUIVALENT_NON_NAN_OPS` on line 42 is used to validate a function argument, not an internal invariant. When Python runs with the `-O` (optimize) flag, `assert` statements are stripped entirely. If a caller passes an invalid `unary_red_code`, the function will silently fall through to `_EQUIVALENT_NON_NAN_OPS[unary_red_code]` and raise a bare `KeyError` with no context about what went wrong or which inputs were invalid. For library code, argument validation should use explicit if/raise so it is never stripped by the interpreter. This also contradicts the project's own style guide ("Errors: try/except with raise").


Affected files: _unary_red_utils.py

Signed-off-by: kumburovicbranko682-boop <295886834+kumburovicbranko682-boop@users.noreply.github.com>
@copy-pr-bot

copy-pr-bot Bot commented Jun 28, 2026

Copy link
Copy Markdown
Contributor

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: assert used for input validation instead of proper error handling

1 participant