Repository navigation
Port set_branch's validation from MATLAB — the last unvalidated branch stub #52
Description
Database.set_branch is the last of the branch methods still carrying a # Validation logic would go here placeholder. add_branch and delete_branch were ported in #51; this one was deliberately left, because closing it has a consequence that needed a decision first. That decision has been made: Python should match MATLAB.
What MATLAB does
function set_branch(database_obj, branch_id)
branch_id = database_obj.validate_branch_id(branch_id); % non-empty string, must exist
database_obj.current_branch_id = branch_id;Python's accepts anything and assigns it. The mistake surfaces later, at the next add_docs (Branch '<id>' does not exist.) — fail-deferred rather than fail-silent, but the two languages report it at different points.
The consequence, which is the whole reason this was held back
Since #51, add_branch reads an empty parent as "the current branch", matching MATLAB. A branch with no parent — a root — is therefore only creatable when there is no current branch. Python can arrange that with set_branch(""); MATLAB cannot, because its set_branch rejects an empty id.
So porting this validation removes multi-root creation from Python, matching a MATLAB limitation rather than a MATLAB feature. Three callers rely on it today:
tests/helpers.pyadd_branch_nodes— builds the multi-root foreststest_branchuses (make_tree(4, …), four roots)tests/test_add_docs_atomicity.py— needs a branch that does not inherit the document, so the following add is a genuine cross-branch add rather than a duplicatetests/test_add_docs_file_handler.py— same reason
None of these wants a stub; each wants "a branch with no parent", and set_branch("") is merely the only way to say it right now.
So this issue has a prerequisite
VH-Lab/DID-matlab#165 covers the same gap from the MATLAB side — its own add_branch_nodes helper silently misplaces the second and later roots of a multi-root forest, and it cannot be fixed there without deciding how a root gets created. The options listed there are:
- Let
set_branch('')mean "no current branch", in both languages. Python's tests keep working unchanged; MATLAB gains the ability and can fix its helper. - Give
add_branchan explicit way to request a root (a sentinel, or anis_rootflag), and keepset_branchstrict in both. - Declare multi-root databases unsupported, and change these three Python callers to build single-root fixtures.
Under option 1 this issue is a small strict-validation change plus nothing else. Under option 2 it is that plus a new add_branch argument in both languages. Under option 3 it is that plus rewriting the three callers and test_branch's fixture.
The schema allows multi-root databases either way — branches.parent_id is nullable and _do_add_branch writes NULL for an empty parent — so this is a decision about the API, not a constraint from storage.
Scope when it lands
Database.set_branchvalidates via_validate_branch_id(added in Give add_branch and delete_branch the guards MATLAB has #51; it already does the non-empty-string and existence checks).ValueError, as everywhere else here, where MATLAB raisesDID:Database:InvalidBranch.- Update the
set_branchentry indid_matlab_python_bridge.yaml, which currently records exactly why it was left open, and the delete_branch's guards section ofPORTING_INSTRUCTIONS.md. - Whatever the chosen option requires of the three callers above.
Activity