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

Fix recursively_populate_jsonld failing when given array values with non-dict elements #708

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 4 additions & 1 deletion python/mlcroissant/mlcroissant/_src/core/json_ld.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,10 @@ def box_singleton_list(element: Any) -> list[Any] | None:

def recursively_populate_jsonld(entry_node: Json, id_to_node: dict[str, Json]) -> Any:
"""Changes in place `entry_node` with its children."""
if "@value" in entry_node:
if not isinstance(entry_node, dict):
# If entry_node is not dict, just return it without doing anything.
return entry_node
elif "@value" in entry_node:
if entry_node.get("@type") == namespace.RDF.JSON:
# Stringified JSON is loaded as a dict.
return json.loads(entry_node["@value"])
Expand Down
16 changes: 16 additions & 0 deletions python/mlcroissant/mlcroissant/_src/core/json_ld_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
import json

from etils import epath
import pytest

from mlcroissant._src.core.json_ld import recursively_populate_jsonld
from mlcroissant._src.core.rdf import make_context
from mlcroissant._src.core.types import Json
from mlcroissant._src.datasets import Dataset
from mlcroissant._src.tests.versions import parametrize_version

Expand Down Expand Up @@ -68,3 +71,16 @@ def test_make_context():
"transform": "cr:transform",
"foo": "bar",
}


@pytest.mark.parametrize(
["data"],
[
[{}],
[{"value": 3}],
[{"singelton_array": ["a"]}],
[{"array": ["a", "b", "c"]}],
],
)
def test_sanity_recursively_populate_jsonld(data: Json) -> None:
recursively_populate_jsonld(data, {})
Loading