Skip to content

Commit 43a501c

Browse files
author
Matt Sokoloff
committed
requested changes
1 parent e356ee9 commit 43a501c

File tree

7 files changed

+40
-38
lines changed

7 files changed

+40
-38
lines changed

labelbox/schema/asset_metadata.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class MetaType(Enum):
1818
TEXT = "TEXT"
1919
IMAGE_OVERLAY = "IMAGE_OVERLAY"
2020

21-
#For backwards compatibility
21+
# For backwards compatibility
2222
for topic in MetaType:
2323
vars()[topic.name] = topic.value
2424

labelbox/schema/data_row.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ class DataRow(DbObject, Updateable, BulkDeletable):
3434
metadata = Relationship.ToMany("AssetMetadata", False, "metadata")
3535
predictions = Relationship.ToMany("Prediction", False)
3636

37+
supported_meta_types = {
38+
meta_type.value for meta_type in AssetMetadata.MetaType
39+
}
40+
3741
@staticmethod
3842
def bulk_delete(data_rows):
3943
""" Deletes all the given DataRows.
@@ -62,10 +66,10 @@ def create_metadata(self, meta_type, meta_value):
6266
Raises:
6367
ValueError: meta_type must be one of the supported types.
6468
"""
65-
supported_meta_types = [x.value for x in AssetMetadata.MetaType]
66-
if meta_type not in supported_meta_types:
69+
70+
if meta_type not in self.supported_meta_types:
6771
raise ValueError(
68-
f"metadata type must be one of {supported_meta_types}. Found {meta_type}"
72+
f"meta_type must be one of {self.supported_meta_types}. Found {meta_type}"
6973
)
7074

7175
meta_type_param = "metaType"

labelbox/schema/dataset.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,6 @@ def data_row_for_external_id(self, external_id):
210210
limit=2)
211211
if len(data_rows) > 1:
212212
logging.warn(
213-
"More than one data_row has the provided external_id. Use function data_rows_for_external_id to fetch all"
213+
f"More than one data_row has the provided external_id : `{external_id}`. Use function data_rows_for_external_id to fetch all"
214214
)
215215
return data_rows[0]

labelbox/schema/project.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def create_label(self, **kwargs):
9090
# about them. At the same time they're connected to a Label at
9191
# label creation in a non-standard way (connect via name).
9292
logger.warning(
93-
"This function is deprecated and is not compatible with the new editor."
93+
"`create_label` is deprecated and is not compatible with the new editor."
9494
)
9595

9696
Label = Entity.Label
@@ -417,7 +417,7 @@ def upsert_review_queue(self, quota_factor):
417417
to reinitiate. Between 0 and 1.
418418
"""
419419

420-
if (quota_factor > 1.) or (quota_factor < 0.):
420+
if not 0. < quota_factor < 1.:
421421
raise ValueError("Quota factor must be in the range of [0,1]")
422422

423423
id_param = "projectId"
@@ -460,7 +460,7 @@ def create_prediction_model(self, name, version):
460460
"""
461461

462462
logger.warning(
463-
"This function is deprecated and is not compatible with the new editor."
463+
"`create_prediction_model` is deprecated and is not compatible with the new editor."
464464
)
465465

466466
PM = Entity.PredictionModel
@@ -489,7 +489,7 @@ def create_prediction(self, label, data_row, prediction_model=None):
489489
None.
490490
"""
491491
logger.warning(
492-
"This function is deprecated and is not compatible with the new editor."
492+
"`create_prediction` is deprecated and is not compatible with the new editor."
493493
)
494494

495495
if prediction_model is None:

labelbox/schema/webhook.py

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import logging
22
from enum import Enum
3-
from typing import List, Optional, Union
3+
from typing import Iterable, List
44

55
from labelbox.orm import query
66
from labelbox.orm.db_object import DbObject, Updateable
77
from labelbox.orm.model import Entity, Field, Relationship
8-
from labelbox import Client, Project
98

109
logger = logging.getLogger(__name__)
1110

@@ -25,24 +24,24 @@ class Webhook(DbObject, Updateable):
2524
2625
"""
2726

28-
class WebhookStatus(Enum):
27+
class Status(Enum):
2928
ACTIVE = "ACTIVE"
3029
INACTIVE = "INACTIVE"
3130
REVOKED = "REVOKED"
3231

33-
class WebhookTopic(Enum):
32+
class Topic(Enum):
3433
LABEL_CREATED = "LABEL_CREATED"
3534
LABEL_UPDATED = "LABEL_UPDATED"
3635
LABEL_DELETED = "LABEL_DELETED"
3736
REVIEW_CREATED = "REVIEW_CREATED"
3837
REVIEW_UPDATED = "REVIEW_UPDATED"
3938
REVIEW_DELETED = "REVIEW_DELETED"
4039

41-
#For backwards compatibility
42-
for topic in WebhookStatus:
40+
# For backwards compatibility
41+
for topic in Status:
4342
vars()[topic.name] = topic.value
4443

45-
for status in WebhookTopic:
44+
for status in Topic:
4645
vars()[status.name] = status.value
4746

4847
updated_at = Field.DateTime("updated_at")
@@ -56,15 +55,14 @@ class WebhookTopic(Enum):
5655
project = Relationship.ToOne("Project")
5756

