Skip to content

Commit ebc285e

Browse files
author
Adrian Chang
committed
Address 3-5 feedback
1 parent 7ddeef7 commit ebc285e

File tree

2 files changed

+65
-104
lines changed

2 files changed

+65
-104
lines changed

libs/labelbox/src/labelbox/schema/user_group.py

Lines changed: 28 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -89,24 +89,23 @@ class UserGroup(BaseModel):
8989
Represents a user group in Labelbox.
9090
9191
Attributes:
92-
id (Optional[str]): The ID of the user group.
93-
name (Optional[str]): The name of the user group.
92+
id (str): The ID of the user group.
93+
name (str): The name of the user group.
9494
color (UserGroupColor): The color of the user group.
9595
users (Set[UserGroupUser]): The set of users in the user group.
9696
projects (Set[UserGroupProject]): The set of projects associated with the user group.
9797
client (Client): The Labelbox client object.
9898
9999
Methods:
100-
__init__(self, client: Client, id: str = "", name: str = "", color: UserGroupColor = UserGroupColor.BLUE,
101-
users: Set[UserGroupUser] = set(), projects: Set[UserGroupProject] = set(), reload=True)
102-
_reload(self)
100+
__init__(self, client: Client)
101+
get(self) -> "UserGroup"
103102
update(self) -> "UserGroup"
104103
create(self) -> "UserGroup"
105104
delete(self) -> bool
106105
get_user_groups(client: Client) -> Iterator["UserGroup"]
107106
"""
108-
id: Optional[str]
109-
name: Optional[str]
107+
id: str
108+
name: str
110109
color: UserGroupColor
111110
users: Set[UserGroupUser]
112111
projects: Set[UserGroupProject]
@@ -119,51 +118,38 @@ class Config:
119118
def __init__(
120119
self,
121120
client: Client,
122-
id: str = "",
123-
name: str = "",
124-
color: UserGroupColor = UserGroupColor.BLUE,
125-
users: Set[UserGroupUser] = set(),
126-
projects: Set[UserGroupProject] = set(),
127-
reload=True,
128121
):
129122
"""
130123
Initializes a UserGroup object.
131124
132125
Args:
133126
client (Client): The Labelbox client object.
134-
id (str, optional): The ID of the user group. Defaults to an empty string.
135-
name (str, optional): The name of the user group. Defaults to an empty string.
136-
color (UserGroupColor, optional): The color of the user group. Defaults to UserGroupColor.BLUE.
137-
users (Set[UserGroupUser], optional): The set of users in the user group. Defaults to an empty set.
138-
projects (Set[UserGroupProject], optional): The set of projects associated with the user group. Defaults to an empty set.
139-
reload (bool, optional): Whether to reload the partial representation of the group. Defaults to True.
140127
141128
Raises:
142129
RuntimeError: If the experimental feature is not enabled in the client.
143130
144131
"""
145-
super().__init__(client=client, id=id, name=name, color=color, users=users, projects=projects)
132+
super().__init__(client=client, id="", name="", color=UserGroupColor.BLUE, users=set(), projects=set())
146133
if not self.client.enable_experimental:
147134
raise RuntimeError(
148135
"Please enable experimental in client to use UserGroups")
149136

150-
# partial representation of the group, reload
151-
if self.id and reload:
152-
self._reload()
153-
154-
def _reload(self):
137+
def get(self) -> "UserGroup":
155138
"""
156139
Reloads the user group information from the server.
157140
158141
This method sends a GraphQL query to the server to fetch the latest information
159142
about the user group, including its name, color, projects, and members. The fetched
160143
information is then used to update the corresponding attributes of the `Group` object.
161144
162-
Raises:
163-
InvalidQueryError: If the query fails to fetch the group information.
145+
Args:
146+
id (str): The ID of the user group to fetch.
164147
165148
Returns:
166-
None
149+
UserGroup of passed in ID (self)
150+
151+
Raises:
152+
InvalidQueryError: If the query fails to fetch the group information.
167153
"""
168154
query = """
169155
query GetUserGroupPyApi($id: ID!) {
@@ -204,6 +190,7 @@ def _reload(self):
204190
UserGroupUser(id=member["id"], email=member["email"])
205191
for member in result["userGroup"]["members"]["nodes"]
206192
}
193+
return self
207194

