Serialize NaN and Infinity values as strings for Hamilton UI #1322
+27
−2
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.
Fix it so that NaN and Infinity floats do not throw an error when being passed to Hamilton UI.
Changes
This commit uses
json.dumps(…, allow_nan=False)
, which raises aValueError
for such values, and in that case converts them to strings.The result is that Hamliton UI will show such values as strings
"nan"
,"inf"
, and"-inf"
.This could be confusing only to users who are also using such string values.
How I tested this
Created failing unit tests, patched the code, ensured the failing tests now pass.
Ran test code from #1310 and confirmed that runs made with this fix show up, they just show
"nan"
as the input value for the run.Ran all unit tests; while some fail, they also failed before this fix.
Notes
Float values that are "out of range" were previously serialized—to send to the tracker/Hamilton UI—in a way that produced invalid JSON (but value JS), which caused a runtime error trying to send values to the tracker.
math.nan
,math.inf
, and-math.inf
are serialized byjson.dumps()
toNaN
,Infinity
, and-Infinity
values by default(NOT as strings).Fixes #1310
Checklist