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

Support __bool__ where sensible #2695

Merged
merged 3 commits into from Jul 13, 2022
Merged

Support __bool__ where sensible #2695

merged 3 commits into from Jul 13, 2022

Conversation

SimonHeybrock
Copy link
Member

Fixes #2691.

lib/python/bind_operators.h Outdated Show resolved Hide resolved
if (self.unit() != scipp::units::none)
throw scipp::except::UnitError(
"The thruth value of a variable with unit is undefined.");
return self.template value<bool>() == true;
Copy link
Member

Choose a reason for hiding this comment

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

Is this the part that makes sure we don't have more than one value in the self? self.value raises if the variable is not 0-D?

Copy link
Member Author

Choose a reason for hiding this comment

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

Correct.



def test_scalar_Variable_conversion_to_builtin_bool_bad_dtype():
var = sc.scalar(value=1.0, unit=None)
Copy link
Member

Choose a reason for hiding this comment

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

Out of interest, do we support conversion from integer? e.g. sc.scalar(value=1, unit=None)

Copy link
Member Author

Choose a reason for hiding this comment

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

No, because we use .value<bool>().

Co-authored-by: Neil Vaytet <39047984+nvaytet@users.noreply.github.com>
if (self.unit() != scipp::units::none)
throw scipp::except::UnitError(
"The thruth value of a variable with unit is undefined.");
return self.template value<bool>() == true;
Copy link
Member

Choose a reason for hiding this comment

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

Missing check for variances?

Copy link
Member Author

Choose a reason for hiding this comment

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

bool cannot have variances.

Copy link
Member

Choose a reason for hiding this comment

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

Right

@SimonHeybrock SimonHeybrock merged commit d2c8eba into main Jul 13, 2022
@SimonHeybrock SimonHeybrock deleted the support-builtin-bool branch July 13, 2022 07:08
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.

Support __bool__ in some cases?
3 participants