Skip to content

Commit ec95611

Browse files
[Collections] Add collections to collections by slug id (#3551)
* make it more explicit that collections can be added to a collection * docstring
1 parent c48b70a commit ec95611

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

src/huggingface_hub/hf_api.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8352,10 +8352,10 @@ def add_collection_item(
83528352
collection_slug (`str`):
83538353
Slug of the collection to update. Example: `"TheBloke/recent-models-64f9a55bb3115b4f513ec026"`.
83548354
item_id (`str`):
8355-
ID of the item to add to the collection. It can be the ID of a repo on the Hub (e.g. `"facebook/bart-large-mnli"`)
8356-
or a paper id (e.g. `"2307.09288"`).
8355+
Id of the item to add to the collection. Use the repo_id for repos/spaces/datasets,
8356+
the paper id for papers, or the slug of another collection (e.g. `"moonshotai/kimi-k2"`).
83578357
item_type (`str`):
8358-
Type of the item to add. Can be one of `"model"`, `"dataset"`, `"space"` or `"paper"`.
8358+
Type of the item to add. Can be one of `"model"`, `"dataset"`, `"space"`, `"paper"` or `"collection"`.
83598359
note (`str`, *optional*):
83608360
A note to attach to the item in the collection. The maximum size for a note is 500 characters.
83618361
exists_ok (`bool`, *optional*):

tests/test_hf_api.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4020,15 +4020,17 @@ def test_collection_items(self) -> None:
40204020
# Create some repos
40214021
model_id = self._api.create_repo(repo_name()).repo_id
40224022
dataset_id = self._api.create_repo(repo_name(), repo_type="dataset").repo_id
4023+
collection_id = self._api.create_collection("nested collection", exists_ok=True).slug
40234024

40244025
# Create collection + add items to it
40254026
collection = self._api.create_collection(self.title)
40264027
self._api.add_collection_item(collection.slug, model_id, "model", note="This is my model")
40274028
self._api.add_collection_item(collection.slug, dataset_id, "dataset") # note is optional
4029+
self._api.add_collection_item(collection.slug, collection_id, "collection")
40284030

40294031
# Check consistency
40304032
collection = self._api.get_collection(collection.slug)
4031-
self.assertEqual(len(collection.items), 2)
4033+
self.assertEqual(len(collection.items), 3)
40324034
self.assertEqual(collection.items[0].item_id, model_id)
40334035
self.assertEqual(collection.items[0].item_type, "model")
40344036
self.assertEqual(collection.items[0].note, "This is my model")
@@ -4037,6 +4039,9 @@ def test_collection_items(self) -> None:
40374039
self.assertEqual(collection.items[1].item_type, "dataset")
40384040
self.assertIsNone(collection.items[1].note)
40394041

4042+
self.assertEqual(collection.items[2].item_id, collection_id)
4043+
self.assertEqual(collection.items[2].item_type, "collection")
4044+
40404045
# Add existing item fails (except if ignore error)
40414046
with self.assertRaises(HfHubHTTPError):
40424047
self._api.add_collection_item(collection.slug, model_id, "model")
@@ -4063,7 +4068,7 @@ def test_collection_items(self) -> None:
40634068

40644069
# Check consistency
40654070
collection = self._api.get_collection(collection.slug)
4066-
self.assertEqual(len(collection.items), 1) # only 1 item remaining
4071+
self.assertEqual(len(collection.items), 2) # only 1 item remaining
40674072
self.assertEqual(collection.items[0].item_id, dataset_id) # position got updated
40684073

40694074
# Delete everything

0 commit comments

Comments
 (0)