Skip to content

Commit

Permalink
Remove 'too many arguments' error when interpolating over mappings (g…
Browse files Browse the repository at this point in the history
…oogle#520)

* Fix inconsistent interpolation with empty mappings and tuples

* Clarify spec when no conversions are present

* Rename `any` function

* Only check for `Mapping` in the uncommon case
  • Loading branch information
TheSignPainter98 authored Nov 21, 2023
1 parent 556fd59 commit 90ade8b
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 deletions.
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) {
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

0 comments on commit 90ade8b

Please sign in to comment.