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 BooleanArtifact #905

Merged
merged 6 commits into from
Jun 28, 2024
Merged

Add BooleanArtifact #905

merged 6 commits into from
Jun 28, 2024

Conversation

vachillo
Copy link
Member

@vachillo vachillo commented Jun 26, 2024

artifact that is explicitly True or False.

i want to output a boolean from a Task and do something with that information later, and checking the string value of a TextArtifact is clunky.

incremental update to support #904

return BooleanArtifact(True)
elif value.lower() == "false":
return BooleanArtifact(False)
raise ValueError(f"Cannot convert string literal '{value}' to BooleanArtifact")
Copy link
Member Author

Choose a reason for hiding this comment

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

should this return None or raise an exception?

Copy link
Contributor

Choose a reason for hiding this comment

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

I vote for raise an exception.

If the user's code assumes that the parse always succeeds and then it fails, returning None will (most likely) cause an exception to be raised by code that consumes this output. Then it will be the user's responsibility to trace it back to here, so we might as well do it for them.


@define
class BooleanArtifact(BaseArtifact):
value: bool = field(converter=bool, metadata={"serializable": True})
Copy link
Member Author

Choose a reason for hiding this comment

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

passing in a value on init will get coerced into a bool, whereas using parse_bool will check for the literal true or false. should that check be done on init instead?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think having these two methods of creating a BooleanArtifact seems reasonable. The default (init) way being a little more flexible, but still offering a stricter option for strings specifically.

If you don't offer the bool coercion option and the use case comes up, then what would that look like? like this: BooleanArtifact(bool(x))? I suppose that's easy enough, but BooleanArtifact(x) is a little better. Actually, the parse_bool wouldn't work for a literal True or False, so I definitely vote for keeping both creation options.

Copy link
Member Author

Choose a reason for hiding this comment

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

the init method will always convert the input to a boolean. this can lead to suprising behavior sometimes where BooleanArtifact("False").value is True. parse_bool is to parse string literal values as a convenience. thinking about it now, seems more "pythonic" to keep the init as to whatever python interprets as a bool, and use parse_bool to parse string literal values as a convenience

@vachillo vachillo marked this pull request as ready for review June 26, 2024 22:17
Copy link

codecov bot commented Jun 26, 2024

Codecov Report

Attention: Patch coverage is 95.83333% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
griptape/artifacts/boolean_artifact.py 95.65% 0 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

Copy link
Contributor

@dylanholmes dylanholmes left a comment

Choose a reason for hiding this comment

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

Nice work!

I know there's not much to test, but wanna add unit tests?

return BooleanArtifact(True)
elif value.lower() == "false":
return BooleanArtifact(False)
raise ValueError(f"Cannot convert string literal '{value}' to BooleanArtifact")
Copy link
Contributor

Choose a reason for hiding this comment

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

I vote for raise an exception.

If the user's code assumes that the parse always succeeds and then it fails, returning None will (most likely) cause an exception to be raised by code that consumes this output. Then it will be the user's responsibility to trace it back to here, so we might as well do it for them.


@define
class BooleanArtifact(BaseArtifact):
value: bool = field(converter=bool, metadata={"serializable": True})
Copy link
Contributor

Choose a reason for hiding this comment

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

I think having these two methods of creating a BooleanArtifact seems reasonable. The default (init) way being a little more flexible, but still offering a stricter option for strings specifically.

If you don't offer the bool coercion option and the use case comes up, then what would that look like? like this: BooleanArtifact(bool(x))? I suppose that's easy enough, but BooleanArtifact(x) is a little better. Actually, the parse_bool wouldn't work for a literal True or False, so I definitely vote for keeping both creation options.

@vachillo vachillo merged commit 3ee8c35 into dev Jun 28, 2024
11 checks passed
@vachillo vachillo deleted the boolean branch June 28, 2024 16:50
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