From 40a64c84ed085481d83d00fae1ae416fc4493c8e Mon Sep 17 00:00:00 2001 From: Lucain Date: Tue, 22 Oct 2024 16:42:43 +0200 Subject: [PATCH] Do not remove None values in RepoCardData serialization (#2626) * Do not remove None values in RepoCardData serialization * style --- src/huggingface_hub/repocard_data.py | 2 +- tests/test_repocard_data.py | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/huggingface_hub/repocard_data.py b/src/huggingface_hub/repocard_data.py index 855d3a1f13..fb2c6e8f96 100644 --- a/src/huggingface_hub/repocard_data.py +++ b/src/huggingface_hub/repocard_data.py @@ -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. diff --git a/tests/test_repocard_data.py b/tests/test_repocard_data.py index 51b1601239..5d7052fc6b 100644 --- a/tests/test_repocard_data.py +++ b/tests/test_repocard_data.py @@ -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):