Skip to content

Commit 40a64c8

Browse files
authored
Do not remove None values in RepoCardData serialization (#2626)
* Do not remove None values in RepoCardData serialization * style
1 parent ab0b386 commit 40a64c8

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/huggingface_hub/repocard_data.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ def to_dict(self):
185185

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

190190
def _to_dict(self, data_dict):
191191
"""Use this method in child classes to alter the dict representation of the data. Alter the dict in-place.

tests/test_repocard_data.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,19 @@ def test_model_card_unique_tags(self):
237237
data = ModelCardData(tags=["tag2", "tag1", "tag2", "tag3"])
238238
assert data.tags == ["tag2", "tag1", "tag3"]
239239

240+
def test_remove_top_level_none_values(self):
241+
as_obj = ModelCardData(tags=["tag1", None], foo={"bar": 3, "baz": None}, pipeline_tag=None)
242+
as_dict = as_obj.to_dict()
243+
244+
assert as_obj.tags == ["tag1", None]
245+
assert as_dict["tags"] == ["tag1", None] # none value inside list should be kept
246+
247+
assert as_obj.foo == {"bar": 3, "baz": None}
248+
assert as_dict["foo"] == {"bar": 3, "baz": None} # none value inside dict should be kept
249+
250+
assert as_obj.pipeline_tag is None
251+
assert "pipeline_tag" not in as_dict # top level none value should be removed
252+
240253

241254
class DatasetCardDataTest(unittest.TestCase):
242255
def test_train_eval_index_keys_updated(self):

0 commit comments

Comments
 (0)