Skip to content

Commit 1a9a1da

Browse files
committed
ast_from_value: reject non-finite numbers
Replicates graphql/graphql-js@4288c19
1 parent 8217a3f commit 1a9a1da

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

src/graphql/utilities/ast_from_value.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from math import isfinite
12
import re
23
from typing import Any, Iterable, Mapping, Optional, cast
34

@@ -113,7 +114,7 @@ def ast_from_value(value: Any, type_: GraphQLInputType) -> Optional[ValueNode]:
113114
# Python ints and floats correspond nicely to Int and Float values.
114115
if isinstance(serialized, int):
115116
return IntValueNode(value=f"{serialized:d}")
116-
if isinstance(serialized, float):
117+
if isinstance(serialized, float) and isfinite(serialized):
117118
return FloatValueNode(value=f"{serialized:g}")
118119

119120
if isinstance(serialized, str):

tests/utilities/test_ast_from_value.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from math import nan
1+
from math import inf, nan
22

33
from pytest import raises # type: ignore
44

@@ -134,6 +134,11 @@ def converts_using_serialize_from_a_custom_scalar_type():
134134
value="value"
135135
)
136136

137+
assert ast_from_value(nan, pass_through_scalar) is None
138+
with raises(TypeError) as exc_info:
139+
ast_from_value(inf, pass_through_scalar)
140+
assert str(exc_info.value) == "Cannot convert value to AST: inf."
141+
137142
return_null_scalar = GraphQLScalarType(
138143
"ReturnNullScalar", serialize=lambda value: None,
139144
)

0 commit comments

Comments
 (0)