Proposal: Add Function Type Annotation Syntax #439
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I'd like to re-visit the proposal for python-like function type annotations in starlark, at least in the
starlark-go
implementation. This has been discussed before in:An example of what this looks like in practice:
This proposal is only for function definition type hints. Other kinds (such as assignment annotations) could be discussed, but I believe function annotations provide the most value with least effort, and can be done without changing any runtime semantics. Function type annotation syntax has already been implemented in
buildifier
(bazelbuild/buildtools#946) andstarlark-rust
(https://github.com/facebookexperimental/starlark-rust/blob/main/docs/types.md).The proposed syntax follows a subset of python type annotations, as well as the same syntax already supported by
buildifier
andstarlark-rust
. Type annotations areTest
syntax elements, and are parsed as expressions.Requirements
Why Now?
Choices
Syntatic Element of a Type Annotation
Test / Expr. This is the same element chosen by
starlark-rust
andbuildifier
, and is the closest analogue to python type hints which allows arbitrary expressions.Ident with TypeHint Field instead of TypedIdent
The buildifier implementation parses a TypedIdent with an ident and type hint:
Whereas the proposed implementation for
starlark-go
reusestype Ident struct
with a TypeHintExpr
field:There are two reasons for this:
syntax.Walk Support
This PR intentionally excludes
syntax.Walk
support for type hints, as it would change the behaviour for programs usingstarlark-go
to parse starlark.On-the-fence Choices
Syntax Feature Flag
Whether or not to lock the syntax behind a flag. Unlike other features behind flags, this does not change the output of programs and does not have backwards compatibility implications.
Resolving Type Annotation Identifiers
The PR adds a
ResolveTypeHintIdents
flag in theresolve
package, which defaults to false. When it is false, type hints cannot cause resolution/compilation failures.When it is true, type annotation identifiers are resolved and given binding information. If a type annotation refers to an unknown identifier, it could cause resolution to fail. Additionally, if a type hint in a nested function definition referred to an identifier from its parent scopes, it could cause local variables to be promoted to
Cell
. Because this can slightly change program behaviour, and cause compilation failures it is enabled behind a flag.I consider this fairly desirable because:
resolve
code this is readily available (otherwise theresolve
logic would have to be heavily duplicated).Skip Type Hints for Assignment Syntax
These are supported by
starlark-rust
, but notbuildifier
. Python PEP-526 formalizes this for python, however the only applicable syntax for starlark would be variable assignment and definition:Python changed its behaviour slightly to allow binding unknown variables if given a typehint. This would be a runtime behaviour change in starlark. The currently supported behaviour would require assignment anyways, where we can often infer the type anyways.
While assignment annotations could provide some value, it's a lot less clear cut to me than function definition annotations. I'd like to table discussion of assignment type hints for a different proposal.