Skip to content

Commit ae8a3bf

Browse files
committed
Add type hints in several test files and cleanup
Replicates graphql/graphql-js@c1ca546
1 parent 2e5ab8e commit ae8a3bf

File tree

5 files changed

+38
-22
lines changed

5 files changed

+38
-22
lines changed

tests/execution/test_abstract.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from collections import namedtuple
1+
from typing import NamedTuple
22

33
from graphql import graphql_sync
44
from graphql.error import format_error
@@ -13,9 +13,22 @@
1313
GraphQLUnionType,
1414
)
1515

16-
Dog = namedtuple("Dog", "name woofs")
17-
Cat = namedtuple("Cat", "name meows")
18-
Human = namedtuple("Human", "name")
16+
17+
class Dog(NamedTuple):
18+
19+
name: str
20+
woofs: bool
21+
22+
23+
class Cat(NamedTuple):
24+
25+
name: str
26+
meows: bool
27+
28+
29+
class Human(NamedTuple):
30+
31+
name: str
1932

2033

2134
def get_is_type_of(type_):
@@ -322,7 +335,7 @@ def returning_invalid_value_from_resolve_type_yields_useful_error():
322335
resolve_type=lambda *_args: [],
323336
)
324337

325-
fooObject = GraphQLObjectType(
338+
foo_object = GraphQLObjectType(
326339
"FooObject", {"bar": GraphQLField(GraphQLString)}, interfaces=[fooInterface]
327340
)
328341

@@ -331,7 +344,7 @@ def returning_invalid_value_from_resolve_type_yields_useful_error():
331344
"Query",
332345
{"foo": GraphQLField(fooInterface, resolve=lambda *_args: "dummy")},
333346
),
334-
types=[fooObject],
347+
types=[foo_object],
335348
)
336349

337350
result = graphql_sync(schema, "{ foo { bar } }")

tests/language/test_lexer.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ def assert_syntax_error(text, message, location):
1414
with raises(GraphQLSyntaxError) as exc_info:
1515
lex_one(text)
1616
error = exc_info.value
17-
assert message in error.message
17+
assert error.message == f"Syntax Error: {message}"
1818
assert error.locations == [location]
1919

2020

2121
def describe_lexer():
2222
def disallows_uncommon_control_characters():
2323
assert_syntax_error(
24-
"\x07", "Cannot contain the invalid character '\\x07'", (1, 1)
24+
"\x07", "Cannot contain the invalid character '\\x07'.", (1, 1)
2525
)
2626

2727
# noinspection PyArgumentEqualDefault
@@ -141,8 +141,8 @@ def lex_reports_useful_string_errors():
141141
"Invalid character within String: '\\x00'.",
142142
(1, 19),
143143
)
144-
assert_syntax_error('"multi\nline"', "Unterminated string", (1, 7))
145-
assert_syntax_error('"multi\rline"', "Unterminated string", (1, 7))
144+
assert_syntax_error('"multi\nline"', "Unterminated string.", (1, 7))
145+
assert_syntax_error('"multi\rline"', "Unterminated string.", (1, 7))
146146
assert_syntax_error(
147147
'"bad \\x esc"', "Invalid character escape sequence: '\\x'.", (1, 7)
148148
)
@@ -258,7 +258,9 @@ def lex_reports_useful_number_errors():
258258
assert_syntax_error(
259259
"1.e1", "Invalid number, expected digit but got: 'e'.", (1, 3)
260260
)
261-
assert_syntax_error(".123", "Cannot parse the unexpected character '.'", (1, 1))
261+
assert_syntax_error(
262+
".123", "Cannot parse the unexpected character '.'.", (1, 1)
263+
)
262264
assert_syntax_error(
263265
"1.A", "Invalid number, expected digit but got: 'A'.", (1, 3)
264266
)
@@ -289,13 +291,13 @@ def lexes_punctuation():
289291
assert lex_one("|") == Token(TokenKind.PIPE, 0, 1, 1, 1, None, None)
290292

291293
def lex_reports_useful_unknown_character_error():
292-
assert_syntax_error("..", "Cannot parse the unexpected character '.'", (1, 1))
293-
assert_syntax_error("?", "Cannot parse the unexpected character '?'", (1, 1))
294+
assert_syntax_error("..", "Cannot parse the unexpected character '.'.", (1, 1))
295+
assert_syntax_error("?", "Cannot parse the unexpected character '?'.", (1, 1))
294296
assert_syntax_error(
295-
"\u203B", "Cannot parse the unexpected character '\u203B'", (1, 1)
297+
"\u203B", "Cannot parse the unexpected character '\u203B'.", (1, 1)
296298
)
297299
assert_syntax_error(
298-
"\u200b", "Cannot parse the unexpected character '\\u200b'", (1, 1)
300+
"\u200b", "Cannot parse the unexpected character '\\u200b'.", (1, 1)
299301
)
300302

301303
# noinspection PyArgumentEqualDefault

tests/type/test_enum.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
GraphQLArgument,
66
GraphQLBoolean,
77
GraphQLEnumType,
8+
GraphQLEnumValue,
89
GraphQLField,
910
GraphQLInt,
1011
GraphQLObjectType,
@@ -300,10 +301,12 @@ def enum_inputs_may_be_nullable():
300301

301302
def presents_a_values_property_for_complex_enums():
302303
values = ComplexEnum.values
303-
assert len(values) == 2
304304
assert isinstance(values, dict)
305-
assert values["ONE"].value is complex1
306-
assert values["TWO"].value is complex2
305+
assert all(isinstance(value, GraphQLEnumValue) for value in values.values())
306+
assert {key: value.value for key, value in values.items()} == {
307+
"ONE": complex1,
308+
"TWO": complex2,
309+
}
307310

308311
def may_be_internally_represented_with_complex_values():
309312
result = execute_query(

tests/type/test_introspection.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -871,9 +871,7 @@ def introspects_on_input_object():
871871
"TestType",
872872
{
873873
"field": GraphQLField(
874-
GraphQLString,
875-
args={"complex": GraphQLArgument(TestInputObject)},
876-
resolve=lambda obj, info, **args: repr(args.get("complex")),
874+
GraphQLString, args={"complex": GraphQLArgument(TestInputObject)}
877875
)
878876
},
879877
)

tests/type/test_predicate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
)
5454

5555
ObjectType = GraphQLObjectType("Object", {})
56-
InterfaceType = GraphQLInterfaceType("Interface")
56+
InterfaceType = GraphQLInterfaceType("Interface", {})
5757
UnionType = GraphQLUnionType("Union", types=[ObjectType])
5858
EnumType = GraphQLEnumType("Enum", values={"foo": {}})
5959
InputObjectType = GraphQLInputObjectType("InputObject", {})

0 commit comments

Comments
 (0)