Skip to content

Commit 0a33b50

Browse files
committed
Use type hints in schema validation tests
Replicates graphql/graphql-js@0786b6b
1 parent fc39b3d commit 0a33b50

File tree

1 file changed

+36
-22
lines changed

1 file changed

+36
-22
lines changed

tests/type/test_validation.py

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from typing import cast
1+
from functools import partial
2+
from typing import cast, List, Union
23

34
from pytest import mark, raises
45

@@ -8,15 +9,19 @@
89
GraphQLEnumValue,
910
GraphQLField,
1011
GraphQLInputField,
12+
GraphQLInputType,
1113
GraphQLInputObjectType,
1214
GraphQLInterfaceType,
1315
GraphQLList,
16+
GraphQLNamedType,
1417
GraphQLNonNull,
1518
GraphQLObjectType,
19+
GraphQLOutputType,
1620
GraphQLScalarType,
1721
GraphQLSchema,
1822
GraphQLString,
1923
GraphQLUnionType,
24+
GraphQLWrappingType,
2025
validate_schema,
2126
GraphQLArgument,
2227
GraphQLDirective,
@@ -56,7 +61,10 @@ def _get(value):
5661
)
5762

5863

59-
def with_modifiers(types):
64+
def with_modifiers(
65+
types: List[GraphQLNamedType]
66+
) -> List[Union[GraphQLNamedType, GraphQLWrappingType]]:
67+
types = cast(List[Union[GraphQLNamedType, GraphQLWrappingType]], types)
6068
return (
6169
types
6270
+ [GraphQLList(t) for t in types]
@@ -65,7 +73,7 @@ def with_modifiers(types):
6573
)
6674

6775

68-
output_types = with_modifiers(
76+
output_types: List[GraphQLOutputType] = with_modifiers(
6977
[
7078
GraphQLString,
7179
SomeScalarType,
@@ -76,13 +84,19 @@ def with_modifiers(types):
7684
]
7785
)
7886

79-
not_output_types = with_modifiers([SomeInputObjectType])
87+
not_output_types: List[GraphQLInputType] = with_modifiers([SomeInputObjectType])
8088

81-
input_types = with_modifiers(
89+
input_types: List[GraphQLInputType] = with_modifiers(
8290
[GraphQLString, SomeScalarType, SomeEnumType, SomeInputObjectType]
8391
)
8492

85-
not_input_types = with_modifiers([SomeObjectType, SomeUnionType, SomeInterfaceType])
93+
not_input_types: List[GraphQLOutputType] = with_modifiers(
94+
[SomeObjectType, SomeUnionType, SomeInterfaceType]
95+
)
96+
97+
parametrize_type = partial(
98+
mark.parametrize("type_", ids=lambda type_: type_.__class__.__name__)
99+
)
86100

87101

88102
def schema_with_field_type(type_):
@@ -763,7 +777,7 @@ def schema_with_enum(name):
763777

764778

765779
def describe_type_system_object_fields_must_have_output_types():
766-
def _schema_with_object_field_of_type(field_type):
780+
def _schema_with_object_field_of_type(field_type: GraphQLOutputType):
767781
BadObjectType = GraphQLObjectType(
768782
"BadObject", {"badField": GraphQLField(field_type)}
769783
)
@@ -772,7 +786,7 @@ def _schema_with_object_field_of_type(field_type):
772786
types=[SomeObjectType],
773787
)
774788

775-
@mark.parametrize("type_", output_types)
789+
@parametrize_type(output_types)
776790
def accepts_an_output_type_as_an_object_field_type(type_):
777791
schema = _schema_with_object_field_of_type(type_)
778792
assert validate_schema(schema) == []
@@ -784,15 +798,15 @@ def rejects_an_empty_object_field_type():
784798
msg = str(exc_info.value)
785799
assert msg == "Field type must be an output type."
786800

787-
@mark.parametrize("type_", not_output_types)
801+
@parametrize_type(not_output_types)
788802
def rejects_a_non_output_type_as_an_object_field_type(type_):
789803
# invalid schema cannot be built with Python
790804
with raises(TypeError) as exc_info:
791805
_schema_with_object_field_of_type(type_)
792806
msg = str(exc_info.value)
793807
assert msg == "Field type must be an output type."
794808

795-
@mark.parametrize("type_", [int, float, str])
809+
@parametrize_type([int, float, str])
796810
def rejects_a_non_type_value_as_an_object_field_type(type_):
797811
# invalid schema cannot be built with Python
798812
with raises(TypeError) as exc_info:
@@ -1042,7 +1056,7 @@ def rejects_object_implementing_extended_interface_due_to_type_mismatch():
10421056

10431057

10441058
def describe_type_system_interface_fields_must_have_output_types():
1045-
def _schema_with_interface_field_of_type(field_type):
1059+
def _schema_with_interface_field_of_type(field_type: GraphQLOutputType):
10461060
BadInterfaceType = GraphQLInterfaceType(
10471061
"BadInterface", {"badField": GraphQLField(field_type)}
10481062
)
@@ -1056,7 +1070,7 @@ def _schema_with_interface_field_of_type(field_type):
10561070
types=[BadImplementingType, SomeObjectType],
10571071
)
10581072

