From 7a2fdc0bf5f2eb81c3c0c1e102143a41408cac4e Mon Sep 17 00:00:00 2001 From: Kien Dang Date: Thu, 22 Aug 2024 15:26:56 +0800 Subject: [PATCH] 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 e1c1dfe1b47..d4568dd36f6 100644 --- a/packages/syft/src/syft/service/project/project.py +++ b/packages/syft/src/syft/service/project/project.py @@ -1132,6 +1132,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" @@ -1258,6 +1261,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 587f9582274..15f6de06703 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): @@ -108,3 +110,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