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

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

Closed
zapster opened this issue Sep 11, 2023 · 0 comments · Fixed by #212
Closed

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

zapster opened this issue Sep 11, 2023 · 0 comments · Fixed by #212

Comments

@zapster
Copy link

zapster commented Sep 11, 2023

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant