Skip to content

Commit 12ba12a

Browse files
feat: ability to add client_metadata directly in folder.create_data_group - BOT-516 (#1032)
1 parent dbabc87 commit 12ba12a

File tree

5 files changed

+81
-34
lines changed

5 files changed

+81
-34
lines changed

encord/common/deprecated.py

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,34 @@
11
import functools
2+
import inspect
23
import warnings
34

45

56
def deprecated(version, alternative=None):
6-
def decorator(func):
7-
@functools.wraps(func)
8-
def new_func(*args, **kwargs):
9-
message = f"Function {func.__name__} is deprecated"
10-
if version:
11-
message += f" since version {version}"
12-
if alternative:
13-
message += f", use {alternative} instead"
14-
warnings.warn(message, category=DeprecationWarning, stacklevel=2)
15-
return func(*args, **kwargs)
7+
def decorator(obj):
8+
message = f"{obj.__name__} is deprecated"
9+
if version:
10+
message += f" since version {version}"
11+
if alternative:
12+
message += f", use {alternative} instead"
1613

17-
return new_func
14+
if inspect.isclass(obj):
15+
# Handle class
16+
original_init = obj.__init__
17+
18+
@functools.wraps(original_init)
19+
def new_init(self, *args, **kwargs):
20+
warnings.warn(message, category=DeprecationWarning, stacklevel=2)
21+
original_init(self, *args, **kwargs)
22+
23+
obj.__init__ = new_init
24+
return obj
25+
else:
26+
# Handle function
27+
@functools.wraps(obj)
28+
def new_func(*args, **kwargs):
29+
warnings.warn(message, category=DeprecationWarning, stacklevel=2)
30+
return obj(*args, **kwargs)
31+
32+
return new_func
1833

1934
return decorator

encord/orm/storage.py

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
from dataclasses import dataclass
44
from datetime import datetime
55
from enum import auto
6-
from typing import Dict, List, Optional, Union
6+
from typing import Any, Dict, List, Optional, Union
77
from uuid import UUID
88

9+
from encord.common.deprecated import deprecated
910
from encord.orm.analytics import CamelStrEnum
1011
from encord.orm.base_dto import BaseDTO, Field
1112
from encord.orm.dataset import DataUnitError, LongPollingStatus
@@ -266,23 +267,10 @@ class CreateStorageFolderPayload(BaseDTO):
266267
cloud_synced_folder_params: Optional[CloudSyncedFolderParams] = None
267268

268269

269-
class LayoutPayload(BaseDTO):
270-
"""Layout configuration for a group or view.
271-
272-
Args:
273-
layout: Layout specification. Can be a built-in layout identifier
274-
(``"default-grid"`` or ``"default-list"``) or a custom
275-
layout dictionary.
276-
"""
277-
278-
layout: Union[Literal["default-grid"], Literal["default-list"], Dict]
279-
280-
281270
class DataGroupGrid(BaseDTO):
282271
"""Grid-based layout for a data group.
283272
284273
Args:
285-
layout_type: Fixed value ``"default-grid"`` identifying grid layout.
286274
layout_contents: Ordered list of item UUIDs to display in the grid.
287275
name: Optional name of the data group.
288276
"""
@@ -292,12 +280,11 @@ class DataGroupGrid(BaseDTO):
292280
name: Optional[str] = None
293281

294282

295-
class DataGroupList(BaseDTO):
283+
class DataGroupCarousel(BaseDTO):
296284
"""Carousel layout for a data group.
297285
298286
Args:
299-
layout_type: Fixed value ``"default-list"`` identifying list layout.
300-
layout_contents: Ordered list of item UUIDs to display in the list.
287+
layout_contents: Ordered list of item UUIDs to display in the carousel.
301288
name: Optional name of the data group.
302289
"""
303290

@@ -306,11 +293,17 @@ class DataGroupList(BaseDTO):
306293
name: Optional[str] = None
307294

308295

296+
@deprecated(version=None, alternative="DataGroupCarousel")
297+
class DataGroupList(DataGroupCarousel):
298+
"""
299+
Deprecated, will be removed in a future release. Use `DataGroupCarousel` instead.
300+
"""
301+
302+
309303
class DataGroupCustom(BaseDTO):
310304
"""Custom layout for a data group.
311305
312306
Args:
313-
layout_type: Fixed value ``"custom"`` identifying custom layout.
314307
name: Optional name of the data group.
315308
layout_contents: Mapping from arbitrary keys to item UUIDs.
316309
layout: Arbitrary layout configuration structure.
@@ -324,7 +317,7 @@ class DataGroupCustom(BaseDTO):
324317
settings: Optional[Dict] = None
325318

326319

327-
DataGroupInput = Union[DataGroupGrid, DataGroupList, DataGroupCustom]
320+
DataGroupInput = Union[DataGroupGrid, DataGroupCarousel, DataGroupCustom]
328321

329322

330323
class CreateDataGroupPayload(BaseDTO):
@@ -333,10 +326,12 @@ class CreateDataGroupPayload(BaseDTO):
333326
Args:
334327
item_type: Item type of the group. Must be ``"GROUP"``.
335328
params: Layout and configuration for the data group.
329+
client_metadata: Optional custom metadata to be associated with the data group.
336330
"""
337331

338332
item_type: Literal["GROUP"] = "GROUP"
339333
params: DataGroupInput
334+
client_metadata: Optional[Dict[str, Any]] = None
340335

341336

342337
class UploadSignedUrlsPayload(BaseDTO):

encord/storage.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1232,12 +1232,16 @@ def move_to_folder(self, target_folder: Optional[Union["StorageFolder", UUID]])
12321232
def create_data_group(
12331233
self,
12341234
params: Union[DataGroupInput, List[UUID]],
1235+
*,
1236+
client_metadata: Optional[Dict[str, Any]] = None,
12351237
) -> UUID:
12361238
"""Creates a data group storage item in this folder.
12371239
12381240
Args:
12391241
params (Union[DataGroupInput, List[UUID]]): Parameters for the data group. When a list of UUIDs is provided,
12401242
the group will be created with a grid layout. For custom layouts, use DataGroupGrid, DataGroupList or DataGroupCustom.
1243+
client_metadata (Optional[Dict[str, Any]]): Optional custom metadata to be associated with the data group.
1244+
Should be a dictionary that is JSON-serializable.
12411245
12421246
Returns:
12431247
UUID: The UUID of the data group storage item.
@@ -1247,7 +1251,10 @@ def create_data_group(
12471251
return self._api_client.post(
12481252
f"storage/folders/{self.uuid}/create-group-item",
12491253
params=None,
1250-
payload=orm_storage.CreateDataGroupPayload(params=params),
1254+
payload=orm_storage.CreateDataGroupPayload(
1255+
params=params,
1256+
client_metadata=client_metadata,
1257+
),
12511258
result_type=UUID,
12521259
)
12531260

tests/common/test_deprecated.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import warnings
2+
3+
from encord.common.deprecated import deprecated
4+
5+
6+
def test_deprecated() -> None:
7+
class SampleClass:
8+
@deprecated(version="1.0", alternative="new_method")
9+
def method(self):
10+
return "method called"
11+
12+
@deprecated(version="1.0")
13+
class OldClass:
14+
pass
15+
16+
with warnings.catch_warnings(record=True) as w:
17+
obj = SampleClass()
18+
result = obj.method()
19+
20+
assert result == "method called"
21+
assert len(w) == 1
22+
assert issubclass(w[0].category, DeprecationWarning)
23+
assert "method is deprecated since version 1.0, use new_method instead" in str(w[0].message)
24+
25+
with warnings.catch_warnings(record=True) as w:
26+
OldClass()
27+
28+
assert len(w) == 1
29+
assert issubclass(w[0].category, DeprecationWarning)
30+
assert "OldClass is deprecated since version 1.0" in str(w[0].message)

tests/docs/data-groups-caro.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from encord.constants.enums import DataType
44
from encord.objects.metadata import DataGroupMetadata
5-
from encord.orm.storage import DataGroupList, StorageItemType
5+
from encord.orm.storage import DataGroupCarousel, StorageItemType
66
from encord.user_client import EncordUserClient
77

88
# --- Configuration ---
@@ -76,10 +76,10 @@
7676
# Add more groups as needed...
7777
]
7878

79-
# --- Create the data groups using default list layout ---
79+
# --- Create the data groups using default carousel layout ---
8080
for g in groups:
8181
group = folder.create_data_group(
82-
DataGroupList(
82+
DataGroupCarousel(
8383
name=g["name"],
8484
layout_contents=g["uuids"],
8585
)

0 commit comments

Comments
 (0)