Skip to content

Commit ce61c88

Browse files
committed
validation-test: Improve typings
Replicates graphql/graphql-js@6012d28
1 parent 6f8a10b commit ce61c88

File tree

1 file changed

+57
-53
lines changed

1 file changed

+57
-53
lines changed

tests/type/test_validation.py

Lines changed: 57 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,7 @@ def with_modifiers(
115115

116116
def schema_with_field_type(type_):
117117
return GraphQLSchema(
118-
query=GraphQLObjectType(name="Query", fields={"f": GraphQLField(type_)}),
119-
types=[type_],
118+
query=GraphQLObjectType(name="Query", fields={"f": GraphQLField(type_)})
120119
)
121120

122121

@@ -520,17 +519,19 @@ def accepts_field_args_with_valid_names():
520519
)
521520
assert validate_schema(schema) == []
522521

523-
def reject_field_args_with_invalid_names():
524-
QueryType = GraphQLObjectType(
525-
"SomeObject",
526-
{
527-
"badField": GraphQLField(
528-
GraphQLString,
529-
args={"bad-name-with-dashes": GraphQLArgument(GraphQLString)},
530-
)
531-
},
522+
def rejects_field_args_with_invalid_names():
523+
schema = schema_with_field_type(
524+
GraphQLObjectType(
525+
"SomeObject",
526+
{
527+
"badField": GraphQLField(
528+
GraphQLString,
529+
args={"bad-name-with-dashes": GraphQLArgument(GraphQLString)},
530+
)
531+
},
532+
)
532533
)
533-
schema = GraphQLSchema(QueryType)
534+
534535
msg = validate_schema(schema)[0].message
535536
assert msg == (
536537
"Names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/"
@@ -971,7 +972,7 @@ def rejects_an_enum_type_without_values():
971972
]
972973

973974
def rejects_an_enum_type_with_incorrectly_named_values():
974-
def schema_with_enum(name):
975+
def schema_with_enum(name: str) -> GraphQLSchema:
975976
return schema_with_field_type(
976977
GraphQLEnumType("SomeEnum", {name: GraphQLEnumValue(1)})
977978
)
@@ -1008,17 +1009,17 @@ def schema_with_enum(name):
10081009

10091010

10101011
def describe_type_system_object_fields_must_have_output_types():
1011-
def _schema_with_object_field_of_type(field_type: GraphQLOutputType):
1012-
if is_output_type(field_type):
1013-
field = GraphQLField(field_type)
1012+
def _schema_with_object_field(type_: GraphQLOutputType) -> GraphQLSchema:
1013+
if is_output_type(type_):
1014+
field = GraphQLField(type_)
10141015
else:
10151016
# invalid field cannot be built with Python directly
10161017
with raises(TypeError) as exc_info:
1017-
GraphQLField(field_type)
1018+
GraphQLField(type_)
10181019
assert str(exc_info.value) == "Field type must be an output type."
10191020
# therefore we need to monkey-patch a valid field
10201021
field = GraphQLField(GraphQLString)
1021-
field.type = field_type
1022+
field.type = type_
10221023
bad_object_type = GraphQLObjectType("BadObject", {"badField": field})
10231024
return GraphQLSchema(
10241025
GraphQLObjectType("Query", {"f": GraphQLField(bad_object_type)}),
@@ -1027,12 +1028,12 @@ def _schema_with_object_field_of_type(field_type: GraphQLOutputType):
10271028

10281029
@mark.parametrize("type_", output_types, ids=get_name)
10291030
def accepts_an_output_type_as_an_object_field_type(type_):
1030-
schema = _schema_with_object_field_of_type(type_)
1031+
schema = _schema_with_object_field(type_)
10311032
assert validate_schema(schema) == []
10321033

10331034
def rejects_an_empty_object_field_type():
10341035
# noinspection PyTypeChecker
1035-
schema = _schema_with_object_field_of_type(None) # type: ignore
1036+
schema = _schema_with_object_field(None) # type: ignore
10361037
assert validate_schema(schema) == [
10371038
{
10381039
"message": "The type of BadObject.badField must be Output Type"
@@ -1042,7 +1043,7 @@ def rejects_an_empty_object_field_type():
10421043

10431044
@mark.parametrize("type_", not_output_types, ids=get_name)
10441045
def rejects_a_non_output_type_as_an_object_field_type(type_):
1045-
schema = _schema_with_object_field_of_type(type_)
1046+
schema = _schema_with_object_field(type_)
10461047
assert validate_schema(schema) == [
10471048
{
10481049
"message": "The type of BadObject.badField must be Output Type"
@@ -1052,7 +1053,7 @@ def rejects_a_non_output_type_as_an_object_field_type(type_):
10521053

10531054
@mark.parametrize("type_", not_graphql_types, ids=get_name)
10541055
def rejects_a_non_type_value_as_an_object_field_type(type_):
1055-
schema = _schema_with_object_field_of_type(type_)
1056+
schema = _schema_with_object_field(type_)
10561057
assert validate_schema(schema) == [
10571058
{
10581059
"message": "The type of BadObject.badField must be Output Type"
@@ -1325,21 +1326,23 @@ def rejects_object_implementing_extended_interface_due_to_type_mismatch():
13251326

13261327

13271328
def describe_type_system_interface_fields_must_have_output_types():
1328-
def _schema_with_interface_field_of_type(field_type: GraphQLOutputType):
1329-
if is_output_type(field_type):
1330-
field = GraphQLField(field_type)
1329+
def _schema_with_interface_field(type_: GraphQLOutputType) -> GraphQLSchema:
1330+
if is_output_type(type_):
1331+
field = GraphQLField(type_)
13311332
else:
13321333
# invalid field cannot be built with Python directly
13331334
with raises(TypeError) as exc_info:
1334-
GraphQLField(field_type)
1335+
GraphQLField(type_)
13351336
assert str(exc_info.value) == "Field type must be an output type."
13361337
# therefore we need to monkey-patch a valid field
13371338
field = GraphQLField(GraphQLString)
1338-
field.type = field_type
1339-
bad_interface_type = GraphQLInterfaceType("BadInterface", {"badField": field})
1339+
field.type = type_
1340+
fields = {"badField": field}
1341+
1342+
bad_interface_type = GraphQLInterfaceType("BadInterface", fields)
13401343
bad_implementing_type = GraphQLObjectType(
13411344
"BadImplementing",
1342-
{"badField": field},
1345+
fields,
13431346
interfaces=[bad_interface_type],
13441347
)
13451348
return GraphQLSchema(
@@ -1349,12 +1352,12 @@ def _schema_with_interface_field_of_type(field_type: GraphQLOutputType):
13491352

13501353
@mark.parametrize("type_", output_types, ids=get_name)
13511354
def accepts_an_output_type_as_an_interface_field_type(type_):
1352-
schema = _schema_with_interface_field_of_type(type_)
1355+
schema = _schema_with_interface_field(type_)
13531356
assert validate_schema(schema) == []
13541357

13551358
def rejects_an_empty_interface_field_type():
13561359
# noinspection PyTypeChecker
1357-
schema = _schema_with_interface_field_of_type(None) # type: ignore
1360+
schema = _schema_with_interface_field(None) # type: ignore
13581361
assert validate_schema(schema) == [
13591362
{
13601363
"message": "The type of BadImplementing.badField must be Output Type"
@@ -1368,7 +1371,7 @@ def rejects_an_empty_interface_field_type():
13681371

13691372
@mark.parametrize("type_", not_output_types, ids=get_name)
13701373
def rejects_a_non_output_type_as_an_interface_field_type(type_):
1371-
schema = _schema_with_interface_field_of_type(type_)
1374+
schema = _schema_with_interface_field(type_)
13721375
assert validate_schema(schema) == [
13731376
{
13741377
"message": "The type of BadImplementing.badField must be Output Type"
@@ -1382,7 +1385,7 @@ def rejects_a_non_output_type_as_an_interface_field_type(type_):
13821385

13831386
@mark.parametrize("type_", not_graphql_types, ids=get_name)
13841387
def rejects_a_non_type_value_as_an_interface_field_type(type_):
1385-
schema = _schema_with_interface_field_of_type(type_)
1388+
schema = _schema_with_interface_field(type_)
13861389
assert validate_schema(schema) == [
13871390
{
13881391
"message": "The type of BadImplementing.badField must be Output Type"
@@ -1476,40 +1479,41 @@ def accepts_an_interface_not_implemented_by_at_least_one_object():
14761479

14771480

14781481
def describe_type_system_arguments_must_have_input_types():
1479-
def _schema_with_arg_of_type(arg_type: GraphQLInputType):
1480-
if is_input_type(arg_type):
1481-
argument = GraphQLArgument(arg_type)
1482+
def _schema_with_arg(type_: GraphQLInputType) -> GraphQLSchema:
1483+
if is_input_type(type_):
1484+
argument = GraphQLArgument(type_)
14821485
else:
14831486
# invalid argument cannot be built with Python directly
14841487
with raises(TypeError) as exc_info:
1485-
GraphQLArgument(arg_type)
1488+
GraphQLArgument(type_)
14861489
assert str(exc_info.value) == "Argument type must be a GraphQL input type."
14871490
# therefore we need to monkey-patch a valid argument
14881491
argument = GraphQLArgument(GraphQLString)
1489-
argument.type = arg_type
1492+
argument.type = type_
1493+
args = {"badArg": argument}
14901494
bad_object_type = GraphQLObjectType(
14911495
"BadObject",
1492-
{"badField": GraphQLField(GraphQLString, args={"badArg": argument})},
1496+
{"badField": GraphQLField(GraphQLString, args)},
14931497
)
14941498
return GraphQLSchema(
14951499
GraphQLObjectType("Query", {"f": GraphQLField(bad_object_type)}),
14961500
directives=[
14971501
GraphQLDirective(
14981502
"BadDirective",
14991503
[DirectiveLocation.QUERY],
1500-
{"badArg": argument},
1504+
args,
15011505
)
15021506
],
15031507
)
15041508

15051509
@mark.parametrize("type_", input_types, ids=get_name)
15061510
def accepts_an_input_type_as_a_field_arg_type(type_):
1507-
schema = _schema_with_arg_of_type(type_)
1511+
schema = _schema_with_arg(type_)
15081512
assert validate_schema(schema) == []
15091513

15101514
def rejects_an_empty_field_arg_type():
15111515
# noinspection PyTypeChecker
1512-
schema = _schema_with_arg_of_type(None) # type: ignore
1516+
schema = _schema_with_arg(None) # type: ignore
15131517
assert validate_schema(schema) == [
15141518
{
15151519
"message": "The type of @BadDirective(badArg:) must be Input Type"
@@ -1523,7 +1527,7 @@ def rejects_an_empty_field_arg_type():
15231527

15241528
@mark.parametrize("type_", not_input_types, ids=get_name)
15251529
def rejects_a_non_input_type_as_a_field_arg_type(type_):
1526-
schema = _schema_with_arg_of_type(type_)
1530+
schema = _schema_with_arg(type_)
15271531
assert validate_schema(schema) == [
15281532
{
15291533
"message": "The type of @BadDirective(badArg:) must be Input Type"
@@ -1537,7 +1541,7 @@ def rejects_a_non_input_type_as_a_field_arg_type(type_):
15371541

15381542
@mark.parametrize("type_", not_graphql_types, ids=get_name)
15391543
def rejects_a_non_type_value_as_a_field_arg_type(type_):
1540-
schema = _schema_with_arg_of_type(type_)
1544+
schema = _schema_with_arg(type_)
15411545
assert validate_schema(schema) == [
15421546
{
15431547
"message": "The type of @BadDirective(badArg:) must be Input Type"
@@ -1592,19 +1596,19 @@ def rejects_a_non_input_type_as_a_field_arg_with_locations():
15921596

15931597

15941598
def describe_type_system_input_object_fields_must_have_input_types():
1595-
def _schema_with_input_field_of_type(input_field_type: GraphQLInputType):
1596-
if is_input_type(input_field_type):
1597-
input_field = GraphQLInputField(input_field_type)
1599+
def _schema_with_input_field(type_: GraphQLInputType) -> GraphQLSchema:
1600+
if is_input_type(type_):
1601+
input_field = GraphQLInputField(type_)
15981602
else:
15991603
# invalid input field cannot be built with Python directly
16001604
with raises(TypeError) as exc_info:
1601-
GraphQLInputField(input_field_type)
1605+
GraphQLInputField(type_)
16021606
assert str(exc_info.value) == (
16031607
"Input field type must be a GraphQL input type."
16041608
)
16051609
# therefore we need to monkey-patch a valid input field
16061610
input_field = GraphQLInputField(GraphQLString)
1607-
input_field.type = input_field_type
1611+
input_field.type = type_
16081612
bad_input_object_type = GraphQLInputObjectType(
16091613
"BadInputObject", {"badField": input_field}
16101614
)
@@ -1622,12 +1626,12 @@ def _schema_with_input_field_of_type(input_field_type: GraphQLInputType):
16221626

16231627
@mark.parametrize("type_", input_types, ids=get_name)
16241628
def accepts_an_input_type_as_an_input_field_type(type_):
1625-
schema = _schema_with_input_field_of_type(type_)
1629+
schema = _schema_with_input_field(type_)
16261630
assert validate_schema(schema) == []
16271631

16281632
def rejects_an_empty_input_field_type():
16291633
# noinspection PyTypeChecker
1630-
schema = _schema_with_input_field_of_type(None) # type: ignore
1634+
schema = _schema_with_input_field(None) # type: ignore
16311635
assert validate_schema(schema) == [
16321636
{
16331637
"message": "The type of BadInputObject.badField must be Input Type"
@@ -1637,7 +1641,7 @@ def rejects_an_empty_input_field_type():
16371641

16381642
@mark.parametrize("type_", not_input_types, ids=get_name)
16391643
def rejects_a_non_input_type_as_an_input_field_type(type_):
1640-
schema = _schema_with_input_field_of_type(type_)
1644+
schema = _schema_with_input_field(type_)
16411645
assert validate_schema(schema) == [
16421646
{
16431647
"message": "The type of BadInputObject.badField must be Input Type"
@@ -1647,7 +1651,7 @@ def rejects_a_non_input_type_as_an_input_field_type(type_):
16471651

16481652
@mark.parametrize("type_", not_graphql_types, ids=get_name)
16491653
def rejects_a_non_type_value_as_an_input_field_type(type_):
1650-
schema = _schema_with_input_field_of_type(type_)
1654+
schema = _schema_with_input_field(type_)
16511655
assert validate_schema(schema) == [
16521656
{
16531657
"message": "The type of BadInputObject.badField must be Input Type"

0 commit comments

Comments
 (0)