Skip to content

refactor: Rewrite assert statements as exceptions#1931

Open
rapsealk wants to merge 1 commit intobitsandbytes-foundation:mainfrom
rapsealk:rewrite-asserts-as-exceptions
Open

refactor: Rewrite assert statements as exceptions#1931
rapsealk wants to merge 1 commit intobitsandbytes-foundation:mainfrom
rapsealk:rewrite-asserts-as-exceptions

Conversation

@rapsealk
Copy link
Copy Markdown
Contributor

@rapsealk rapsealk commented Apr 23, 2026

Summary

Mechanical sweep of assert statements in library code, rewriting them as if not cond: raise AssertionError[(msg)] so the checks still fire under python -O (which strips assert). Picks up the task described in #1408.

  • Scope: 37 sites across bitsandbytes/ (36) + check_bnb_install.py (1).
  • Out of scope: tests/ (pytest relies on assert for introspection) and examples/ (didactic code).
  • Approach: mechanical rewrite only — no semantic exception upgrades (ValueError/TypeError/etc.), since the issue is labeled Low Risk and per-site judgment would enlarge review surface. Happy to follow up with semantic upgrades in a separate PR if maintainers prefer.

Audits run before the rewrite

  • Tuple-trap audit (assert (cond, msg) — always-truthy bug): none found.
  • Side-effect audit (assert fn(...) patterns that would be skipped under -O): all hits were pure queries (isinstance, len, .numel(), set().issubset, P.is_parametrized) — no state-mutating calls, so no behavior regression from "now runs under -O".

Test plan

  • grep -rnE "^\s*assert\b" bitsandbytes/ check_bnb_install.py returns empty (0 asserts remain in scope)
  • python3 -m py_compile on all 9 touched files
  • ruff check bitsandbytes/ check_bnb_install.py — all checks passed
  • ruff format --check bitsandbytes/ check_bnb_install.py — all files already formatted
  • pre-commit run --files ... with the repo's pinned ruff 0.14.3 — all hooks pass (ruff, ruff-format, EOF, trailing whitespace, line endings, typos)
  • python -c "import bitsandbytes" — not run locally (no torch on my machine). CI tests-pr.yml covers this.
  • pytest --collect-only tests/ — same; relying on CI.

Closes #1408.


Assisted by Claude Code.

Mechanically rewrite `assert cond[, msg]` in library code as
`if not cond: raise AssertionError[(msg)]` so the checks still fire
under `python -O`, which strips asserts. Test files are left alone
since pytest relies on `assert` for introspection.

Closes bitsandbytes-foundation#1408.

Assisted-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@rapsealk rapsealk changed the title Rewrite asserts as exceptions refactor: Rewrite asserts as exceptions Apr 23, 2026
@rapsealk rapsealk changed the title refactor: Rewrite asserts as exceptions refactor: Rewrite assert statements as exceptions Apr 23, 2026
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.

Rewrite asserts as exceptions

1 participant