|
1 | 1 | import pickle |
| 2 | +from decimal import Decimal |
| 3 | +from fractions import Fraction |
2 | 4 | from math import inf, nan, pi |
3 | 5 | from typing import Any |
4 | 6 |
|
|
17 | 19 | ) |
18 | 20 |
|
19 | 21 |
|
| 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 | + |
20 | 52 | def describe_type_system_specified_scalar_types(): |
21 | 53 | def describe_graphql_int(): |
22 | 54 | def coerce_input_value(): |
@@ -200,6 +232,65 @@ def coerce_output_value(): |
200 | 232 | coerce_output_value([5]) |
201 | 233 | assert str(exc_info.value) == "Int cannot represent non-integer value: [5]" |
202 | 234 |
|
| 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 | + |
203 | 294 | def cannot_be_redefined(): |
204 | 295 | with pytest.raises(TypeError, match="Redefinition of reserved type 'Int'"): |
205 | 296 | GraphQLScalarType(name="Int") |
@@ -378,6 +469,51 @@ def coerce_output_value(): |
378 | 469 | str(exc_info.value) == "Float cannot represent non numeric value: [5]" |
379 | 470 | ) |
380 | 471 |
|
| 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 | + |
381 | 517 | def cannot_be_redefined(): |
382 | 518 | with pytest.raises( |
383 | 519 | TypeError, match="Redefinition of reserved type 'Float'" |
@@ -656,6 +792,43 @@ def coerce_output_value(): |
656 | 792 | "Boolean cannot represent a non boolean value: {}" |
657 | 793 | ) |
658 | 794 |
|
| 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 | + |
659 | 832 | def cannot_be_redefined(): |
660 | 833 | with pytest.raises( |
661 | 834 | TypeError, match="Redefinition of reserved type 'Boolean'" |
|
0 commit comments