5857
@staticmethod
59-
def create(client: Client, topics: List[WebhookTopic], url: str,
60-
secret: str, project: Project):
58+
def create(client, topics, url, secret, project):
6159
""" Creates a Webhook.
6260
6361
Args:
6462
client (Client): The Labelbox client used to connect
6563
to the server.
6664
topics (list of str): A list of topics this Webhook should
67-
get notifications for. Must be one of Webhook.WebhookTopic
65+
get notifications for. Must be one of Webhook.Topic
6866
url (str): The URL to which notifications should be sent
6967
by the Labelbox server.
7068
secret (str): A secret key used for signing notifications.
@@ -75,7 +73,7 @@ def create(client: Client, topics: List[WebhookTopic], url: str,
7573
A newly created Webhook.
7674
7775
Raises:
78-
ValueError: If the topic is not one of WebhookTopic or status is not one of WebhookStatus
76+
ValueError: If the topic is not one of Topic or status is not one of Status
7977
8078
Information on configuring your server can be found here (this is where the url points to and the secret is set).
8179
https://docs.labelbox.com/en/configure-editor/webhooks-setup#setup-steps
@@ -94,17 +92,17 @@ def create(client: Client, topics: List[WebhookTopic], url: str,
9492
return Webhook(client, client.execute(query_str)["createWebhook"])
9593

9694
@staticmethod
97-
def validate_topics(topics: List[WebhookTopic]):
98-
if not isinstance(topics, list):
95+
def validate_topics(topics):
96+
if isinstance(topics, str) or not isinstance(topics, Iterable):
9997
raise TypeError(
100-
f"Topics must be List[Webhook.WebhookTopic]. Found `{topics}`")
98+
f"Topics must be List[Webhook.Topic]. Found `{topics}`")
10199

102100
for topic in topics:
103-
Webhook.validate_value(topic, Webhook.WebhookTopic)
101+
Webhook.validate_value(topic, Webhook.Topic)
104102

105103
@staticmethod
106-
def validate_value(value: str, enum: Union[WebhookStatus, WebhookTopic]):
107-
supported_values = [x.value for x in enum]
104+
def validate_value(value, enum):
105+
supported_values = {item.value for item in enum}
108106
if value not in supported_values:
109107
raise ValueError(
110108
f"Value `{value}` does not exist in supported values. Expected one of {supported_values}"
@@ -114,18 +112,15 @@ def delete(self):
114112
"""
115113
Deletes the webhook
116114
"""
117-
self.update(status=self.WebhookStatus.INACTIVE)
115+
self.update(status=self.Status.INACTIVE)
118116

119-
def update(self,
120-
topics: Optional[List[WebhookTopic]] = None,
121-
url: Optional[str] = None,
122-
status: Optional[WebhookStatus] = None):
117+
def update(self, topics=None, url=None, status=None):
123118
""" Updates the Webhook.
124119
125120
Args:
126-
topics (Optional[List[WebhookTopic]]): The new topics.
121+
topics (Optional[List[Topic]]): The new topics.
127122
url Optional[str): The new URL value.
128-
status (Optional[WebhookStatus]): The new status.
123+
status (Optional[Status]): The new status.
129124
130125
If values are set to None then there are no updates made to that field.
131126
@@ -138,7 +133,7 @@ def update(self,
138133
self.validate_topics(topics)
139134

140135
if status is not None:
141-
self.validate_value(status, self.WebhookStatus)
136+
self.validate_value(status, self.Status)
142137

143138
topics_str = "" if topics is None \
144139
else "topics: {set: [%s]}" % " ".join(topics)

tests/integration/test_asset_metadata.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ def test_asset_metadata_crud(project, dataset, rand_gen):
2121

2222
with pytest.raises(ValueError) as exc_info:
2323
data_row.create_metadata("NOT_SUPPORTED_TYPE", "Value")
24+
expected_types = {item.value for item in AssetMetadata.MetaType}
2425
assert str(exc_info.value) == \
25-
f"metadata type must be one of {[x.value for x in AssetMetadata.MetaType]}. Found NOT_SUPPORTED_TYPE"
26+
f"meta_type must be one of {expected_types}. Found NOT_SUPPORTED_TYPE"
2627

2728
# Check that filtering and sorting is prettily disabled
2829
with pytest.raises(InvalidQueryError) as exc_info:

tests/integration/test_webhook.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,17 @@ def test_webhook_create_update(project, rand_gen):
2424

2525
with pytest.raises(ValueError) as exc_info:
2626
webhook.update(status="invalid..")
27+
valid_webhook_statuses = {item.value for item in Webhook.Status}
2728
assert str(exc_info.value) == \
28-
f"Value `invalid..` does not exist in supported values. Expected one of {[x.value for x in Webhook.WebhookStatus]}"
29+
f"Value `invalid..` does not exist in supported values. Expected one of {valid_webhook_statuses}"
2930

3031
with pytest.raises(ValueError) as exc_info:
3132
webhook.update(topics=["invalid.."])
33+
valid_webhook_topics = {item.value for item in Webhook.Topic}
3234
assert str(exc_info.value) == \
33-
f"Value `invalid..` does not exist in supported values. Expected one of {[x.value for x in Webhook.WebhookTopic]}"
35+
f"Value `invalid..` does not exist in supported values. Expected one of {valid_webhook_topics}"
3436

3537
with pytest.raises(TypeError) as exc_info:
3638
webhook.update(topics="invalid..")
3739
assert str(exc_info.value) == \
38-
"Topics must be List[Webhook.WebhookTopic]. Found `invalid..`"
40+
"Topics must be List[Webhook.Topic]. Found `invalid..`"

0 commit comments

Comments
 (0)