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

Do not remove None values in RepoCardData serialization #2626

Merged
merged 2 commits into from
Oct 22, 2024
Merged
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
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
Loading