From b254e99f283916fcff47aeeb44b2d55f3cbaff15 Mon Sep 17 00:00:00 2001 From: Kien Dang Date: Thu, 22 Aug 2024 15:26:56 +0800 Subject: [PATCH 1/2] Improve error message when submitting a Project with no members --- packages/syft/src/syft/service/project/project.py | 6 ++++++ packages/syft/tests/syft/project/project_test.py | 12 ++++++++++++ 2 files changed, 18 insertions(+) diff --git a/packages/syft/src/syft/service/project/project.py b/packages/syft/src/syft/service/project/project.py index 0b64a9d8870..406a2d7508e 100644 --- a/packages/syft/src/syft/service/project/project.py +++ b/packages/syft/src/syft/service/project/project.py @@ -1106,6 +1106,9 @@ def pending_requests(self) -> int: ) +_EMPTY_MEMBER_LIST_ERROR_MESSAGE = "Project needs at least 1 member." + + @serializable(without=["bootstrap_events", "clients"]) class ProjectSubmit(SyftObject): __canonical_name__ = "ProjectSubmit" @@ -1232,6 +1235,9 @@ def start(self, return_all_projects: bool = False) -> Project | list[Project]: return self.send(return_all_projects=return_all_projects) def send(self, return_all_projects: bool = False) -> Project | list[Project]: + if len(self.clients) == 0: + return SyftError(message=_EMPTY_MEMBER_LIST_ERROR_MESSAGE) + # Currently we are assuming that the first member is the leader # This would be changed in our future leaderless approach leader = self.clients[0] diff --git a/packages/syft/tests/syft/project/project_test.py b/packages/syft/tests/syft/project/project_test.py index ba531ff2aac..abb18921a19 100644 --- a/packages/syft/tests/syft/project/project_test.py +++ b/packages/syft/tests/syft/project/project_test.py @@ -5,6 +5,8 @@ # syft absolute import syft as sy from syft.service.project.project import Project +from syft.service.project.project import _EMPTY_MEMBER_LIST_ERROR_MESSAGE +from syft.service.response import SyftError def test_project_creation(worker): @@ -110,3 +112,13 @@ def test_project_serde(worker): deser_data = sy.deserialize(ser_data, from_bytes=True) assert isinstance(deser_data, type(project)) assert deser_data == project + + +def test_submit_project_with_empty_member_list_error() -> None: + new_project = sy.Project( + name="My Cool Project", description="My Cool Description", members=[] + ) + + res = new_project.send() + assert isinstance(res, SyftError) + assert _EMPTY_MEMBER_LIST_ERROR_MESSAGE in res.message From 2848e38da951b89c94ab628909b4eb8a91075d22 Mon Sep 17 00:00:00 2001 From: Kien Dang Date: Tue, 27 Aug 2024 16:24:34 +0800 Subject: [PATCH 2/2] Use SyftException --- packages/syft/src/syft/service/project/project.py | 2 +- packages/syft/tests/syft/project/project_test.py | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/syft/src/syft/service/project/project.py b/packages/syft/src/syft/service/project/project.py index 406a2d7508e..540ae586152 100644 --- a/packages/syft/src/syft/service/project/project.py +++ b/packages/syft/src/syft/service/project/project.py @@ -1236,7 +1236,7 @@ def start(self, return_all_projects: bool = False) -> Project | list[Project]: def send(self, return_all_projects: bool = False) -> Project | list[Project]: if len(self.clients) == 0: - return SyftError(message=_EMPTY_MEMBER_LIST_ERROR_MESSAGE) + raise SyftException(public_message=_EMPTY_MEMBER_LIST_ERROR_MESSAGE) # Currently we are assuming that the first member is the leader # This would be changed in our future leaderless approach diff --git a/packages/syft/tests/syft/project/project_test.py b/packages/syft/tests/syft/project/project_test.py index abb18921a19..797583ad522 100644 --- a/packages/syft/tests/syft/project/project_test.py +++ b/packages/syft/tests/syft/project/project_test.py @@ -6,7 +6,7 @@ import syft as sy from syft.service.project.project import Project from syft.service.project.project import _EMPTY_MEMBER_LIST_ERROR_MESSAGE -from syft.service.response import SyftError +from syft.types.errors import SyftException def test_project_creation(worker): @@ -119,6 +119,6 @@ def test_submit_project_with_empty_member_list_error() -> None: name="My Cool Project", description="My Cool Description", members=[] ) - res = new_project.send() - assert isinstance(res, SyftError) - assert _EMPTY_MEMBER_LIST_ERROR_MESSAGE in res.message + with pytest.raises(SyftException) as exc: + new_project.send() + assert _EMPTY_MEMBER_LIST_ERROR_MESSAGE in exc.value.public_message