google / starlark-go

Starlark in Go: the Starlark configuration language, implemented in Go
BSD 3-Clause "New" or "Revised" License
2.31k stars 209 forks source link

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

Closed TheSignPainter98 closed 10 months ago

TheSignPainter98 commented 10 months ago

When no conversions are specified, interpolation over an empty tuple is accepted, but interpolation over an empty dict is rejected:

>>> 'foo' % ()
"foo"
>>> 'foo' % {}
Traceback (most recent call last):
  <stdin>:1:7: in <expr>
Error: too many arguments for format string

For context, extra dict arguments are not rejected when a conversion is present:

>>> '%(a)d' % {'a': 1, 'unused': 2}
"1"

This PR relaxes the 'too many arguments' check for mappings and adds tests to ensure consistency between tuples and dicts

TheSignPainter98 commented 10 months ago

Adding the line to the spec, I think the suggestion isn't quite right as an empty mapping would also be acceptable. Perhaps something like this would work?

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