Skip to content
This repository has been archived by the owner on Sep 27, 2024. It is now read-only.

Prevent empty fields from rendering as "None" #251

Merged
merged 3 commits into from
Dec 16, 2022

Conversation

codesue
Copy link
Contributor

@codesue codesue commented Dec 2, 2022

What does this pull request do?

This change fixes a bug that made empty objects render as "None" by:

  • defining a __len__() for model card fields that ignores None values recursively when counting items, meaning empty fields and nested empty fields will be falsy
  • adding if statements to default templates as needed

This also fixes a bug where nested empty were being serialized when writing a model card to a proto since they were truthy.

Example usage of len() and bool():

>>> from model_card_toolkit.model_card import ModelCard
>>> model_card = ModelCard()
>>> len(model_card)
0
>>> bool(model_card)
False
>>> model_card.model_details.name = "Placeholder"
>>> len(model_card)
1
>>> bool(model_card)
True

If you're recreating a model card from a proto, use if model_card is None instead of if not model_card to test whether the operation was successful if you want to fail on None but not on ModelCard().

Fixes #213

How did you test this change?

Screenshot from the offical MLMD Model Card Toolkit Demo:

mct-213-table-bottom-before

Screenshot from running the demo notebook with this fix implemented:

mct-213-table-bottom-after

How did you document this change?

Docstrings

Before submitting

Before submitting a pull request, please be sure to do the following:

  • Read the How to Contribute guide if this is your first contribution.
  • Open an issue or discussion topic to discuss this change.
  • Write new tests if applicable.
  • Update documentation if applicable.

@codesue codesue marked this pull request as ready for review December 2, 2022 17:44
@codesue
Copy link
Contributor Author

codesue commented Dec 16, 2022

Hi @casassg, @deutranium, or @hanneshapke, would you please review this fix when you get a chance. 🙏

@codesue codesue merged commit f24c2c7 into tensorflow:master Dec 16, 2022
@codesue codesue deleted the codesue/213-prevent-rendering-none branch December 16, 2022 03:45
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Prevent empty objects from rendering as "None"
2 participants