Skip to content
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

Remove 'too many arguments' error when interpolating over mappings #520

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -2197,6 +2197,9 @@ which must be a tuple with exactly one component per conversion,
unless the format string contains only a single conversion, in which
case `args` itself is its operand.

If the format string contains no conversions, the operand must be a
`Mapping` or an empty tuple.

Starlark does not support the flag, width, and padding specifiers
supported by Python's `%` and other variants of C's `printf`.

Expand Down
7 changes: 6 additions & 1 deletion starlark/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -1659,9 +1659,14 @@ func interpolate(format string, x Value) (Value, error) {
index++
}

if index < nargs {
if index < nargs && !is[Mapping](x) {
return nil, fmt.Errorf("too many arguments for format string")
}

return String(buf.String()), nil
}

func is[T any](x any) bool {
_, ok := x.(T)
return ok
}
4 changes: 2 additions & 2 deletions starlark/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func init() {
"True": True,
"False": False,
"abs": NewBuiltin("abs", abs),
"any": NewBuiltin("any", any),
"any": NewBuiltin("any", any_),
"all": NewBuiltin("all", all),
"bool": NewBuiltin("bool", bool_),
"bytes": NewBuiltin("bytes", bytes_),
Expand Down Expand Up @@ -210,7 +210,7 @@ func all(thread *Thread, _ *Builtin, args Tuple, kwargs []Tuple) (Value, error)
}

// https://github.com/google/starlark-go/blob/master/doc/spec.md#any
func any(thread *Thread, _ *Builtin, args Tuple, kwargs []Tuple) (Value, error) {
func any_(thread *Thread, _ *Builtin, args Tuple, kwargs []Tuple) (Value, error) {
TheSignPainter98 marked this conversation as resolved.
Show resolved Hide resolved
var iterable Iterable
if err := UnpackPositionalArgs("any", args, kwargs, 1, &iterable); err != nil {
return nil, err
Expand Down
2 changes: 2 additions & 0 deletions starlark/testdata/string.star
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,9 @@ assert.eq(gothash, wanthash)
# TODO(adonovan): ordered comparisons

# string % tuple formatting
assert.eq("A" % (), "A")
assert.eq("A %d %x Z" % (123, 456), "A 123 1c8 Z")
assert.eq("A" % {'unused': 123}, "A")
assert.eq("A %(foo)d %(bar)s Z" % {"foo": 123, "bar": "hi"}, "A 123 hi Z")
assert.eq("%s %r" % ("hi", "hi"), 'hi "hi"') # TODO(adonovan): use ''-quotation
assert.eq("%%d %d" % 1, "%d 1")
Expand Down
Loading