-
Notifications
You must be signed in to change notification settings - Fork 163
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
spec: new import syntax, and first class modules #111
Comments
Compatibility with Python is quite a useful feature.
|
That would mean starlark is no longer a syntactic subset of python, which would be a big deviation from status quo? |
One could easily write a Python function that scans a file for |
I have always liked the I wonder what you do with I was personally motivated to move towards the "only import modules" functionality to limit the import search space so that there's room for a tool that's similar to |
Translating
More importantly, the benefits don't seem to justify deviating from the current invariant "all starlark code is syntactically valid python3" |
Personally I prefer the proposed |
Could this proposal be split into a smaller scope by first making the changes to the Define the local namespaced module load("module.star") Define the module load(name = "module.star") Using kwargs could avoid issues with translating filenames to identifiers and gives the ability to rename it. Another option to support building top level modules would be to return the module from the load statement: module = load("module.star") which would define a global scoped |
My largest gripe with load has been able to load the entire module. I have found a bit of a workaround that works at least in the Go implementation of starlark with the existing In addition to the to the normal values being exported via the For naming this well known identifier I have chosen So you end up with statements like
A quick implementation for using built-ins could be
You could do something similar for globals coming from execed files as well (wrapping them in a starlarkstruct.Module first for the special However being that with the CC: @carlverge |
State that Starlark syntax (but not semantic) is a strict subset of Python, and this property will be maintained in the future iterations of Starlark. Context: users have asked what tools they can use to work with starlark sources, to implementing codemods, linters, code formatters, syntax highlighters etc. One possible option is to recommend Python tools when AST is needed, for example, Python's builtin `ast.parse`. However, to make that recommendation correct, we should guarantee that Starlark syntax (but not semantics) will stay strict subset of Python: so each valid Starlark program could be parsed as Python, and tools won't need to be heaviy rewritten when Starlark language changes. This PR automatically closes bazelbuild#111 as it contravenes this newly established requirement.
State that Starlark syntax (but not semantic) is a strict subset of Python, and this property will be maintained in the future iterations of Starlark. Context: users have asked what tools they can use to work with starlark sources to implement codemods, linters, code formatters, syntax highlighters, documentation generators etc. One possible option is to recommend Python tools when AST is needed, for example, Python's builtin `ast.parse`. However, to make that recommendation correct, we should guarantee that Starlark syntax (but not semantics) will stay strict subset of Python: so each valid Starlark program could be parsed as Python, and tools won't need to be heaviy rewritten when Starlark language changes. This PR automatically closes bazelbuild#111 as it contravenes this newly established requirement.
State that Starlark syntax (but not semantic) is a strict subset of Python, and this property will be maintained in the future iterations of Starlark. Context: users have asked what tools they can use to work with starlark sources to implement codemods, linters, code formatters, syntax highlighters, documentation generators etc. One possible option is to recommend Python tools when AST is needed, for example, Python's builtin `ast.parse`. However, to make that recommendation correct, we should guarantee that Starlark syntax (but not semantics) will stay strict subset of Python: so each valid Starlark program could be parsed as Python, and tools won't need to be heaviy rewritten when Starlark language changes. This PR automatically closes bazelbuild#111 as it contravenes this newly established requirement.
State that Starlark syntax (but not semantic) is a strict subset of Python, and this property will be maintained in the future iterations of Starlark. Context: users have asked what tools they can use to work with starlark sources to implement codemods, linters, code formatters, syntax highlighters, documentation generators etc. One possible option is to recommend Python tools when AST is needed, for example, Python's builtin `ast.parse`. However, to make that recommendation correct, we should guarantee that Starlark syntax (but not semantics) will stay strict subset of Python: so each valid Starlark program could be parsed as Python, and tools won't need to be heaviy rewritten when Starlark language changes. This PR automatically closes bazelbuild#111 as it contravenes this newly established requirement.
There are several minor problems with Starlark's current syntax for importing another module:
load
is a reserved word, unnecessarily. Starlark already reservesimport
.The motivation for all these suboptimal choices was to permit Starlark programs to be executed by an unmodified Python interpreter, in which 'load' has been defined as a function. I don't think this is a compelling reason to keep it.
I propose we add a new
from
/import
syntax to Starlark---essentially that of Python, but with quotes around the module name---and gradually eliminate the 'load' syntax. These forms would be temporarily be equivalent:Furthermore, I propose that we support modules as first-class values. That is, both of these statements would import the module of file "dir/name.v2.star" and bind it to the local name "name".
In the first case, the local name is derived from the last segment of the module name ("name.v2.star"), minus any dotted suffixes (".v2.star").
Imports would be remain subject to the same restriction as load statements of appearing only at top-level, and would continue to bind names in the file-local lexical block.
Migration considerations
The text was updated successfully, but these errors were encountered: