Skip to content

Commit b7bf565

Browse files
feat!(project): Remove "put-several" mechanic
This is for several reasons: - it is not as explicit as several simple `put` calls: the mechanic is ambiguous with regards to atomicity (if a key-value pair is invalid, does it make the whole operation fail?) - the mechanic makes it complicated to add options to `put`, e.g. the `note` option proposed in #1041 or the `display_as` option proposed in #1045 (comment).
1 parent 947eee5 commit b7bf565

File tree

2 files changed

+7
-74
lines changed

2 files changed

+7
-74
lines changed

skore/src/skore/project/project.py

Lines changed: 7 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -80,44 +80,18 @@ def __init__(
8080
self.item_repository = item_repository
8181
self.view_repository = view_repository
8282

83-
def put(self, key: Union[str, dict[str, Any]], value: Optional[Any] = MISSING):
84-
"""Add one or more key-value pairs to the Project.
83+
def put(self, key: str, value: Any):
84+
"""Add a key-value pair to the Project.
8585
8686
If an item with the same key already exists, its value is replaced by the new
8787
one.
8888
89-
If ``key`` is a string, then :func:`~skore.Project.put` adds the single
90-
``key``-``value`` pair mapping to the Project.
91-
If ``key`` is a dict, it is interpreted as multiple key-value pairs to add to
92-
the Project.
93-
94-
The dict format is equivalent to running :func:`~skore.Project.put`
95-
for each individual key-value pair. In other words,
96-
97-
.. code-block:: python
98-
99-
project.put({"hello": 1, "goodbye": 2})
100-
101-
is equivalent to
102-
103-
.. code-block:: python
104-
105-
project.put("hello", 1)
106-
project.put("goodbye", 2)
107-
108-
In particular, this means that if some key-value pair is invalid
109-
(e.g. if a key is not a string, or a value's type is not supported),
110-
then all the key-value pairs up to the first offending key-value pair will
111-
be successfully inserted, *and then* an error will be raised.
112-
11389
Parameters
11490
----------
115-
key : str | dict[str, Any]
116-
The key to associate with ``value`` in the Project,
117-
or dict of key-value pairs to add to the Project.
91+
key : str
92+
The key to associate with ``value`` in the Project.
11893
value : Any, optional
11994
The value to associate with ``key`` in the Project.
120-
If ``key`` is a dict, this argument is ignored.
12195
12296
Raises
12397
------
@@ -127,21 +101,10 @@ def put(self, key: Union[str, dict[str, Any]], value: Optional[Any] = MISSING):
127101
NotImplementedError
128102
If the value type is not supported.
129103
"""
130-
if value is not MISSING:
131-
key_to_item = {key: value}
132-
elif isinstance(key, dict):
133-
key_to_item = key
134-
else:
135-
raise TypeError(
136-
f"Bad parameters. "
137-
f"When value is not specified, key must be a dict (found '{type(key)}')"
138-
)
139-
140-
for key, value in key_to_item.items():
141-
if not isinstance(key, str):
142-
raise TypeError(f"Key must be a string (found '{type(key)}')")
104+
if not isinstance(key, str):
105+
raise TypeError(f"Key must be a string (found '{type(key)}')")
143106

144-
self.item_repository.put_item(key, object_to_item(value))
107+
self.item_repository.put_item(key, object_to_item(value))
145108

146109
def put_item(self, key: str, item: Item):
147110
"""Add an Item to the Project."""

skore/tests/unit/project/test_project.py

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -218,36 +218,6 @@ def test_list_view_keys(in_memory_project):
218218
assert in_memory_project.list_view_keys() == ["view"]
219219

220220

221-
def test_put_several_happy_path(in_memory_project):
222-
in_memory_project.put({"a": "foo", "b": "bar"})
223-
assert in_memory_project.list_item_keys() == ["a", "b"]
224-
225-
226-
def test_put_several_some_errors(in_memory_project):
227-
with pytest.raises(TypeError):
228-
in_memory_project.put({0: "hello", 1: "hello", 2: "hello"})
229-
assert in_memory_project.list_item_keys() == []
230-
231-
232-
def test_put_several_nested(in_memory_project):
233-
in_memory_project.put({"a": {"b": "baz"}})
234-
assert in_memory_project.list_item_keys() == ["a"]
235-
assert in_memory_project.get("a") == {"b": "baz"}
236-
237-
238-
def test_put_several_error(in_memory_project):
239-
"""If some key-value pairs are wrong, add all that are valid and print a warning."""
240-
with pytest.raises(NotImplementedError):
241-
in_memory_project.put(
242-
{
243-
"a": "foo",
244-
"b": (lambda: "unsupported object"),
245-
}
246-
)
247-
248-
assert in_memory_project.list_item_keys() == ["a"]
249-
250-
251221
def test_put_key_is_a_tuple(in_memory_project):
252222
"""If key is not a string, warn."""
253223
with pytest.raises(TypeError):

0 commit comments

Comments
 (0)