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

Workchains: Raise if if_/while_ predicate does not return boolean #259

Merged

Conversation

sphuber
Copy link
Collaborator

@sphuber sphuber commented Mar 7, 2023

Fixes #258

The if_ and while_ conditionals are constructed with a predicate. The interface expects the predicate to be a callable that returns a boolean, which if true, the body of the conditional is entered.

The problem is that the type of the value returned by the predicate was not explicitly checked, and any value that would evaluate as truthy would be accepted. This could potentially lead to unexpected behavior, such as an infinite loop for the while_ construct.

Here the _Conditional.is_true method is updated to explicitly check the type of the value returned by the predicate. If anything but a boolean is returned, a TypeError is raised.

@sphuber sphuber requested a review from unkcpz March 7, 2023 13:37
@sphuber sphuber force-pushed the fix/258/conditional-predicate-return-type-check branch from 93434b8 to e61637e Compare March 7, 2023 13:38
@codecov
Copy link

codecov bot commented Mar 7, 2023

Codecov Report

Patch and project coverage have no change.

Comparison is base (e5a0ce1) 90.74% compared to head (e5a0ce1) 90.74%.

❗ Current head e5a0ce1 differs from pull request most recent head bda08b3. Consider uploading reports for the commit bda08b3 to get more accurate results

Additional details and impacted files
@@               Coverage Diff               @@
##           support/0.21.x     #259   +/-   ##
===============================================
  Coverage           90.74%   90.74%           
===============================================
  Files                  21       21           
  Lines                2967     2967           
===============================================
  Hits                 2692     2692           
  Misses                275      275           

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

The `if_` and `while_` conditionals are constructed with a predicate.
The interface expects the predicate to be a callable that returns a
boolean, which if true, the body of the conditional is entered.

The problem is that the type of the value returned by the predicate was
not explicitly checked, and any value that would evaluate as truthy
would be accepted. This could potentially lead to unexpected behavior,
such as an infinite loop for the `while_` construct.

Here the `_Conditional.is_true` method is updated to explicitly check
the type of the value returned by the predicate. If anything but a
boolean is returned, a `TypeError` is raised.
Copy link
Member

@unkcpz unkcpz left a comment

Choose a reason for hiding this comment

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

Thanks @sphuber. It looks good to me.

@sphuber sphuber merged commit 800bcf1 into support/0.21.x Mar 9, 2023
@sphuber sphuber deleted the fix/258/conditional-predicate-return-type-check branch March 9, 2023 13:08
sphuber added a commit that referenced this pull request Mar 9, 2023
…259)

The `if_` and `while_` conditionals are constructed with a predicate.
The interface expects the predicate to be a callable that returns a
boolean, which if true, the body of the conditional is entered.

The problem is that the type of the value returned by the predicate was
not explicitly checked, and any value that would evaluate as truthy
would be accepted. This could potentially lead to unexpected behavior,
such as an infinite loop for the `while_` construct.

Here the `_Conditional.is_true` method is updated to explicitly check
the type of the value returned by the predicate. If anything but a
boolean is returned, a `TypeError` is raised.

Cherry-pick: 800bcf1
unkcpz pushed a commit to unkcpz/plumpy that referenced this pull request Dec 14, 2024
…iidateam#259)

The `if_` and `while_` conditionals are constructed with a predicate.
The interface expects the predicate to be a callable that returns a
boolean, which if true, the body of the conditional is entered.

The problem is that the type of the value returned by the predicate was
not explicitly checked, and any value that would evaluate as truthy
would be accepted. This could potentially lead to unexpected behavior,
such as an infinite loop for the `while_` construct.

Here the `_Conditional.is_true` method is updated to explicitly check
the type of the value returned by the predicate. If anything but a
boolean is returned, a `TypeError` is raised.

Cherry-pick: 800bcf1
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