Skip to content

Commit 3ddb4b6

Browse files
committed
NaN values should be checked inside scalar's serialize method
The is_invalid and is_nullish functions now are not needed any more and have been removed from the pyutils package. Replicates graphql/graphql-js@c9850c1
1 parent 1a9a1da commit 3ddb4b6

File tree

12 files changed

+41
-134
lines changed

12 files changed

+41
-134
lines changed

docs/modules/pyutils.rst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ PyUtils
2020
.. autofunction:: inspect
2121
.. autofunction:: is_finite
2222
.. autofunction:: is_integer
23-
.. autofunction:: is_invalid
24-
.. autofunction:: is_nullish
2523
.. autoclass:: AwaitableOrValue
2624
:members:
2725
.. autofunction:: suggestion_list

src/graphql/execution/execute.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
)
2929
from ..pyutils import (
3030
inspect,
31-
is_invalid,
32-
is_nullish,
3331
AwaitableOrValue,
3432
FrozenList,
3533
Path,
@@ -756,8 +754,8 @@ def complete_value(
756754
)
757755
return completed
758756

759-
# If result value is null-ish (null, Undefined, or NaN) then return null.
760-
if is_nullish(result):
757+
# If result value is null or undefined then return null.
758+
if result is None or result is Undefined:
761759
return None
762760

763761
# If field type is List, complete each item in the list with inner type
@@ -852,7 +850,7 @@ def complete_leaf_value(return_type: GraphQLLeafType, result: Any) -> Any:
852850
serialization is not possible.
853851
"""
854852
serialized_result = return_type.serialize(result)
855-
if is_invalid(serialized_result):
853+
if serialized_result is Undefined:
856854
raise TypeError(
857855
f"Expected a value of type '{inspect(return_type)}'"
858856
f" but received: {inspect(result)}"

src/graphql/pyutils/__init__.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
from .is_collection import is_collection
2525
from .is_finite import is_finite
2626
from .is_integer import is_integer
27-
from .is_invalid import is_invalid
28-
from .is_nullish import is_nullish
2927
from .awaitable_or_value import AwaitableOrValue
3028
from .suggestion_list import suggestion_list
3129
from .frozen_error import FrozenError
@@ -52,8 +50,6 @@
5250
"is_collection",
5351
"is_finite",
5452
"is_integer",
55-
"is_invalid",
56-
"is_nullish",
5753
"AwaitableOrValue",
5854
"suggestion_list",
5955
"FrozenError",

src/graphql/pyutils/is_invalid.py

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/graphql/pyutils/is_nullish.py

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/graphql/utilities/ast_from_value.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from math import isfinite
21
import re
2+
from math import isfinite
33
from typing import Any, Iterable, Mapping, Optional, cast
44

55
from ..language import (
@@ -15,7 +15,7 @@
1515
StringValueNode,
1616
ValueNode,
1717
)
18-
from ..pyutils import FrozenList, inspect, is_nullish, is_invalid
18+
from ..pyutils import inspect, FrozenList, Undefined
1919
from ..type import (
2020
GraphQLID,
2121
GraphQLInputType,
@@ -67,8 +67,8 @@ def ast_from_value(value: Any, type_: GraphQLInputType) -> Optional[ValueNode]:
6767
if value is None:
6868
return NullValueNode()
6969

70-
# Undefined or NaN
71-
if is_invalid(value):
70+
# undefined
71+
if value is Undefined:
7272
return None
7373

7474
# Convert Python list to GraphQL list. If the GraphQLType is a list, but the value
@@ -104,7 +104,7 @@ def ast_from_value(value: Any, type_: GraphQLInputType) -> Optional[ValueNode]:
104104
# Since value is an internally represented value, it must be serialized to an
105105
# externally represented value before converting into an AST.
106106
serialized = type_.serialize(value) # type: ignore
107-
if is_nullish(serialized):
107+
if serialized is None or serialized is Undefined:
108108
return None
109109

110110
# Others serialize based on their corresponding Python scalar types.

src/graphql/utilities/value_from_ast.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
ValueNode,
88
VariableNode,
99
)
10-
from ..pyutils import inspect, is_invalid, Undefined
10+
from ..pyutils import inspect, Undefined
1111
from ..type import (
1212
GraphQLInputObjectType,
1313
GraphQLInputType,
@@ -88,12 +88,12 @@ def value_from_ast(
8888
append_value(None)
8989
else:
9090
item_value = value_from_ast(item_node, item_type, variables)
91-
if is_invalid(item_value):
91+
if item_value is Undefined:
9292
return Undefined
9393
append_value(item_value)
9494
return coerced_values
9595
coerced_value = value_from_ast(value_node, item_type, variables)
96-
if is_invalid(coerced_value):
96+
if coerced_value is Undefined:
9797
return Undefined
9898
return [coerced_value]
9999

@@ -114,7 +114,7 @@ def value_from_ast(
114114
return Undefined
115115
continue
116116
field_value = value_from_ast(field_node.value, field.type, variables)
117-
if is_invalid(field_value):
117+
if field_value is Undefined:
118118
return Undefined
119119
coerced_obj[field.out_name or field_name] = field_value
120120

@@ -143,5 +143,5 @@ def is_missing_variable(
143143
) -> bool:
144144
"""Check if `value_node` is a variable not defined in the `variables` dict."""
145145
return isinstance(value_node, VariableNode) and (
146-
not variables or is_invalid(variables.get(value_node.name.value, Undefined))
146+
not variables or variables.get(value_node.name.value, Undefined) is Undefined
147147
)

src/graphql/validation/rules/values_of_correct_type.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
ValueNode,
1515
print_ast,
1616
)
17-
from ...pyutils import did_you_mean, is_invalid, suggestion_list
17+
from ...pyutils import did_you_mean, suggestion_list, Undefined
1818
from ...type import (
1919
GraphQLScalarType,
2020
get_named_type,
@@ -130,7 +130,7 @@ def is_valid_value_node(self, node: ValueNode) -> None:
130130
type_ = cast(GraphQLScalarType, type_)
131131
try:
132132
parse_result = type_.parse_literal(node)
133-
if is_invalid(parse_result):
133+
if parse_result is Undefined:
134134
self.report_error(
135135
GraphQLError(
136136
f"Expected value of type '{location_type}',"

tests/pyutils/test_is_invalid.py

Lines changed: 0 additions & 30 deletions
This file was deleted.

tests/pyutils/test_is_nullish.py

Lines changed: 0 additions & 48 deletions
This file was deleted.

0 commit comments

Comments
 (0)