Skip to content

Commit

Permalink
Do not remove None values in RepoCardData serialization (#2626)
Browse files Browse the repository at this point in the history
* Do not remove None values in RepoCardData serialization

* style
  • Loading branch information
Wauplin authored Oct 22, 2024
1 parent ab0b386 commit 40a64c8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/huggingface_hub/repocard_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def to_dict(self):

data_dict = copy.deepcopy(self.__dict__)
self._to_dict(data_dict)
return _remove_none(data_dict)
return {key: value for key, value in data_dict.items() if value is not None}

def _to_dict(self, data_dict):
"""Use this method in child classes to alter the dict representation of the data. Alter the dict in-place.
Expand Down
13 changes: 13 additions & 0 deletions tests/test_repocard_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,19 @@ def test_model_card_unique_tags(self):
data = ModelCardData(tags=["tag2", "tag1", "tag2", "tag3"])
assert data.tags == ["tag2", "tag1", "tag3"]

def test_remove_top_level_none_values(self):
as_obj = ModelCardData(tags=["tag1", None], foo={"bar": 3, "baz": None}, pipeline_tag=None)
as_dict = as_obj.to_dict()

assert as_obj.tags == ["tag1", None]
assert as_dict["tags"] == ["tag1", None] # none value inside list should be kept

assert as_obj.foo == {"bar": 3, "baz": None}
assert as_dict["foo"] == {"bar": 3, "baz": None} # none value inside dict should be kept

assert as_obj.pipeline_tag is None
assert "pipeline_tag" not in as_dict # top level none value should be removed


class DatasetCardDataTest(unittest.TestCase):
def test_train_eval_index_keys_updated(self):
Expand Down

0 comments on commit 40a64c8

Please sign in to comment.