Skip to content

Commit e46bc83

Browse files
authored
Serialize Decimal, Fraction and NumPy scalars (#281)
1 parent bdab43b commit e46bc83

2 files changed

Lines changed: 238 additions & 4 deletions

File tree

‎src/graphql/type/scalars.py‎

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ def serialize_int(output_value: Any) -> int:
5858
return coerce_int_from_number(output_value)
5959
if isinstance(output_value, str):
6060
return coerce_int_from_string(output_value)
61+
if is_numeric_object(output_value):
62+
return coerce_int_from_numeric_object(output_value)
6163
msg = "Int cannot represent non-integer value: " + inspect(output_value)
6264
raise GraphQLError(msg)
6365

@@ -106,7 +108,11 @@ def int_value_to_literal(value: Any) -> ConstValueNode | None:
106108
coerce_input_literal=parse_int_literal,
107109
value_to_literal=int_value_to_literal,
108110
)
109-
"""The built-in ``Int`` scalar type."""
111+
"""The built-in ``Int`` scalar type.
112+
113+
Besides Python ints, other numeric types like ``Decimal``, ``Fraction`` or NumPy
114+
scalars can also be serialized as Int, as long as they represent 32-bit integers.
115+
"""
110116

111117

112118
def serialize_float(output_value: Any) -> float:
@@ -118,6 +124,10 @@ def serialize_float(output_value: Any) -> float:
118124
return coerce_float_from_int(output_value)
119125
if isinstance(output_value, str):
120126
return coerce_float_from_string(output_value)
127+
if is_numeric_object(output_value):
128+
num = finite_float_from_numeric_object(output_value)
129+
if num is not None:
130+
return num
121131
msg = "Float cannot represent non numeric value: " + inspect(output_value)
122132
raise GraphQLError(msg)
123133

@@ -160,7 +170,13 @@ def float_value_to_literal(value: Any) -> ConstValueNode | None:
160170
coerce_input_literal=parse_float_literal,
161171
value_to_literal=float_value_to_literal,
162172
)
163-
"""The built-in ``Float`` scalar type."""
173+
"""The built-in ``Float`` scalar type.
174+
175+
Besides Python floats and ints, other numeric types like ``Decimal``, ``Fraction``
176+
or NumPy scalars can also be serialized as Float. Note that they are converted to
177+
Python floats and may lose precision. Use a custom scalar type if you need to
178+
preserve the precision.
179+
"""
164180

165181

166182
def serialize_string(output_value: Any) -> str:
@@ -230,6 +246,10 @@ def serialize_boolean(output_value: Any) -> bool:
230246
# always finite.
231247
if isinstance(output_value, int):
232248
return output_value != 0
249+
if is_numeric_object(output_value):
250+
num = finite_float_from_numeric_object(output_value)
251+
if num is not None:
252+
return num != 0
233253
raise GraphQLError(
234254
"Boolean cannot represent a non boolean value: " + inspect(output_value)
235255
)
@@ -269,7 +289,12 @@ def boolean_value_to_literal(value: Any) -> ConstValueNode | None:
269289
coerce_input_literal=parse_boolean_literal,
270290
value_to_literal=boolean_value_to_literal,
271291
)
272-
"""The built-in ``Boolean`` scalar type."""
292+
"""The built-in ``Boolean`` scalar type.
293+
294+
Besides Python bools and other numbers, numeric types like ``Decimal``,
295+
``Fraction`` or NumPy scalars (including NumPy booleans) can also be serialized
296+
as Boolean.
297+
"""
273298

274299

275300
def serialize_id(output_value: Any) -> str:
@@ -412,7 +437,8 @@ def coerce_boolean_from_number(value: float) -> bool:
412437
if not isfinite(value):
413438
msg = "Boolean cannot represent a non boolean value: " + inspect(value)
414439
raise GraphQLError(msg)
415-
return value != 0
440+
# use bool() since subclasses like NumPy's float64 return NumPy's bool here
441+
return bool(value != 0)
416442

417443

418444
def coerce_id_from_number(value: float) -> str:
@@ -422,6 +448,41 @@ def coerce_id_from_number(value: float) -> str:
422448
return str(int(value))
423449

424450

