Skip to content

Represent serialized floats to python float precision #164

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

Merged
merged 4 commits into from
Apr 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/graphql/utilities/ast_from_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,12 @@ def ast_from_value(value: Any, type_: GraphQLInputType) -> Optional[ValueNode]:

# Python ints and floats correspond nicely to Int and Float values.
if isinstance(serialized, int):
return IntValueNode(value=f"{serialized:d}")
return IntValueNode(value=str(serialized))
if isinstance(serialized, float) and isfinite(serialized):
return FloatValueNode(value=f"{serialized:g}")
value = str(serialized)
if value.endswith('.0'):
value = value[:-2]
return FloatValueNode(value=value)

if isinstance(serialized, str):
# Enum types use Enum literals.
Expand Down
6 changes: 6 additions & 0 deletions tests/utilities/test_ast_from_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ def converts_float_values_to_float_asts():

assert ast_from_value(1e40, GraphQLFloat) == FloatValueNode(value="1e+40")

assert ast_from_value(123456789.01234567, GraphQLFloat) == FloatValueNode(
value="123456789.01234567"
)

assert ast_from_value(1.1, GraphQLFloat) == FloatValueNode(value="1.1")

def converts_string_values_to_string_asts():
assert ast_from_value("hello", GraphQLString) == StringValueNode(value="hello")

Expand Down