Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(project.put): Add note argument #1101

Merged
merged 1 commit into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions skore/src/skore/project/project.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Define a Project."""

import logging
from typing import Any, Union
from typing import Any, Optional, Union

from skore.item import (
CrossValidationItem,
Expand Down Expand Up @@ -77,7 +77,7 @@ def __init__(
self.item_repository = item_repository
self.view_repository = view_repository

def put(self, key: str, value: Any):
def put(self, key: str, value: Any, *, note: Optional[str] = None):
"""Add a key-value pair to the Project.

If an item with the same key already exists, its value is replaced by the new
Expand All @@ -89,6 +89,8 @@ def put(self, key: str, value: Any):
The key to associate with ``value`` in the Project.
value : Any
The value to associate with ``key`` in the Project.
note : str or None, optional
A note to attach with the item.

Raises
------
Expand All @@ -101,7 +103,14 @@ def put(self, key: str, value: Any):
if not isinstance(key, str):
raise TypeError(f"Key must be a string (found '{type(key)}')")

self.item_repository.put_item(key, object_to_item(value))
item = object_to_item(value)

if note is not None:
if not isinstance(note, str):
raise TypeError(f"Note must be a string (found '{type(note)}')")
item.note = note

self.item_repository.put_item(key, item)

def put_item(self, key: str, item: Item):
"""Add an Item to the Project."""
Expand Down
18 changes: 18 additions & 0 deletions skore/tests/unit/project/test_notes.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,21 @@ def test_delete_note_no_key(in_memory_project):
def test_delete_note_no_note(in_memory_project):
in_memory_project.put("key", "hello")
assert in_memory_project.get_note("key") is None


def test_put_with_note(in_memory_project):
in_memory_project.put("key", "hello", note="note")
assert in_memory_project.get_note("key") == "note"


def test_put_with_note_annotates_latest(in_memory_project):
"""Adding the `note` argument annotates the latest version of the item."""
in_memory_project.put("key", "hello")
in_memory_project.put("key", "goodbye", note="note")
assert in_memory_project.get_note("key", version=0) is None


def test_put_with_note_wrong_type(in_memory_project):
"""Adding the `note` argument annotates the latest version of the item."""
with pytest.raises(TypeError):
in_memory_project.put("key", "hello", note=0)
Loading