Skip to content

Commit

Permalink
feat: implement additional parameters in Metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
unexcellent committed Nov 12, 2024
1 parent 838b7f4 commit 3f51b48
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
8 changes: 7 additions & 1 deletion raillabel/format/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Metadata:
@classmethod
def from_json(cls, json: JSONMetadata) -> Metadata:
"""Construct an instant of this class from RailLabel JSON data."""
return Metadata(
metadata = Metadata(
schema_version=json.schema_version,
name=json.name,
subschema_version=json.subschema_version,
Expand All @@ -49,3 +49,9 @@ def from_json(cls, json: JSONMetadata) -> Metadata:
annotator=json.annotator,
comment=json.comment,
)

if json.model_extra is not None:
for extra_field, extra_value in json.model_extra.items():
setattr(metadata, extra_field, extra_value)

return metadata
4 changes: 2 additions & 2 deletions raillabel/json_format/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

from typing import Literal

from pydantic import BaseModel
from pydantic import BaseModel, Extra


class JSONMetadata(BaseModel):
class JSONMetadata(BaseModel, extra=Extra.allow):
"""Metadata about the annotation file itself."""

schema_version: Literal["1.0.0"]
Expand Down
14 changes: 14 additions & 0 deletions tests/test_raillabel/format/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,19 @@ def test_from_json(metadata, metadata_json):
assert actual == metadata


def test_from_json__extra_fields():
json_metadata = JSONMetadata(
**{
"schema_version": "1.0.0",
"ADDITIONAL_STR": "SOME_VALUE",
"ADDITIONAL_OBJECT": {"first_field": 2, "second_field": [1, 2, 3]},
}
)

actual = Metadata.from_json(json_metadata)
assert actual.ADDITIONAL_STR == "SOME_VALUE"
assert actual.ADDITIONAL_OBJECT == {"first_field": 2, "second_field": [1, 2, 3]}


if __name__ == "__main__":
pytest.main([__file__, "-v"])

0 comments on commit 3f51b48

Please sign in to comment.