refactor: Rewrite assert statements as exceptions#1931
Open
rapsealk wants to merge 1 commit intobitsandbytes-foundation:mainfrom
Open
refactor: Rewrite assert statements as exceptions#1931rapsealk wants to merge 1 commit intobitsandbytes-foundation:mainfrom
assert statements as exceptions#1931rapsealk wants to merge 1 commit intobitsandbytes-foundation:mainfrom
Conversation
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>
asserts as exceptionsasserts as exceptions
asserts as exceptionsassert statements as exceptions
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.
Summary
Mechanical sweep of
assertstatements in library code, rewriting them asif not cond: raise AssertionError[(msg)]so the checks still fire underpython -O(which stripsassert). Picks up the task described in #1408.bitsandbytes/(36) +check_bnb_install.py(1).tests/(pytest relies onassertfor introspection) andexamples/(didactic code).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
assert (cond, msg)— always-truthy bug): none found.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.pyreturns empty (0 asserts remain in scope)python3 -m py_compileon all 9 touched filesruff check bitsandbytes/ check_bnb_install.py— all checks passedruff format --check bitsandbytes/ check_bnb_install.py— all files already formattedpre-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 (notorchon my machine). CItests-pr.ymlcovers this.pytest --collect-only tests/— same; relying on CI.Closes #1408.
Assisted by Claude Code.