Skip to content

Python wrapper: send request options from Branch.create, Branch.commit and merge_into - #10622

Merged
AliRamberg merged 3 commits into
masterfrom
issue-1417-python-hl-sdk-branch-create
Nov 30, 2026
Merged

AliRamberg merged 3 commits into
masterfrom
issue-1417-python-hl-sdk-branch-create

Conversation

@AliRamberg

Copy link
Copy Markdown
Contributor

Closes treeverse/product#1417

Change Description

Bug Fix

  1. Problem: Branch.create, Branch.commit and Reference.merge_into accept **kwargs, but request options such as _request_timeout passed to them have no effect and raise no error. A caller trying to bound how long branch creation may take gets a request with no timeout.
  2. Root cause: these three methods pass **kwargs only to the request body model (BranchCreation, CommitCreation, Merge), which ignores keys it does not define. The other high-level methods pass **kwargs to the API call instead.
  3. Solution: keyword arguments named by a field of the body model (by name or alias, e.g. hidden, allow_empty, date) still go into the body; every other keyword argument goes to the API call.
 branch.create("main", hidden=True, _request_timeout=5)
   BranchCreation(name, source, hidden=True)
-  create_branch(repo, branch_creation)
+  create_branch(repo, branch_creation, _request_timeout=5)

Testing Details

Unit tests for create, commit and merge_into check that body fields reach the model and _request_timeout reaches the API call. pytest tests/utests (54 passed), pylint and mypy pass locally on Python 3.12. Integration tests were not run.

Breaking Change?

A misspelled or unknown keyword argument to these three methods now raises ApiTypeError from the SDK instead of being silently dropped. This matches what reset_changes, delete, revert and the tag methods already do.

Branch.create, Branch.commit and Reference.merge_into put their keyword
arguments into the request body model, which ignores keys it does not
define.  Request options such as _request_timeout were dropped without
an error.

Keyword arguments named by a field of the request body model go into
the body; all others go to the API call, as in the other methods.
@AliRamberg AliRamberg added bug Something isn't working area/sdk/python include-changelog PR description should be included in next release changelog labels Nov 24, 2026
@github-actions github-actions Bot added the area/testing Improvements or additions to tests label Nov 24, 2026

@N-o-Z N-o-Z left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!
One request before merge: state the breaking change in the changelog. The other comments are follow-ups or nits.


## Unreleased

:bug: Bugs fixed:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changelog lists this only under "Bugs fixed", but the PR also changes what callers see for an unknown keyword argument. For example, branch.commit("msg", allow_emtpy=True) was accepted before and now raises lakefs_sdk.exceptions.ApiTypeError. I think that is the right behaviour, but a user reading the release notes gets no warning. Please add it under :warning: Breaking changes:, as v0.16.0 does, and end the entry with (#10622) like the other entries in this file.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 30a8a35: added a :warning: Breaking changes: entry for the new ApiTypeError, and both entries now end with (#10622).

branch_creation = lakefs_sdk.BranchCreation(name=self._id, source=reference_id, **creation_kwargs)
with api_exception_handler(handle_conflict):
self._client.sdk_client.branches_api.create_branch(self.repo_id, branch_creation)
self._client.sdk_client.branches_api.create_branch(self.repo_id, branch_creation, **request_kwargs)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not introduced here, but this PR is what makes it reachable from these methods. A request that hits the timeout raises urllib3.exceptions.ReadTimeoutError, which is not a LakeFSException, because api_exception_handler converts only lakefs_sdk.ApiException. I ran branch.create("main", _request_timeout=0.5) against a server that never answers and got that exception after 0.5s. The customer in the linked issue wraps calls to bound their duration, so except LakeFSException will miss it. Can we open a follow-up issue, and name the exception in the kwargs docstring until then?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is nasty: raising a new exception type is actually a breaking change!1

Could we instead additionally convert these errors? If we did it now, it wouldn't be a breaking change.

In contrast, in an "exceptions" language like Python, Java, or C++, new exception types leak up through the call stack (in Java only unchecked exceptions). So adding a new exception type forces checking every caller even though the type does not change. typing cannot spec exceptions, apparently by design. So it's a breaking change.

Footnotes

  1. In a "results" language like C, or Rust, this change would not be breaking. A caller has to opt into receiving the new error type, and then it can handle the new error type. And because each caller up the stack checks its error type, handling the new error type stops escalation, and not handling the new error type changes the return type. Results have static scope, so either old code "works" (assuming errors are correctly handled) or it fails to compile. ↩

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opened #10630 for converting request timeouts into a LakeFSException, with Ariel's suggestion as the proposal: a subclass that also subclasses the urllib3 timeout error, so existing handlers keep catching it. Until then the kwargs docstrings of create, commit and merge_into name urllib3.exceptions.ReadTimeoutError (30a8a35).

return


def split_body_kwargs(model: Any, kwargs: dict[str, Any]) -> tuple[dict[str, Any], dict[str, Any]]:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The wrapper now has two ways to separate body fields from request options. Repository.create pops storage_id by hand at repository.py:57 (added in #9704), and this PR adds split_body_kwargs for three other methods. The helper is the better of the two, since a new field on the model needs no wrapper change. Could Repository.create call split_body_kwargs(lakefs_sdk.RepositoryCreation, kwargs) and drop the pop, here or in a follow-up?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done here in 30a8a35: Repository.create calls split_body_kwargs(lakefs_sdk.RepositoryCreation, kwargs) and the storage_id pop is gone. A side effect: read_only=True now reaches the request body instead of raising ApiTypeError.


monkeypatch.setattr(branch._client.sdk_client.commits_api, "commit", monkey_commit)
res = branch.commit(commit_message, metadata=md, allow_empty=True, ignored_field="test")
res = branch.commit(commit_message, metadata=md, allow_empty=True, date=123, _request_timeout=5)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The removed ignored_field="test" case was the only test of what happens to an unknown keyword, and nothing replaces it. The new tests stub the SDK method with a function that accepts any **kwargs, so they cannot fail if unknown keywords stop raising. Please add one case that patches ApiClient.call_api instead of commits_api.commit and expects ApiTypeError for something like branch.commit("msg", allow_emtpy=True). That case would also prove the real SDK method accepts _request_timeout.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added test_branch_commit_keyword_arguments in 30a8a35. It patches ApiClient.call_api, so the generated commit method runs: _request_timeout=5 reaches call_api, and allow_emtpy=True raises ApiTypeError without sending a request. Run against master's wrapper code it fails with [None] == [5].

Comment thread clients/python-wrapper/lakefs/branch.py Resolved
List the new ApiTypeError for unknown keyword arguments as a breaking
change, name the timeout exception in the kwargs docstrings, use
split_body_kwargs in Repository.create, and test keyword handling
through the real SDK method.
@AliRamberg
AliRamberg merged commit a72e9e9 into master Nov 30, 2026
58 checks passed
@AliRamberg
AliRamberg deleted the issue-1417-python-hl-sdk-branch-create branch November 30, 2026 16:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/sdk/python area/testing Improvements or additions to tests bug Something isn't working include-changelog PR description should be included in next release changelog

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants