databricks / sjsonnet

Apache License 2.0
266 stars 54 forks source link

Formatting (std.mod, %) does not perform type check #185

Closed zapster closed 31 minutes ago

zapster commented 1 year ago

See the following reproducer:

$ cat test.jsonnet
[ "test%d" % "3" ]
$ sjsonnet test.jsonnet
[
   "test3"
]
$ jsonnet test.jsonnet
RUNTIME ERROR: Format required number at 0, got string
        <std>:(588:11)-(589:47) function <format_code>
        <std>:715:15-60 thunk <s> from <function <format_codes_arr>>
        <std>:720:24-25 thunk from <thunk <s_padded> from <function <format_codes_arr>>>
        <std>:480:30-33 thunk from <thunk from <function <pad_left>>>
        <std>:480:19-34 thunk from <function <pad_left>>
        <std>:476:11-12 thunk from <function <padding>>
        <std>:472:12-13 function <aux>
        <std>:476:7-17  function <padding>
        <std>:480:7-38  function <pad_left>
        <std>:720:15-39 thunk <s_padded> from <function <format_codes_arr>>
        <std>:726:55-63 thunk from <function <format_codes_arr>>
        <std>:726:11-64 function <format_codes_arr>
        <std>:774:7-48  function <anonymous>
        <std>:237:7-23  function <anonymous>
        test.jsonnet:1:3-17
        During manifestation

So the reference implementation issues an error while sjsonnet accepts the input.

According to the reference:

The function std.mod(a, b) is what the % operator is desugared to. It performs modulo arithmetic if the left hand side is a number, or if the left hand side is a string, it does Python-style string formatting with std.format().

Python issues an error as well:

python -c "'test%d' % '3'"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
TypeError: %d format: a real number is required, not str