451+
def is_numeric_object(value: Any) -> bool:
452+
"""Check whether the value is a number of a non-builtin type.
453+
454+
Numbers of types such as ``Decimal``, ``Fraction`` or NumPy scalars do not
455+
subclass Python's ``int`` or ``float``, but can be converted to them.
456+
"""
457+
type_ = type(value)
458+
return type_.__module__ != "builtins" and hasattr(type_, "__float__")
459+
460+
461+
def coerce_int_from_numeric_object(value: Any) -> int:
462+
try:
463+
num = int(value)
464+
except (OverflowError, TypeError, ValueError) as error:
465+
msg = "Int cannot represent non-integer value: " + inspect(value)
466+
raise GraphQLError(msg) from error
467+
# do not truncate non-integer values (the comparison is exact for decimals)
468+
if num != value:
469+
msg = "Int cannot represent non-integer value: " + inspect(value)
470+
raise GraphQLError(msg)
471+
if not GRAPHQL_MIN_INT <= num <= GRAPHQL_MAX_INT:
472+
msg = "Int cannot represent non 32-bit signed integer value: " + inspect(value)
473+
raise GraphQLError(msg)
474+
return num
475+
476+
477+
def finite_float_from_numeric_object(value: Any) -> float | None:
478+
"""Convert a number of a non-builtin type to a finite float if possible."""
479+
try:
480+
num = float(value)
481+
except (OverflowError, ValueError):
482+
return None
483+
return num if isfinite(num) else None
484+
485+
425486
specified_scalar_types: Mapping[str, GraphQLScalarType] = {
426487
type_.name: type_
427488
for type_ in (

‎tests/type/test_scalars.py‎

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import pickle
2+
from decimal import Decimal
3+
from fractions import Fraction
24
from math import inf, nan, pi
35
from typing import Any
46

@@ -17,6 +19,36 @@
1719
)
1820

1921

22+
class NumPyLikeScalar: # noqa: PLW1641
23+
"""Mimic a NumPy scalar such as numpy.int64, numpy.float32 or numpy.bool.
24+
25+
Like these, it does not subclass a builtin numeric type,
26+
but can be converted to and compared with one.
27+
"""
28+
29+
def __init__(self, value: float) -> None:
30+
self.value = value
31+
32+
def __int__(self) -> int:
33+
return int(self.value)
34+
35+
def __float__(self) -> float:
36+
return float(self.value)
37+
38+
def __bool__(self) -> bool:
39+
return bool(self.value)
40+
41+
def __eq__(self, other: object) -> bool:
42+
return self.value == other
43+
44+
45+
class NumPyLikeFloat64(float):
46+
"""Mimic numpy.float64, which subclasses float but compares to numpy.bool."""
47+
48+
def __ne__(self, other: object) -> Any:
49+
return NumPyLikeScalar(super().__ne__(other))
50+
51+
2052
def describe_type_system_specified_scalar_types():
2153
def describe_graphql_int():
2254
def coerce_input_value():
@@ -200,6 +232,65 @@ def coerce_output_value():
200232
coerce_output_value([5])
201233
assert str(exc_info.value) == "Int cannot represent non-integer value: [5]"
202234

235+
def coerce_output_value_from_other_numeric_types():
236+
coerce_output_value = GraphQLInt.coerce_output_value
237+
238+
def _coerce_output_value_raises(value: Any, message: str):
239+
with pytest.raises(GraphQLError) as exc_info:
240+
coerce_output_value(value)
241+
assert str(exc_info.value) == message
242+
243+
result = coerce_output_value(Decimal(123))
244+
assert result == 123
245+
assert type(result) is int
246+
assert coerce_output_value(Decimal("-1.0")) == -1
247+
assert coerce_output_value(Fraction(4, 2)) == 2
248+
result = coerce_output_value(NumPyLikeScalar(5))
249+
assert result == 5
250+
assert type(result) is int
251+
assert coerce_output_value(NumPyLikeScalar(1e5)) == 100000
252+
assert coerce_output_value(NumPyLikeScalar(True)) == 1
253+
254+
# Non-integer values are not truncated, even if the loss is not
255+
# visible after conversion to float.
256+
for value in (
257+
Decimal("0.1"),
258+
Decimal("1.0000000000000000000001"),
259+
Fraction(1, 2),
260+
NumPyLikeScalar(1.1),
261+
Decimal("NaN"),
262+
Decimal("sNaN"),
263+
Decimal("Infinity"),
264+
NumPyLikeScalar(nan),
265+
NumPyLikeScalar(inf),
266+
):
267+
_coerce_output_value_raises(
268+
value, "Int cannot represent non-integer value: " + inspect(value)
269+
)
270+
_coerce_output_value_raises(
271+
Decimal(9876504321),
272+
"Int cannot represent non 32-bit signed integer value:"
273+
" <Decimal instance>",
274+
)
275+
_coerce_output_value_raises(
276+
NumPyLikeScalar(-9876504321),
277+
"Int cannot represent non 32-bit signed integer value:"
278+
" <NumPyLikeScalar instance>",
279+
)
280+
# Numbers that cannot be converted to int are not accepted.
281+
float_only = type("FloatOnly", (), {"__float__": lambda _self: 1.0})()
282+
_coerce_output_value_raises(
283+
float_only,
284+
"Int cannot represent non-integer value: <FloatOnly instance>",
285+
)
286+
# Builtin types are not converted.
287+
_coerce_output_value_raises(
288+
b"5", "Int cannot represent non-integer value: b'5'"
289+
)
290+
_coerce_output_value_raises(
291+
5 + 0j, "Int cannot represent non-integer value: (5+0j)"
292+
)
293+
203294
def cannot_be_redefined():
204295
with pytest.raises(TypeError, match="Redefinition of reserved type 'Int'"):
205296
GraphQLScalarType(name="Int")
@@ -378,6 +469,51 @@ def coerce_output_value():
378469
str(exc_info.value) == "Float cannot represent non numeric value: [5]"
379470
)
380471