208195
def update(self) -> "UserGroup":
209196
"""
@@ -397,21 +384,19 @@ def get_user_groups(client: Client) -> Iterator["UserGroup"]:
397384
yield
398385
groups = userGroups["nodes"]
399386
for group in groups:
400-
yield UserGroup(client,
401-
reload=False,
402-
id=group["id"],
403-
name=group["name"],
404-
color=UserGroupColor(group["color"]),
405-
users={
406-
UserGroupUser(id=member["id"],
407-
email=member["email"])
408-
for member in group["members"]["nodes"]
409-
},
410-
projects={
411-
UserGroupProject(id=project["id"],
412-
name=project["name"])
413-
for project in group["projects"]["nodes"]
414-
})
387+
userGroup = UserGroup(client)
388+
userGroup.id = group["id"]
389+
userGroup.name = group["name"]
390+
userGroup.color = UserGroupColor(group["color"])
391+
userGroup.users = {
392+
UserGroupUser(id=member["id"], email=member["email"])
393+
for member in group["members"]["nodes"]
394+
}
395+
userGroup.projects = {
396+
UserGroupProject(id=project["id"], name=project["name"])
397+
for project in group["projects"]["nodes"]
398+
}
399+
yield userGroup
415400
nextCursor = userGroups["nextCursor"]
416401
# this doesn't seem to be implemented right now to return a value other than null from the api
417402
if not nextCursor:

libs/labelbox/tests/unit/schema/test_user_group.py

Lines changed: 37 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -59,71 +59,44 @@ class TestUserGroup:
5959
def setup_method(self):
6060
self.client = MagicMock(Client)
6161
self.client.enable_experimental = True
62-
self.group = UserGroup(client=self.client, name="Test Group")
62+
self.group = UserGroup(client=self.client)
6363

6464
def test_constructor_experimental_needed(self):
6565
client = MagicMock(Client)
6666
client.enable_experimental = False
6767
with pytest.raises(RuntimeError):
6868
group = UserGroup(client)
6969

70-
def test_constructor_name(self):
71-
group = self.group
72-
assert group.name == "Test Group"
73-
assert group.color == UserGroupColor.BLUE
74-
75-
def test_constructor_id_no_reload(self):
76-
projects = [{
77-
"id": "project_id_1",
78-
"name": "project_1"
79-
}, {
80-
"id": "project_id_2",
81-
"name": "project_2"
82-
}]
83-
group_members = [{
84-
"id": "user_id_1",
85-
"email": "email_1"
86-
}, {
87-
"id": "user_id_2",
88-
"email": "email_2"
89-
}]
90-
self.client.execute.return_value = {
91-
"userGroup": {
92-
"id": "group_id",
93-
"name": "Test Group",
94-
"color": "4ED2F9",
95-
"projects": {
96-
"nodes": projects
97-
},
98-
"members": {
99-
"nodes": group_members
100-
}
101-
}
102-
}
103-
104-
group = UserGroup(self.client, id="group_id", reload=False)
70+
def test_constructor(self):
71+
group = UserGroup(self.client)
10572

106-
assert group.id == "group_id"
73+
assert group.id == ""
10774
assert group.name == ""
10875
assert group.color is UserGroupColor.BLUE
10976
assert len(group.projects) == 0
11077
assert len(group.users) == 0
11178

112-
def test_constructor_id(self):
113-
projects = [{
114-
"id": "project_id_1",
115-
"name": "project_1"
116-
}, {
117-
"id": "project_id_2",
118-
"name": "project_2"
119-
}]
120-
group_members = [{
121-
"id": "user_id_1",
122-
"email": "email_1"
123-
}, {
124-
"id": "user_id_2",
125-
"email": "email_2"
126-
}]
79+
def test_get(self):
80+
projects = [
81+
{
82+
"id": "project_id_1",
83+
"name": "project_1"
84+
},
85+
{
86+
"id": "project_id_2",
87+
"name": "project_2"
88+
}
89+
]
90+
group_members = [
91+
{
92+
"id": "user_id_1",
93+
"email": "email_1"
94+
},
95+
{
96+
"id": "user_id_2",
97+
"email": "email_2"
98+
}
99+
]
127100
self.client.execute.return_value = {
128101
"userGroup": {
129102
"id": "group_id",
@@ -137,26 +110,29 @@ def test_constructor_id(self):
137110
}
138111
}
139112
}
140-
group = UserGroup(self.client, id="group_id")
113+
group = UserGroup(self.client)
114+
assert group.id == ""
115+
assert group.name == ""
116+
assert group.color is UserGroupColor.BLUE
117+
assert len(group.projects) == 0
118+
assert len(group.users) == 0
119+
120+
group.id = "group_id"
121+
group.get()
122+
141123
assert group.id == "group_id"
142124
assert group.name == "Test Group"
143-
assert group.color == UserGroupColor.CYAN
125+
assert group.color is UserGroupColor.CYAN
144126
assert len(group.projects) == 2
145127
assert len(group.users) == 2
146128

147129
def test_id(self):
148130
group = self.group
149131
assert group.id == ""
150132

151-
group.id = "1"
152-
assert group.id == "1"
153-
154-
group.id = "2"
155-
assert group.id == "2"
156-
157133
def test_name(self):
158134
group = self.group
159-
assert group.name == "Test Group"
135+
assert group.name == ""
160136

161137
group.name = "New Group"
162138
assert group.name == "New Group"

0 commit comments

Comments
 (0)