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

Add .from_proto() and .from_json() class methods #278

Merged
merged 1 commit into from
May 21, 2023

Conversation

codesue
Copy link
Collaborator

@codesue codesue commented May 21, 2023

What does this pull request do?

This pull request adds BaseModelCardField.from_proto() and makes ModelCard().from_json() a class method. It deprecates but doesn't remove BaseModelCardField.copy_from_proto().

These are commonly used as alternative constructors, but in a roundabout way:

model_card = ModelCard()
model_card.from_json(model_card_json)

owner = Owner()
owner.copy_from_proto(owner_proto)

Additionally, both .from_json() and .copy_from_proto() call .clear() before copying contents from the JSON/proto into these empty objects.

New usage:

model_card = ModelCard.from_json(model_card_json)

owner = Owner.from_proto(owner_proto)

Relates to #276

How did you test this change?

Updated tests and ran pytest model_card_toolkit.

How did you document this change?

Updated 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.

def from_json(self, json_dict: Dict[str, Any]) -> None:
def merge_from_json(self, json: Union[Dict[str, Any], str]) -> 'ModelCard':
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved the definition of from_json below merge_from_json, so this diff looks strange.

@@ -55,7 +59,7 @@ def to_proto(self) -> message.Message:
for field_name, field_value in self.__dict__.items():
if not hasattr(proto, field_name):
raise ValueError(
"%s has no such field named '%s'." % (type(proto), field_name)
'%s has no such field named "%s".' % (type(proto), field_name)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the swap from " to ' due to an editor config from your side? Should we define one way in yapf to keep it consistent?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it's not from an editor config: mct mostly uses single quotes, and I default to single quotes here like in other tf projects. I switched to single here when yapf complained about the mixing, but I didn't update all other files.

Great idea! Yes, let's define single quotes in yapf.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created #279

@codesue codesue merged commit dffe074 into tensorflow:main May 21, 2023
@codesue codesue deleted the codesue/classmethod-constructors branch May 21, 2023 22:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants