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(weave): Make Dataset iterable #2928

Merged
merged 2 commits into from
Nov 7, 2024
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
10 changes: 10 additions & 0 deletions tests/trace/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,13 @@ def test_basic_dataset_lifecycle(client):
== list(dataset.rows)
== [{"a": 5, "b": 6}, {"a": 7, "b": 10}]
)


def test_dataset_iteration(client):
dataset = weave.Dataset(rows=[{"a": 5, "b": 6}, {"a": 7, "b": 10}])
rows = list(dataset)
assert rows == [{"a": 5, "b": 6}, {"a": 7, "b": 10}]

# Test that we can iterate multiple times
rows2 = list(dataset)
assert rows2 == rows
4 changes: 4 additions & 0 deletions weave/flow/dataset.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from collections.abc import Iterator
from typing import Any

from pydantic import field_validator
Expand Down Expand Up @@ -65,3 +66,6 @@ def convert_to_table(cls, rows: Any) -> weave.Table:
"Attempted to construct a Dataset row with an empty dict."
)
return rows

def __iter__(self) -> Iterator[dict]:
return iter(self.rows)
Loading