Skip to content

Commit e5de933

Browse files
author
Val Brodsky
committed
Add an ability to pass in a config for a relationship
1 parent 142b601 commit e5de933

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

libs/labelbox/src/labelbox/exceptions.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def __init__(self, db_object_type=None, params=None, message=None):
4444
super().__init__(message)
4545
else:
4646
super().__init__("Resource '%s' not found for params: %r" %
47-
(db_object_type.type_name(), params))
47+
(db_object_type.type_name(), params))
4848
self.db_object_type = db_object_type
4949
self.params = params
5050

@@ -144,6 +144,11 @@ class OperationNotAllowedException(Exception):
144144
pass
145145

146146

147+
class OperationNotSupportedException(Exception):
148+
"""Raised when sdk does not support requested operation"""
149+
pass
150+
151+
147152
class ConfidenceNotSupportedException(Exception):
148153
"""Raised when confidence is specified for unsupported annotation type"""
149154

libs/labelbox/src/labelbox/orm/db_object.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
from dataclasses import dataclass
12
from datetime import datetime, timezone
23
from functools import wraps
34
import logging
45
import json
56

67
from labelbox import utils
7-
from labelbox.exceptions import InvalidQueryError, InvalidAttributeError
8+
from labelbox.exceptions import InvalidQueryError, InvalidAttributeError, OperationNotSupportedException
89
from labelbox.orm import query
910
from labelbox.orm.model import Field, Relationship, Entity
1011
from labelbox.pagination import PaginatedCollection
@@ -123,6 +124,7 @@ def __init__(self, source, relationship, value=None):
123124
self.supports_sorting = True
124125
self.filter_on_id = True
125126
self.value = value
127+
self.config = relationship.config
126128

127129
def __call__(self, *args, **kwargs):
128130
""" Forwards the call to either `_to_many` or `_to_one` methods,
@@ -190,7 +192,11 @@ def connect(self, other):
190192
self.source.client.execute(query_string, params)
191193

192194
def disconnect(self, other):
195+
if not self.config.disconnect_supported:
196+
raise OperationNotSupportedException(
197+
"Disconnect is not supported for this relationship")
193198
""" Disconnects source object of this manager from the `other` object. """
199+
194200
query_string, params = query.update_relationship(
195201
self.source, other, self.relationship, "disconnect")
196202
self.source.client.execute(query_string, params)

libs/labelbox/src/labelbox/orm/model.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from dataclasses import dataclass
12
from enum import Enum, auto
23
from typing import Dict, List, Union, Any, Type, TYPE_CHECKING
34

@@ -219,6 +220,10 @@ class Relationship:
219220
220221
"""
221222

223+
@dataclass
224+
class Config:
225+
disconnect_supported: bool = True
226+
222227
class Type(Enum):
223228
ToOne = auto()
224229
ToMany = auto()
@@ -238,12 +243,14 @@ def __init__(self,
238243
name=None,
239244
graphql_name=None,
240245
cache=False,
241-
deprecation_warning=None):
246+
deprecation_warning=None,
247+
config=Config()):
242248
self.relationship_type = relationship_type
243249
self.destination_type_name = destination_type_name
244250
self.filter_deleted = filter_deleted
245251
self.cache = cache
246252
self.deprecation_warning = deprecation_warning
253+
self.config = config
247254

248255
if name is None:
249256
name = utils.snake_case(destination_type_name) + (

0 commit comments

Comments
 (0)