Repository navigation
Python wrapper: send request options from Branch.create, Branch.commit and merge_into #10622
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,7 +19,7 @@ | |
| from lakefs.object import WriteableObject | ||
| from lakefs.object import StoredObject | ||
| from lakefs.import_manager import ImportManager | ||
| from lakefs.reference import Reference, ReferenceType, generate_listing | ||
| from lakefs.reference import Reference, ReferenceType, generate_listing, split_body_kwargs | ||
| if TYPE_CHECKING: | ||
| from lakefs.repository import Repository | ||
| from lakefs.models import Change, Commit | ||
|
|
@@ -195,6 +195,10 @@ def create(self, source_reference: ReferenceType, exist_ok: bool = False, **kwar | |
| :param source_reference: The reference to create the branch from (reference ID, object or Commit object) | ||
| :param exist_ok: If False will throw an exception if a branch by this name already exists. Otherwise, | ||
| return the existing branch without creating a new one | ||
| :param kwargs: Branch creation parameters (fields of ``lakefs_sdk.BranchCreation`` such as ``hidden``), | ||
| and additional Keyword Arguments to send to the server (such as ``_request_timeout``). | ||
| A request that exceeds ``_request_timeout`` raises ``urllib3.exceptions.ReadTimeoutError``, | ||
| which is not a ``LakeFSException`` | ||
| :return: The lakeFS SDK object representing the branch | ||
| :raise NotFoundException: if repo, branch or source reference id does not exist | ||
| :raise ConflictException: if branch already exists and exist_ok is False | ||
|
|
@@ -208,9 +212,10 @@ def handle_conflict(e: LakeFSException): | |
| return e | ||
|
|
||
| reference_id = source_reference if isinstance(source_reference, str) else source_reference.id | ||
| branch_creation = lakefs_sdk.BranchCreation(name=self._id, source=reference_id, **kwargs) | ||
| creation_kwargs, request_kwargs = split_body_kwargs(lakefs_sdk.BranchCreation, kwargs) | ||
| 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) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Opened #10630 for converting request timeouts into a |
||
| return self | ||
|
|
||
| @property | ||
|
|
@@ -233,17 +238,21 @@ def commit(self, message: str, metadata: Optional[dict] = None, **kwargs) -> Ref | |
|
|
||
| :param message: Commit message | ||
| :param metadata: Metadata to attach to the commit | ||
| :param kwargs: Additional Keyword Arguments for commit creation | ||
| :param kwargs: Commit creation parameters (fields of ``lakefs_sdk.CommitCreation`` such as ``allow_empty``), | ||
| and additional Keyword Arguments to send to the server (such as ``_request_timeout``). | ||
| A request that exceeds ``_request_timeout`` raises ``urllib3.exceptions.ReadTimeoutError``, | ||
| which is not a ``LakeFSException`` | ||
| :return: The new reference after the commit | ||
| :raise NotFoundException: if branch by this id does not exist | ||
| :raise ForbiddenException: if commit is not allowed on this branch | ||
| :raise NotAuthorizedException: if user is not authorized to perform this operation | ||
| :raise ServerException: for any other errors | ||
| """ | ||
| commits_creation = lakefs_sdk.CommitCreation(message=message, metadata=metadata, **kwargs) | ||
| creation_kwargs, request_kwargs = split_body_kwargs(lakefs_sdk.CommitCreation, kwargs) | ||
| commits_creation = lakefs_sdk.CommitCreation(message=message, metadata=metadata, **creation_kwargs) | ||
|
|
||
| with api_exception_handler(): | ||
| c = self._client.sdk_client.commits_api.commit(self.repo_id, self._id, commits_creation) | ||
| c = self._client.sdk_client.commits_api.commit(self.repo_id, self._id, commits_creation, **request_kwargs) | ||
|
AliRamberg marked this conversation as resolved.
Resolved
|
||
| return Reference(self._repo, c.id, self._client) | ||
|
|
||
| def delete(self, **kwargs) -> None: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,7 @@ | |
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import Optional, Generator, Union, TYPE_CHECKING, overload | ||
| from typing import Any, Optional, Generator, Union, TYPE_CHECKING, overload | ||
|
|
||
| from typing_extensions import deprecated | ||
|
|
||
|
|
@@ -166,19 +166,24 @@ def merge_into(self, destination_branch: ReferenceType, **kwargs) -> str: | |
| Merge this reference into destination branch | ||
|
|
||
| :param destination_branch: The merge destination (either ID or branch object) | ||
| :param kwargs: Additional Keyword Arguments to send to the server | ||
| :param kwargs: Merge parameters (fields of ``lakefs_sdk.Merge`` such as ``message`` or ``allow_empty``), | ||
| and additional Keyword Arguments to send to the server (such as ``_request_timeout``). | ||
| A request that exceeds ``_request_timeout`` raises ``urllib3.exceptions.ReadTimeoutError``, | ||
| which is not a ``LakeFSException`` | ||
| :return: The reference id of the merge commit | ||
| :raise NotFoundException: if reference by this id does not exist, or branch doesn't exist | ||
| :raise NotAuthorizedException: if user is not authorized to perform this operation | ||
| :raise ServerException: for any other errors | ||
| """ | ||
| branch_id = destination_branch if isinstance(destination_branch, str) else destination_branch.id | ||
| merge_kwargs, request_kwargs = split_body_kwargs(lakefs_sdk.Merge, kwargs) | ||
| with api_exception_handler(): | ||
| merge = lakefs_sdk.Merge(**kwargs) | ||
| merge = lakefs_sdk.Merge(**merge_kwargs) | ||
| res = self._client.sdk_client.refs_api.merge_into_branch(self.repo_id, | ||
| self._id, | ||
| branch_id, | ||
| merge=merge) | ||
| merge=merge, | ||
| **request_kwargs) | ||
| return res.reference | ||
|
|
||
| def object(self, path: str) -> StoredObject: # pylint: disable=C0103 | ||
|
|
@@ -219,4 +224,18 @@ def generate_listing(func, *args, max_amount: Optional[int] = None, **kwargs): | |
| return | ||
|
|
||
|
|
||
| def split_body_kwargs(model: Any, kwargs: dict[str, Any]) -> tuple[dict[str, Any], dict[str, Any]]: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done here in 30a8a35: |
||
| """ | ||
| Split keyword arguments between a lakefs-sdk request body model and the API call that sends it | ||
|
|
||
| :param model: The lakefs-sdk request body model class | ||
| :param kwargs: The keyword arguments to split | ||
| :return: The arguments named by a field of the model (by name or alias), and all other arguments | ||
| """ | ||
| names = set(model.__fields__) | {field.alias for field in model.__fields__.values()} | ||
| body = {k: v for k, v in kwargs.items() if k in names} | ||
| request = {k: v for k, v in kwargs.items() if k not in names} | ||
| return body, request | ||
|
|
||
|
|
||
| ReferenceType = Union[str, Reference, Commit] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,14 +30,16 @@ def test_branch_create(monkeypatch): | |
| branch = get_test_branch() | ||
| source = "main" | ||
| with monkeypatch.context(): | ||
| def monkey_create_branch(_self, repo_name, branch_creation, *_): | ||
| def monkey_create_branch(_self, repo_name, branch_creation, **kwargs): | ||
| assert repo_name == branch.repo_id | ||
| assert branch_creation.name == branch.id | ||
| assert branch_creation.source == source | ||
| assert branch_creation.hidden | ||
| assert kwargs == {"_request_timeout": 5} | ||
| return lakefs_sdk.BranchCreation(name=branch.id, source=source) | ||
|
|
||
| monkeypatch.setattr(lakefs_sdk.BranchesApi, "create_branch", monkey_create_branch) | ||
| branch.create(source) | ||
| branch.create(source, hidden=True, _request_timeout=5) | ||
|
|
||
|
|
||
| def test_branch_create_already_exists(monkeypatch): | ||
|
|
@@ -82,12 +84,14 @@ def test_branch_commit(monkeypatch): | |
| commit_id = "1234" | ||
| commit_message = "test message" | ||
| with monkeypatch.context(): | ||
| def monkey_commit(repo_name, branch_name, commits_creation, *_): | ||
| def monkey_commit(repo_name, branch_name, commits_creation, **kwargs): | ||
| assert repo_name == branch.repo_id | ||
| assert branch_name == branch.id | ||
| assert commits_creation.message == commit_message | ||
| assert commits_creation.metadata == md | ||
| assert commits_creation.allow_empty | ||
| assert commits_creation.var_date == 123 | ||
| assert kwargs == {"_request_timeout": 5} | ||
| return lakefs_sdk.Commit( | ||
| id=commit_id, | ||
| parents=[""], | ||
|
|
@@ -98,10 +102,34 @@ def monkey_commit(repo_name, branch_name, commits_creation, *_): | |
| ) | ||
|
|
||
| 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) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The removed
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added |
||
| assert res.id == commit_id | ||
|
|
||
|
|
||
| def test_branch_commit_keyword_arguments(monkeypatch): | ||
| branch = get_test_branch() | ||
| request_timeouts = [] | ||
| with monkeypatch.context(): | ||
| def monkey_call_api(_self, *_, **kwargs): | ||
| request_timeouts.append(kwargs["_request_timeout"]) | ||
| return lakefs_sdk.Commit( | ||
| id="1234", | ||
| parents=[""], | ||
| committer="Committer", | ||
| message="msg", | ||
| creation_date=0, | ||
| meta_range_id="", | ||
| ) | ||
|
|
||
| monkeypatch.setattr(lakefs_sdk.ApiClient, "call_api", monkey_call_api) | ||
| branch.commit("msg", _request_timeout=5) | ||
| assert request_timeouts == [5] | ||
|
|
||
| with expect_exception_context(lakefs_sdk.exceptions.ApiTypeError): | ||
| branch.commit("msg", allow_emtpy=True) | ||
| assert request_timeouts == [5] | ||
|
|
||
|
|
||
| def test_branch_delete(monkeypatch): | ||
| branch = get_test_branch() | ||
| with monkeypatch.context(): | ||
|
|
||
There was a problem hiding this comment.
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 raiseslakefs_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.
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 newApiTypeError, and both entries now end with(#10622).