-
Notifications
You must be signed in to change notification settings - Fork 164
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
Add a zero-argument form of dir()
to list all visible names
#218
Comments
(Alternative proposals that satisfy the use cases are welcome - @kkress suggested an |
In our setup we have I guess you meant The issue with no-argument
optimizer can replace it with:
However, if code is:
the optimization cannot be done, because analysing this middle statement is virtually impossible. Basically, many optimizations need to disabled if there's no-argument It is even worse than that. Consider this example:
If there's no-argument I was thinking, Starlark may need to have |
I feel like we discussed this in the past, but I can't find the thread. I initially thought this seemed reasonable but @stepancheg reminded me of the reason this is problematic: static scope resolution. Consider:
If foo is predeclared, then the print statement resolves to the predeclared binding, the condition is true, and the value of foo is printed. But if foo is not predeclared, then print(foo) gives an 'undefined' error. Stepan suggests
It would be best to specify that the
'native' isn't part of the Starlark spec; it's a Bazelism. You're right that it has different fields in different environments. [Edited: changed to struct, not dict] |
In general, the set of builtins should be fairly stable. I don't recommend trying to make the same code compatible with multiple languages, or multiple versions of a language. Things are harder to test, analyze, and reason about. Starlark was designed to be much more static than Python. This kind of feature request is like trying to use Starlark in a dynamic way - and it's going to conflict, as mentioned in the previous comments. A Bazel project can specify which version is expected, e.g. in the ̀.bazelversion` file. Use the new features when you've updated your version of Bazel. When new features are added as fields on an object (e.g. (a previous related discussion for Bazel was here: bazelbuild/bazel#7428) |
A feature like this would have come in handy for me a few months ago. I was adding a new global symbol to Google's internal build, and it was effectively impossible to write Starlark code in a forward and backwards compatible manner. I had to wait about a month for a new release to land before I could go any further. Really, I would view this feature as a way for Starklark code not bundled with Bazel to be able to be compatible with multiple versions of Bazel. As it is today, if Bazel added a new global symbol, then a Starlark library simply can't use it until they're ready to entirely drop support and break all prior Bazel versions. This is rather overkill, especially for new features that are additive, and wouldn't otherwise invalidate supporting prior Bazel version. Other cases, like native, or rules exporting a public struct interface, have a natural solution to just check So, I would suggest having a
This doesn't really scale. As a project owner, if any Starlark in my transitive closure even mentions the name, even if I don't use the functionality, then I'm broken and I have to upgrade Bazel. And then my entire transitive closure needs to support the new Bazel version. Stated another way: I don't actually control when Bazel is updated anymore, the library with the shortest Bazel support timeline does. As a library owner, I'm forced to break somebody. Either I force the transitive closure of all my users to stay with the oldest Bazel version any of them use, or I force them all to a minimum Bazel version. This isn't entirely hypothetical, either. This sort of problem has already occurred with skylib, with the feature to add the "name" argument to unittest.make(). That case is somewhat different and has a different solution, but it's still in the same vein: Starlark needing to be cross-Bazel version compatible. Given that there's plenty of room to improve testing Starlark, a new global being added to facilitate that seems very plausible. |
Just echoing the concern that this is a very real problem that affects the health of the ecosystem. This topic spawned a whole design doc: https://docs.google.com/document/d/1HJf3gMYIrzmTRqbD4nWXH2eJRHXjLrOU0mmIeZplUzY/edit In the end we implemented a "bazel_features" project (https://github.com/bazel-contrib/bazel_features) to work around this issue for Bazel itself. It includes a "globals" backdoor where we provide access to global symbols based on Bazel version detection. Worth noting that it's not a replacement for this issue, merely a workaround. |
I propose adding a zero-argument form of
dir()
, matching that of Python 3: https://docs.python.org/3/library/functions.html#dirThere are two use cases, which are currently not very easy to work around otherwise:
The text was updated successfully, but these errors were encountered: