Skip to content

Commit 1fb0107

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

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
@@ -8333,10 +8333,10 @@ def add_collection_item(
83338333
collection_slug (`str`):
83348334
Slug of the collection to update. Example: `"TheBloke/recent-models-64f9a55bb3115b4f513ec026"`.
83358335
item_id (`str`):
8336-
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"`)
8337-
or a paper id (e.g. `"2307.09288"`).
8336+
Id of the item to add to the collection. Use the repo_id for repos/spaces/datasets,
8337+
the paper id for papers, or the slug of another collection (e.g. `"moonshotai/kimi-k2"`).
83388338
item_type (`str`):
8339-
Type of the item to add. Can be one of `"model"`, `"dataset"`, `"space"` or `"paper"`.
8339+
Type of the item to add. Can be one of `"model"`, `"dataset"`, `"space"`, `"paper"` or `"collection"`.
83408340
note (`str`, *optional*):
83418341
A note to attach to the item in the collection. The maximum size for a note is 500 characters.
83428342
exists_ok (`bool`, *optional*):

tests/test_hf_api.py

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

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

40314033
# Check consistency
40324034
collection = self._api.get_collection(collection.slug)
4033-
self.assertEqual(len(collection.items), 2)
4035+
self.assertEqual(len(collection.items), 3)
40344036
self.assertEqual(collection.items[0].item_id, model_id)
40354037
self.assertEqual(collection.items[0].item_type, "model")
40364038
self.assertEqual(collection.items[0].note, "This is my model")
@@ -4039,6 +4041,9 @@ def test_collection_items(self) -> None:
40394041
self.assertEqual(collection.items[1].item_type, "dataset")
40404042
self.assertIsNone(collection.items[1].note)
40414043

4044+
self.assertEqual(collection.items[2].item_id, collection_id)
4045+
self.assertEqual(collection.items[2].item_type, "collection")
4046+
40424047
# Add existing item fails (except if ignore error)
40434048
with self.assertRaises(HfHubHTTPError):
40444049
self._api.add_collection_item(collection.slug, model_id, "model")
@@ -4065,7 +4070,7 @@ def test_collection_items(self) -> None:
40654070

40664071
# Check consistency
40674072
collection = self._api.get_collection(collection.slug)
4068-
self.assertEqual(len(collection.items), 1) # only 1 item remaining
4073+
self.assertEqual(len(collection.items), 2) # only 1 item remaining
40694074
self.assertEqual(collection.items[0].item_id, dataset_id) # position got updated
40704075

40714076
# Delete everything

0 commit comments

Comments
 (0)