fix: std.format accepts boolean for numeric conversion codes#233
Open
He-Pin wants to merge 1 commit into
Open
Conversation
The official Jsonnet spec states that std.format follows Python's string formatting rules. In Python, bool is a subclass of int, so "%d" % True returns "1". This fix coerces boolean values to 0.0/1.0 before processing numeric conversion codes, matching Python's behavior.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The official Jsonnet spec states:
In Python,
boolis a subclass ofint, so"%d" % Truereturns"1". However, jrsonnet currently rejects booleans for all numeric conversion codes withtype error: expected number, got boolean.This fix coerces
Val::Bool(true)toVal::Num(1.0)andVal::Bool(false)toVal::Num(0.0)before numeric conversion informat_code(), matching Python's behavior.The
%sconversion is intentionally left unchanged, as Python's"%s" % Truereturns"True"(not"1").Related upstream PRs: google/jsonnet#1328, google/go-jsonnet#884
Before / After
"%d" % true"1""1""%f" % false"0.000000""0.000000""%x" % true"1""1""%o" % true"1""1""%e" % true"1.000000e+00""1.000000e+00""%g" % true"1""1""%c" % true"\x01""\x01""%s" % true"True""true""true"(unchanged)Test plan
cargo test -p jrsonnet-evaluator-- 7/7 passed)bool->intbehavior for all numeric codes (%d,%o,%x,%e,%f,%g)%c(character code) matching Python%sconversion unaffected -- booleans still render as"true"/"false"