-
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
Extend Starlark syntax with type annotations #24407
Comments
The flag shall be called initially We need to support gradual rollout, not enabling the types everywhere at once. Gradual rollout is usually done using allowlists, however we don't have any mechanisms to read an allowlist during load. One option is to implement a load time allowlist. That would potentially look similar to the prelude. Anybody modifying it would necessarily trigger reloading of everything. Another option is to make the flag accept a list of paths, where the types are allowed, for example We also need to think about supporting older Bazel versions. New Bazel versions will first support types with the flag, and then when they become stable, without any flags. Similar thing needs to happen in older Bazels - first a flag, then no flag. It's unlikely we'll be able to cherry-pick the complete implementation to older Bazel (if we do, that's a plus). In case we can't and we just ignore the type on older Bazel, there needs to be a warning such code needs to be tested with a Bazel that has a complete implementation. |
Initially we're looking to add type annotations only to definitions of functions, for example: One option to extend Starlark syntax is to follow Python (https://docs.python.org/3/reference/grammar.html), The relevant excerpt is:
Python allows the annotation to be an arbitrary expression (often called Converting this to Starlark grammar: -DefStmt = 'def' identifier '(' [Parameters [',']] ')' ':' Suite .
+DefStmt = 'def' identifier '(' [Parameters [',']] ')' ['->' Test ] ':' Suite .
-Parameter = identifier
- | identifier '=' Test
+Parameter = identifier [':' Test]
+ | identifier [':' Test] '=' Test
| '*'
| '*' identifier
| '**' identifier Most if not all other languages (I'm familiar with) define a strict grammar for their types. |
Most if not all other languages (that I'm familiar with) define a strict grammar for their types. An alternative option is to introduce stricter syntax to Starlark: -DefStmt = 'def' identifier '(' [Parameters [',']] ')' ':' Suite .
+DefStmt = 'def' identifier '(' [Parameters [',']] ')' ['->' Type ] ':' Suite .
-Parameter = identifier
- | identifier '=' Test
+Parameter = identifier [':' Type]
+ | identifier [':' Type] '=' Test
| '*'
| '*' identifier
| '**' identifier
+Type = identifier
+ | identifier '|' Type
+ | identifier '[' [TypeArguments [',']] ']' .
+TypeArguments = Type {',' Type} . The benefit of stricter syntax is that evaluation of the types becomes simpler. Something that we need, because types need to be evaluated early to do type checking and they potentially need to be evaluated within Java annotations, to specify types of native Java methods exposed to Starlark. Ellipsis |
Comparison to Buck2 (I'm not very literate in Rust code): From the reference in ast: When evaluating the type expression, most of the expressions error out: Not allowed: calls (because they use regular braces Identifiers, index, "bit or", and dots make complete sense. My best guess is that tuples and non-empty lists are used as some kind of syntax sugar for |
Implement a flag guarding the use of types in Starlark and extend Starlark syntax with type annotation.
The text was updated successfully, but these errors were encountered: