Skip to content

docs: rules for using assert, test the -O guard - #10521

Merged
ThomasWaldmann merged 1 commit into
borgbackup:masterfrom
ThomasWaldmann:assert-rules-8649
Oct 12, 2026
Merged

ThomasWaldmann merged 1 commit into
borgbackup:masterfrom
ThomasWaldmann:assert-rules-8649

Conversation

@ThomasWaldmann

Copy link
Copy Markdown
Member

Refs #8649.

The assert policy from #8649 only existed in the issue body, so asserts get converted one module at a time without a written criterion. This adds the rules to the development docs and a regression test for the startup guard (noted as missing in #10019).

Docs: new "Using assert" section in docs/development.rst

  • assert only for conditions that can be false only through a borg bug: internal invariants, pre-/postconditions between borg's own functions, argument types from borg's own callers, unreachable branches.
  • Everything that can be false without a borg bug uses if ...: raise ...: repository/store objects (authenticated does not mean well-formed), local cache/config/key files, legacy borg 1.x data, remote responses, user input, OS/filesystem results. Prefer borg's exception classes (IntegrityError, Error subclasses with an exit code); ValueError/TypeError only where callers catch them.
  • Validate at the boundary where the data enters; deeper helpers may keep asserts as invariants.
  • Never use assert for security decisions, and no side effects inside an assert expression.
  • borg refuses to start with assertions disabled; the guard at the top of src/borg/archiver/__init__.py only covers code paths that import borg.archiver.
  • The test suite uses plain pytest assert.

Test

test_refuses_to_run_without_assertions in src/borg/testsuite/archiver/version_cmd_test.py runs python -O -m borg --version and PYTHONOPTIMIZE=1 python -m borg --version and checks for rc 2, the "Borg requires working assertions" message on stderr and empty stdout.

🤖 Generated with Claude Code

…8649

Add a "Using assert" section to the development docs: assert is only for
conditions that can be false only due to a borg bug; everything coming from
outside (repo objects, local files, legacy data, remote responses, user
input, OS results) needs a real check that raises a borg exception, done at
the boundary where the data enters.

Add a regression test that borg refuses to run with assertions disabled
(python -O and PYTHONOPTIMIZE=1).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Oct 12, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.32%. Comparing base (2155628) to head (0638531).
⚠️ Report is 1 commits behind head on master.
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #10521      +/-   ##
==========================================
- Coverage   89.34%   89.32%   -0.02%     
==========================================
  Files         104      104              
  Lines       20079    20079              
  Branches     3152     3152              
==========================================
- Hits        17940    17936       -4     
- Misses       1481     1484       +3     
- Partials      658      659       +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@ThomasWaldmann
ThomasWaldmann merged commit 6d8bab7 into borgbackup:master Oct 12, 2026
25 of 26 checks passed
@ThomasWaldmann
ThomasWaldmann deleted the assert-rules-8649 branch October 12, 2026 23:21
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.

1 participant