472+
def coerce_output_value_from_other_numeric_types():
473+
coerce_output_value = GraphQLFloat.coerce_output_value
474+
475+
def _coerce_output_value_raises(value: Any, message: str):
476+
with pytest.raises(GraphQLError) as exc_info:
477+
coerce_output_value(value)
478+
assert str(exc_info.value) == message
479+
480+
result = coerce_output_value(Decimal("123.45"))
481+
assert result == 123.45
482+
assert type(result) is float
483+
assert coerce_output_value(Decimal(-1)) == -1.0
484+
assert coerce_output_value(Fraction(1, 4)) == 0.25
485+
result = coerce_output_value(NumPyLikeScalar(1.5))
486+
assert result == 1.5
487+
assert type(result) is float
488+
assert coerce_output_value(NumPyLikeScalar(3)) == 3.0
489+
assert coerce_output_value(NumPyLikeScalar(True)) == 1.0
490+
491+
# These values may lose precision when converted to float.
492+
assert coerce_output_value(Decimal("0.1000000000000000000001")) == 0.1
493+
assert coerce_output_value(Fraction(1, 3)) == 1 / 3
494+
assert coerce_output_value(Decimal(9007199254740993)) == 9007199254740992
495+
496+
for value in (
497+
Decimal("NaN"),
498+
Decimal("sNaN"),
499+
Decimal("Infinity"),
500+
Decimal("-Infinity"),
501+
Decimal("1e400"),
502+
Fraction(2**1024),
503+
NumPyLikeScalar(nan),
504+
NumPyLikeScalar(inf),
505+
):
506+
_coerce_output_value_raises(
507+
value, "Float cannot represent non numeric value: " + inspect(value)
508+
)
509+
# Builtin types are not converted.
510+
_coerce_output_value_raises(
511+
b"1.5", "Float cannot represent non numeric value: b'1.5'"
512+
)
513+
_coerce_output_value_raises(
514+
1.5 + 0j, "Float cannot represent non numeric value: (1.5+0j)"
515+
)
516+
381517
def cannot_be_redefined():
382518
with pytest.raises(
383519
TypeError, match="Redefinition of reserved type 'Float'"
@@ -656,6 +792,43 @@ def coerce_output_value():
656792
"Boolean cannot represent a non boolean value: {}"
657793
)
658794

795+
def coerce_output_value_from_other_numeric_types():
796+
coerce_output_value = GraphQLBoolean.coerce_output_value
797+
798+
def _coerce_output_value_raises(value: Any, message: str):
799+
with pytest.raises(GraphQLError) as exc_info:
800+
coerce_output_value(value)
801+
assert str(exc_info.value) == message
802+
803+
assert coerce_output_value(NumPyLikeFloat64(2.0)) is True
804+
assert coerce_output_value(NumPyLikeFloat64(0.0)) is False
805+
assert coerce_output_value(NumPyLikeScalar(True)) is True
806+
assert coerce_output_value(NumPyLikeScalar(False)) is False
807+
assert coerce_output_value(NumPyLikeScalar(5)) is True
808+
assert coerce_output_value(NumPyLikeScalar(0.0)) is False
809+
assert coerce_output_value(Decimal("0.5")) is True
810+
assert coerce_output_value(Decimal(0)) is False
811+
assert coerce_output_value(Fraction(1, 2)) is True
812+
813+
for value in (
814+
Decimal("NaN"),
815+
Decimal("sNaN"),
816+
Decimal("Infinity"),
817+
NumPyLikeScalar(nan),
818+
NumPyLikeScalar(inf),
819+
):
820+
_coerce_output_value_raises(
821+
value,
822+
"Boolean cannot represent a non boolean value: " + inspect(value),
823+
)
824+
# Builtin types are not converted.
825+
_coerce_output_value_raises(
826+
b"1", "Boolean cannot represent a non boolean value: b'1'"
827+
)
828+
_coerce_output_value_raises(
829+
1 + 0j, "Boolean cannot represent a non boolean value: (1+0j)"
830+
)
831+
659832
def cannot_be_redefined():
660833
with pytest.raises(
661834
TypeError, match="Redefinition of reserved type 'Boolean'"

0 commit comments

Comments
 (0)