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

Only try to set flag_value if is_flag is true #2829

Merged
merged 2 commits into from
Dec 22, 2024

Conversation

lpsinger
Copy link
Contributor

This statement:

flag_value = not self.default

requires the class to have a working __bool__ method. However, there are some classes that explicitly disable this method:

  • numpy.ndarray raises ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
  • astropy.units.Quantity raises ValueError: Quantity truthiness is ambiguous, especially for logarithmic units and temperatures. Use explicit comparisons.

Move this statement so that it is only executed if is_flag is true.

This statement:

    flag_value = not self.default

requires the class to have a working `__bool__` method. However,
there are some classes that explicitly disable this method:

- [`numpy.ndarray`](https://numpy.org/doc/stable/reference/arrays.ndarray.html)
  raises `ValueError: The truth value of an array with more than
  one element is ambiguous. Use a.any() or a.all()`
- [`astropy.units.Quantity`](https://docs.astropy.org/en/stable/api/astropy.units.Quantity.html)
  raises `ValueError: Quantity truthiness is ambiguous, especially
  for logarithmic units and temperatures. Use explicit  comparisons.`

Move this statement so that it is only executed if `is_flag` is
true.
@lpsinger
Copy link
Contributor Author

See, for example, m4opt/m4opt#220.

@AndreasBackx
Copy link
Collaborator

This seems to make sense. Thank you! Here it would make sense to have some typing to clarify this requirement. Though that's out of scope for this PR.

@AndreasBackx AndreasBackx merged commit b5464b7 into pallets:main Dec 22, 2024
12 checks passed
@AndreasBackx AndreasBackx added this to the 8.2.0 milestone Dec 22, 2024
@lpsinger
Copy link
Contributor Author

lpsinger commented Dec 22, 2024

Thanks for the speedy review, and happy holidays!

@lpsinger lpsinger deleted the flag-value-none branch December 22, 2024 20:31
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 6, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants