Repository navigation
Python wrapper: send request options from Branch.create, Branch.commit and merge_into - #10622
Conversation
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.
N-o-Z
left a comment
There was a problem hiding this comment.
Thanks!
One request before merge: state the breaking change in the changelog. The other comments are follow-ups or nits.
|
|
||
| ## Unreleased | ||
|
|
||
| :bug: Bugs fixed: |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
-
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. ↩
There was a problem hiding this comment.
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]]: |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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].
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.
Closes treeverse/product#1417
Change Description
Bug Fix
Branch.create,Branch.commitandReference.merge_intoaccept**kwargs, but request options such as_request_timeoutpassed 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.**kwargsonly to the request body model (BranchCreation,CommitCreation,Merge), which ignores keys it does not define. The other high-level methods pass**kwargsto the API call instead.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,commitandmerge_intocheck that body fields reach the model and_request_timeoutreaches 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
ApiTypeErrorfrom the SDK instead of being silently dropped. This matches whatreset_changes,delete,revertand the tag methods already do.