Skip to content

Commit 5899a61

Browse files
committed
Narrow the return type of ast_from_value
Replicates graphql/graphql-js@aa43fec
1 parent 8efb8b3 commit 5899a61

File tree

2 files changed

+24
-20
lines changed

2 files changed

+24
-20
lines changed

src/graphql/utilities/ast_from_value.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@
88

99
from ..language import (
1010
BooleanValueNode,
11+
ConstListValueNode,
12+
ConstObjectFieldNode,
13+
ConstObjectValueNode,
14+
ConstValueNode,
1115
EnumValueNode,
1216
FloatValueNode,
1317
IntValueNode,
14-
ListValueNode,
1518
NameNode,
1619
NullValueNode,
17-
ObjectFieldNode,
18-
ObjectValueNode,
1920
StringValueNode,
20-
ValueNode,
2121
)
2222
from ..pyutils import Undefined, inspect, is_iterable
2323
from ..type import (
@@ -35,7 +35,7 @@
3535
_re_integer_string = re.compile("^-?(?:0|[1-9][0-9]*)$")
3636

3737

38-
def ast_from_value(value: Any, type_: GraphQLInputType) -> ValueNode | None:
38+
def ast_from_value(value: Any, type_: GraphQLInputType) -> ConstValueNode | None:
3939
"""Produce a GraphQL Value AST given a Python object.
4040
4141
This function will match Python/JSON values to GraphQL AST schema format by using
@@ -80,7 +80,7 @@ def ast_from_value(value: Any, type_: GraphQLInputType) -> ValueNode | None:
8080
if is_iterable(value):
8181
maybe_value_nodes = (ast_from_value(item, item_type) for item in value)
8282
value_nodes = tuple(node for node in maybe_value_nodes if node)
83-
return ListValueNode(values=value_nodes)
83+
return ConstListValueNode(values=value_nodes)
8484
return ast_from_value(value, item_type)
8585

8686
# Populate the fields of the input object by creating ASTs from each value in the
@@ -94,11 +94,11 @@ def ast_from_value(value: Any, type_: GraphQLInputType) -> ValueNode | None:
9494
if field_name in value
9595
)
9696
field_nodes = tuple(
97-
ObjectFieldNode(name=NameNode(value=field_name), value=field_value)
97+
ConstObjectFieldNode(name=NameNode(value=field_name), value=field_value)
9898
for field_name, field_value in field_items
9999
if field_value
100100
)
101-
return ObjectValueNode(fields=field_nodes)
101+
return ConstObjectValueNode(fields=field_nodes)
102102

103103
if is_leaf_type(type_):
104104
# Since value is an internally represented value, it must be serialized to an

tests/utilities/test_ast_from_value.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
from graphql.error import GraphQLError
55
from graphql.language import (
66
BooleanValueNode,
7+
ConstListValueNode,
8+
ConstObjectFieldNode,
9+
ConstObjectValueNode,
710
EnumValueNode,
811
FloatValueNode,
912
IntValueNode,
10-
ListValueNode,
1113
NameNode,
1214
NullValueNode,
13-
ObjectFieldNode,
14-
ObjectValueNode,
1515
StringValueNode,
1616
)
1717
from graphql.pyutils import Undefined
@@ -202,13 +202,13 @@ def converts_string_values_to_enum_asts_if_possible():
202202
def converts_list_values_to_list_asts():
203203
assert ast_from_value(
204204
["FOO", "BAR"], GraphQLList(GraphQLString)
205-
) == ListValueNode(
205+
) == ConstListValueNode(
206206
values=[StringValueNode(value="FOO"), StringValueNode(value="BAR")]
207207
)
208208

209209
assert ast_from_value(
210210
["HELLO", "GOODBYE"], GraphQLList(my_enum)
211-
) == ListValueNode(
211+
) == ConstListValueNode(
212212
values=[EnumValueNode(value="HELLO"), EnumValueNode(value="GOODBYE")]
213213
)
214214

@@ -218,7 +218,7 @@ def list_generator():
218218
yield 3
219219

220220
assert ast_from_value(list_generator(), GraphQLList(GraphQLInt)) == (
221-
ListValueNode(
221+
ConstListValueNode(
222222
values=[
223223
IntValueNode(value="1"),
224224
IntValueNode(value="2"),
@@ -237,7 +237,7 @@ def skips_invalid_list_items():
237237
["FOO", None, "BAR"], GraphQLList(GraphQLNonNull(GraphQLString))
238238
)
239239

240-
assert ast == ListValueNode(
240+
assert ast == ConstListValueNode(
241241
values=[StringValueNode(value="FOO"), StringValueNode(value="BAR")]
242242
)
243243

@@ -247,20 +247,24 @@ def skips_invalid_list_items():
247247
)
248248

249249
def converts_input_objects():
250-
assert ast_from_value({"foo": 3, "bar": "HELLO"}, input_obj) == ObjectValueNode(
250+
assert ast_from_value(
251+
{"foo": 3, "bar": "HELLO"}, input_obj
252+
) == ConstObjectValueNode(
251253
fields=[
252-
ObjectFieldNode(
254+
ConstObjectFieldNode(
253255
name=NameNode(value="foo"), value=FloatValueNode(value="3")
254256
),
255-
ObjectFieldNode(
257+
ConstObjectFieldNode(
256258
name=NameNode(value="bar"), value=EnumValueNode(value="HELLO")
257259
),
258260
]
259261
)
260262

261263
def converts_input_objects_with_explicit_nulls():
262-
assert ast_from_value({"foo": None}, input_obj) == ObjectValueNode(
263-
fields=[ObjectFieldNode(name=NameNode(value="foo"), value=NullValueNode())]
264+
assert ast_from_value({"foo": None}, input_obj) == ConstObjectValueNode(
265+
fields=[
266+
ConstObjectFieldNode(name=NameNode(value="foo"), value=NullValueNode())
267+
]
264268
)
265269

266270
def does_not_convert_non_object_values_as_input_objects():

0 commit comments

Comments
 (0)