-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Starlark: Disallow conversion of select
to bool
#14506
Comments
I appreciate your point. I believe core Starlark devs are very reluctant to deviate from Pythonic standards. |
We can't do this currently in the Starlark language because all types must be truth-able. Filed bazelbuild/starlark#211 for the language implications.
We don't necessarily need to support it just because Python does. For example, we deviate to be more restrictive than Python in other situations, e.g. implicit string concatenation ( I think the best approach is to just carefully document this trap, and consider this problem alongside the more general issues we have with navigating the API of |
Just re-emphasizing (@criemen already noted) that we document the current behavior at https://bazel.build/docs/configurable-attributes#faq-boolean-select. |
Thank you for contributing to the Bazel repository! This issue has been marked as stale since it has not had any activity in the last 1+ years. It will be closed in the next 90 days unless any other activity occurs or one of the following labels is added: "not stale", "awaiting-bazeler". Please reach out to the triage team ( |
Update in late Q1 2024: We've decided that we should ban implicit conversion of I'm not sure if there's any corner cases that might surprise us as a consequence of having an non-automatically-boolable type, but it seems like it shouldn't be a big concern. The motivation for this is mainly to allow the upcoming symbolic macros feature to (#11992) to automatically promote raw values to Probably targeting Q2. |
Description of the problem / feature request:
The return type of
select
converts silently in truthy contexts toTrue
.This can lead to the following code being legal, but not doing what the programmer intended to do:
This can lead to behavior that bazel novices would think of as unexpected (the if always evaluates to the true branch).
Instead, bazel should emit a warning or an error whenever the return value of select gets converted to a bool.
I cannot imagine any valid use case where the return of a select should be converted to a truth value (it's always
True
anyways), so taking this behavior away, while a breaking change, would be an improvement for bazel.Feature requests: what underlying problem are you trying to solve with this feature?
Improve the user experience for a more confusing part of starlark (selects), by providing a clear error on a beginner mistake.
Have you found anything relevant by searching the web?
The documentation calls this starlark behavior out, but I believe that in this case, starlark should diverge from the pythonic way that everything silently converts to bool without fail.
The reasoning for that is mainly that understanding
select
and the bazel/starlark execution semantics is difficult enough as-is, and this change would remove a pitfall.The text was updated successfully, but these errors were encountered: