Skip to content

Commit 22f4686

Browse files
committed
Represent serialized floats to approximately python float precision
1 parent 1a1a2ee commit 22f4686

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

gql/dsl.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,13 @@ def ast_from_serialized_value_untyped(serialized: Any) -> Optional[ValueNode]:
106106
return BooleanValueNode(value=serialized)
107107

108108
if isinstance(serialized, int):
109-
return IntValueNode(value=f"{serialized:d}")
109+
return IntValueNode(value=str(serialized))
110110

111111
if isinstance(serialized, float) and isfinite(serialized):
112-
return FloatValueNode(value=f"{serialized:g}")
112+
value = str(serialized)
113+
if value.endswith(".0"):
114+
value = value[:-2]
115+
return FloatValueNode(value=value)
113116

114117
if isinstance(serialized, str):
115118
return StringValueNode(value=serialized)

tests/starwars/test_dsl.py

+16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import pytest
22
from graphql import (
3+
FloatValueNode,
34
GraphQLError,
5+
GraphQLFloat,
46
GraphQLID,
57
GraphQLInt,
68
GraphQLList,
@@ -87,6 +89,20 @@ def test_ast_from_value_with_non_null_type_and_none():
8789
assert "Received Null value for a Non-Null type Int." in str(exc_info.value)
8890

8991

92+
def test_ast_from_value_float_precision():
93+
94+
# Checking precision of float serialization
95+
# See https://github.com/graphql-python/graphql-core/pull/164
96+
97+
assert ast_from_value(123456789.01234567, GraphQLFloat) == FloatValueNode(
98+
value="123456789.01234567"
99+
)
100+
101+
assert ast_from_value(1.1, GraphQLFloat) == FloatValueNode(value="1.1")
102+
103+
assert ast_from_value(123.0, GraphQLFloat) == FloatValueNode(value="123")
104+
105+
90106
def test_ast_from_serialized_value_untyped_typeerror():
91107
with pytest.raises(TypeError) as exc_info:
92108
ast_from_serialized_value_untyped(GraphQLInt)

0 commit comments

Comments
 (0)