1059-
@mark.parametrize("type_", output_types)
1073+
@parametrize_type(output_types)
10601074
def accepts_an_output_type_as_an_interface_field_type(type_):
10611075
schema = _schema_with_interface_field_of_type(type_)
10621076
assert validate_schema(schema) == []
@@ -1068,15 +1082,15 @@ def rejects_an_empty_interface_field_type():
10681082
msg = str(exc_info.value)
10691083
assert msg == "Field type must be an output type."
10701084

1071-
@mark.parametrize("type_", not_output_types)
1085+
@parametrize_type(not_output_types)
10721086
def rejects_a_non_output_type_as_an_interface_field_type(type_):
10731087
# invalid schema cannot be built with Python
10741088
with raises(TypeError) as exc_info:
10751089
_schema_with_interface_field_of_type(type_)
10761090
msg = str(exc_info.value)
10771091
assert msg == "Field type must be an output type."
10781092

1079-
@mark.parametrize("type_", [int, float, str])
1093+
@parametrize_type([int, float, str])
10801094
def rejects_a_non_type_value_as_an_interface_field_type(type_):
10811095
# invalid schema cannot be built with Python
10821096
with raises(TypeError) as exc_info:
@@ -1128,7 +1142,7 @@ def accepts_an_interface_not_implemented_by_at_least_one_object():
11281142

11291143

11301144
def describe_type_system_field_arguments_must_have_input_types():
1131-
def _schema_with_arg_of_type(arg_type):
1145+
def _schema_with_arg_of_type(arg_type: GraphQLInputType):
11321146
BadObjectType = GraphQLObjectType(
11331147
"BadObject",
11341148
{
@@ -1141,7 +1155,7 @@ def _schema_with_arg_of_type(arg_type):
11411155
GraphQLObjectType("Query", {"f": GraphQLField(BadObjectType)})
11421156
)
11431157

1144-
@mark.parametrize("type_", input_types)
1158+
@parametrize_type(input_types)
11451159
def accepts_an_input_type_as_a_field_arg_type(type_):
11461160
schema = _schema_with_arg_of_type(type_)
11471161
assert validate_schema(schema) == []
@@ -1153,15 +1167,15 @@ def rejects_an_empty_field_arg_type():
11531167
msg = str(exc_info.value)
11541168
assert msg == "Argument type must be a GraphQL input type."
11551169

1156-
@mark.parametrize("type_", not_input_types)
1170+
@parametrize_type(not_input_types)
11571171
def rejects_a_non_input_type_as_a_field_arg_type(type_):
11581172
# invalid schema cannot be built with Python
11591173
with raises(TypeError) as exc_info:
11601174
_schema_with_arg_of_type(type_)
11611175
msg = str(exc_info.value)
11621176
assert msg == "Argument type must be a GraphQL input type."
11631177

1164-
@mark.parametrize("type_", [int, float, str])
1178+
@parametrize_type([int, float, str])
11651179
def rejects_a_non_type_value_as_a_field_arg_type(type_):
11661180
# invalid schema cannot be built with Python
11671181
with raises(TypeError) as exc_info:
@@ -1191,7 +1205,7 @@ def rejects_a_non_input_type_as_a_field_arg_with_locations():
11911205

11921206

11931207
def describe_type_system_input_object_fields_must_have_input_types():
1194-
def _schema_with_input_field_of_type(input_field_type):
1208+
def _schema_with_input_field_of_type(input_field_type: GraphQLInputType):
11951209
BadInputObjectType = GraphQLInputObjectType(
11961210
"BadInputObject", {"badField": GraphQLInputField(input_field_type)}
11971211
)
@@ -1207,7 +1221,7 @@ def _schema_with_input_field_of_type(input_field_type):
12071221
)
12081222
)
12091223

1210-
@mark.parametrize("type_", input_types)
1224+
@parametrize_type(input_types)
12111225
def accepts_an_input_type_as_an_input_fieldtype(type_):
12121226
schema = _schema_with_input_field_of_type(type_)
12131227
assert validate_schema(schema) == []
@@ -1219,15 +1233,15 @@ def rejects_an_empty_input_field_type():
12191233
msg = str(exc_info.value)
12201234
assert msg == "Input field type must be a GraphQL input type."
12211235

1222-
@mark.parametrize("type_", not_input_types)
1236+
@parametrize_type(not_input_types)
12231237
def rejects_a_non_input_type_as_an_input_field_type(type_):
12241238
# invalid schema cannot be built with Python
12251239
with raises(TypeError) as exc_info:
12261240
_schema_with_input_field_of_type(type_)
12271241
msg = str(exc_info.value)
12281242
assert msg == "Input field type must be a GraphQL input type."
12291243

1230-
@mark.parametrize("type_", [int, float, str])
1244+
@parametrize_type([int, float, str])
12311245
def rejects_a_non_type_value_as_an_input_field_type(type_):
12321246
# invalid schema cannot be built with Python
12331247
with raises(TypeError) as exc_info:

0 commit comments

Comments
 (0)