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

Make predict methods in ZeroShot pipeline return Result instead of panicking on unwrap #301

Merged
merged 10 commits into from
Dec 4, 2022

Conversation

anna-hope
Copy link
Contributor

@anna-hope anna-hope commented Nov 21, 2022

Add checked prediction methods to ZeroShotClassificationModel.
These methods return Result to allow callers to implement appropriate error handling logic.

I named these methods try_ by analogy with try_ methods in HashMap and elsewhere in the standard library.

This closes #297 for ZeroShotClassificationModel.

Anna Melnikov and others added 6 commits November 19, 2022 19:31
- Add checked prediction methods to ZeroShotClassificationModel.
These methods return Option and convert any underlying errors into None,
to allow callers to implement appropriate error handling logic.
Rename *_checked methods into try_* methods.
This is more idiomatic vis-a-vis the Rust standard library.
@anna-hope anna-hope changed the title Add checked prediction methods Add try_predict* methods to ZeroShotPipeline Nov 23, 2022
@anna-hope anna-hope changed the title Add try_predict* methods to ZeroShotPipeline Add try_predict* methods to ZeroShot pipeline Nov 23, 2022
Copy link
Owner

@guillaume-be guillaume-be left a comment

Choose a reason for hiding this comment

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

Thank you very much for the changes @anna-hope , this looks good.
I think it would be OK to change the signature of the current predict and predict_multilabel to return a Result rather than having multiple methods (that just unwrap for the user). The next release will introduce some breaking changes already. I added one minor comment to move the Option to Error conversion to the method that would produce an invalid output (this also saves code duplication since it is called twice)

src/pipelines/zero_shot_classification.rs Show resolved Hide resolved
src/pipelines/zero_shot_classification.rs Outdated Show resolved Hide resolved
Anna Melnikov added 2 commits November 29, 2022 17:03
Change return type of ZeroShotClassificationModel.prepare_for_model
from option into Result. This simplifies the code, and returns
the error closer to its origin.

This addresses comments from @guillaume-be.
@anna-hope
Copy link
Contributor Author

@guillaume-be Thank you very much for your feedback! I addressed your comments. This did indeed result in cleaner code.

@anna-hope anna-hope changed the title Add try_predict* methods to ZeroShot pipeline Make predict methods in ZeroShot pipeline return Result instead of panicking on unwrap Nov 30, 2022
@guillaume-be guillaume-be merged commit a34cf9f into guillaume-be:master Dec 4, 2022
Miezhiko pushed a commit to Masha/rust-bert that referenced this pull request Mar 21, 2023
…nicking on unwrap (guillaume-be#301)

* Add checked prediction methods

- Add checked prediction methods to ZeroShotClassificationModel.
These methods return Option and convert any underlying errors into None,
to allow callers to implement appropriate error handling logic.

* Update ZeroShot example to use checked method.

* Add tests for ZeroShot checked methods

* Change checked prediction methods to return Result

* refactor: rename *_checked into try_*

Rename *_checked methods into try_* methods.
This is more idiomatic vis-a-vis the Rust standard library.

* refactor: remove try_ prefix from predict methods

* refactor: change return from Option to Result

Change return type of ZeroShotClassificationModel.prepare_for_model
from option into Result. This simplifies the code, and returns
the error closer to its origin.

This addresses comments from @guillaume-be.

* refactor: address clippy lints in tests

Co-authored-by: guillaume-be <guillaume.becquin@gmail.com>
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.

Avoid panics in prediction code
2 participants