1
- from typing import cast
1
+ from functools import partial
2
+ from typing import cast , List , Union
2
3
3
4
from pytest import mark , raises
4
5
8
9
GraphQLEnumValue ,
9
10
GraphQLField ,
10
11
GraphQLInputField ,
12
+ GraphQLInputType ,
11
13
GraphQLInputObjectType ,
12
14
GraphQLInterfaceType ,
13
15
GraphQLList ,
16
+ GraphQLNamedType ,
14
17
GraphQLNonNull ,
15
18
GraphQLObjectType ,
19
+ GraphQLOutputType ,
16
20
GraphQLScalarType ,
17
21
GraphQLSchema ,
18
22
GraphQLString ,
19
23
GraphQLUnionType ,
24
+ GraphQLWrappingType ,
20
25
validate_schema ,
21
26
GraphQLArgument ,
22
27
GraphQLDirective ,
@@ -56,7 +61,10 @@ def _get(value):
56
61
)
57
62
58
63
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 )
60
68
return (
61
69
types
62
70
+ [GraphQLList (t ) for t in types ]
@@ -65,7 +73,7 @@ def with_modifiers(types):
65
73
)
66
74
67
75
68
- output_types = with_modifiers (
76
+ output_types : List [ GraphQLOutputType ] = with_modifiers (
69
77
[
70
78
GraphQLString ,
71
79
SomeScalarType ,
@@ -76,13 +84,19 @@ def with_modifiers(types):
76
84
]
77
85
)
78
86
79
- not_output_types = with_modifiers ([SomeInputObjectType ])
87
+ not_output_types : List [ GraphQLInputType ] = with_modifiers ([SomeInputObjectType ])
80
88
81
- input_types = with_modifiers (
89
+ input_types : List [ GraphQLInputType ] = with_modifiers (
82
90
[GraphQLString , SomeScalarType , SomeEnumType , SomeInputObjectType ]
83
91
)
84
92
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
+ )
86
100
87
101
88
102
def schema_with_field_type (type_ ):
@@ -763,7 +777,7 @@ def schema_with_enum(name):
763
777
764
778
765
779
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 ):
767
781
BadObjectType = GraphQLObjectType (
768
782
"BadObject" , {"badField" : GraphQLField (field_type )}
769
783
)
@@ -772,7 +786,7 @@ def _schema_with_object_field_of_type(field_type):
772
786
types = [SomeObjectType ],
773
787
)
774
788
775
- @mark . parametrize ( "type_" , output_types )
789
+ @parametrize_type ( output_types )
776
790
def accepts_an_output_type_as_an_object_field_type (type_ ):
777
791
schema = _schema_with_object_field_of_type (type_ )
778
792
assert validate_schema (schema ) == []
@@ -784,15 +798,15 @@ def rejects_an_empty_object_field_type():
784
798
msg = str (exc_info .value )
785
799
assert msg == "Field type must be an output type."
786
800
787
- @mark . parametrize ( "type_" , not_output_types )
801
+ @parametrize_type ( not_output_types )
788
802
def rejects_a_non_output_type_as_an_object_field_type (type_ ):
789
803
# invalid schema cannot be built with Python
790
804
with raises (TypeError ) as exc_info :
791
805
_schema_with_object_field_of_type (type_ )
792
806
msg = str (exc_info .value )
793
807
assert msg == "Field type must be an output type."
794
808
795
- @mark . parametrize ( "type_" , [int , float , str ])
809
+ @parametrize_type ( [int , float , str ])
796
810
def rejects_a_non_type_value_as_an_object_field_type (type_ ):
797
811
# invalid schema cannot be built with Python
798
812
with raises (TypeError ) as exc_info :
@@ -1042,7 +1056,7 @@ def rejects_object_implementing_extended_interface_due_to_type_mismatch():
1042
1056
1043
1057
1044
1058
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 ):
1046
1060
BadInterfaceType = GraphQLInterfaceType (
1047
1061
"BadInterface" , {"badField" : GraphQLField (field_type )}
1048
1062
)
@@ -1056,7 +1070,7 @@ def _schema_with_interface_field_of_type(field_type):
1056
1070
types = [BadImplementingType , SomeObjectType ],
1057
1071
)
1058
1072
1059
- @mark . parametrize ( "type_" , output_types )
1073
+ @parametrize_type ( output_types )
1060
1074
def accepts_an_output_type_as_an_interface_field_type (type_ ):
1061
1075
schema = _schema_with_interface_field_of_type (type_ )
1062
1076
assert validate_schema (schema ) == []
@@ -1068,15 +1082,15 @@ def rejects_an_empty_interface_field_type():
1068
1082
msg = str (exc_info .value )
1069
1083
assert msg == "Field type must be an output type."
1070
1084
1071
- @mark . parametrize ( "type_" , not_output_types )
1085
+ @parametrize_type ( not_output_types )
1072
1086
def rejects_a_non_output_type_as_an_interface_field_type (type_ ):
1073
1087
# invalid schema cannot be built with Python
1074
1088
with raises (TypeError ) as exc_info :
1075
1089
_schema_with_interface_field_of_type (type_ )
1076
1090
msg = str (exc_info .value )
1077
1091
assert msg == "Field type must be an output type."
1078
1092
1079
- @mark . parametrize ( "type_" , [int , float , str ])
1093
+ @parametrize_type ( [int , float , str ])
1080
1094
def rejects_a_non_type_value_as_an_interface_field_type (type_ ):
1081
1095
# invalid schema cannot be built with Python
1082
1096
with raises (TypeError ) as exc_info :
@@ -1128,7 +1142,7 @@ def accepts_an_interface_not_implemented_by_at_least_one_object():
1128
1142
1129
1143
1130
1144
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 ):
1132
1146
BadObjectType = GraphQLObjectType (
1133
1147
"BadObject" ,
1134
1148
{
@@ -1141,7 +1155,7 @@ def _schema_with_arg_of_type(arg_type):
1141
1155
GraphQLObjectType ("Query" , {"f" : GraphQLField (BadObjectType )})
1142
1156
)
1143
1157
1144
- @mark . parametrize ( "type_" , input_types )
1158
+ @parametrize_type ( input_types )
1145
1159
def accepts_an_input_type_as_a_field_arg_type (type_ ):
1146
1160
schema = _schema_with_arg_of_type (type_ )
1147
1161
assert validate_schema (schema ) == []
@@ -1153,15 +1167,15 @@ def rejects_an_empty_field_arg_type():
1153
1167
msg = str (exc_info .value )
1154
1168
assert msg == "Argument type must be a GraphQL input type."
1155
1169
1156
- @mark . parametrize ( "type_" , not_input_types )
1170
+ @parametrize_type ( not_input_types )
1157
1171
def rejects_a_non_input_type_as_a_field_arg_type (type_ ):
1158
1172
# invalid schema cannot be built with Python
1159
1173
with raises (TypeError ) as exc_info :
1160
1174
_schema_with_arg_of_type (type_ )
1161
1175
msg = str (exc_info .value )
1162
1176
assert msg == "Argument type must be a GraphQL input type."
1163
1177
1164
- @mark . parametrize ( "type_" , [int , float , str ])
1178
+ @parametrize_type ( [int , float , str ])
1165
1179
def rejects_a_non_type_value_as_a_field_arg_type (type_ ):
1166
1180
# invalid schema cannot be built with Python
1167
1181
with raises (TypeError ) as exc_info :
@@ -1191,7 +1205,7 @@ def rejects_a_non_input_type_as_a_field_arg_with_locations():
1191
1205
1192
1206
1193
1207
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 ):
1195
1209
BadInputObjectType = GraphQLInputObjectType (
1196
1210
"BadInputObject" , {"badField" : GraphQLInputField (input_field_type )}
1197
1211
)
@@ -1207,7 +1221,7 @@ def _schema_with_input_field_of_type(input_field_type):
1207
1221
)
1208
1222
)
1209
1223
1210
- @mark . parametrize ( "type_" , input_types )
1224
+ @parametrize_type ( input_types )
1211
1225
def accepts_an_input_type_as_an_input_fieldtype (type_ ):
1212
1226
schema = _schema_with_input_field_of_type (type_ )
1213
1227
assert validate_schema (schema ) == []
@@ -1219,15 +1233,15 @@ def rejects_an_empty_input_field_type():
1219
1233
msg = str (exc_info .value )
1220
1234
assert msg == "Input field type must be a GraphQL input type."
1221
1235
1222
- @mark . parametrize ( "type_" , not_input_types )
1236
+ @parametrize_type ( not_input_types )
1223
1237
def rejects_a_non_input_type_as_an_input_field_type (type_ ):
1224
1238
# invalid schema cannot be built with Python
1225
1239
with raises (TypeError ) as exc_info :
1226
1240
_schema_with_input_field_of_type (type_ )
1227
1241
msg = str (exc_info .value )
1228
1242
assert msg == "Input field type must be a GraphQL input type."
1229
1243
1230
- @mark . parametrize ( "type_" , [int , float , str ])
1244
+ @parametrize_type ( [int , float , str ])
1231
1245
def rejects_a_non_type_value_as_an_input_field_type (type_ ):
1232
1246
# invalid schema cannot be built with Python
1233
1247
with raises (TypeError ) as exc_info :
0 commit comments