Skip to content

Commit a7152f9

Browse files
committed
Add all missing type definitions (#89)
1 parent 57c6e2a commit a7152f9

File tree

72 files changed

+537
-344
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+537
-344
lines changed

mypy.ini

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,19 @@ no_implicit_optional = True
55
strict_optional = True
66
warn_redundant_casts = True
77
warn_unused_ignores = True
8+
disallow_untyped_defs = True
9+
10+
[mypy-graphql.language.printer]
11+
disallow_untyped_defs = False
12+
13+
[mypy-graphql.pyutils.frozen_dict]
14+
disallow_untyped_defs = False
15+
16+
[mypy-graphql.pyutils.frozen_list]
17+
disallow_untyped_defs = False
18+
19+
[mypy-graphql.type.introspection]
20+
disallow_untyped_defs = False
21+
22+
[mypy-tests.*]
23+
disallow_untyped_defs = False

src/graphql/error/graphql_error.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,10 @@ def __init__(
136136
if not self.__traceback__:
137137
self.__traceback__ = exc_info()[2]
138138

139-
def __str__(self):
139+
def __str__(self) -> str:
140140
return print_error(self)
141141

142-
def __repr__(self):
142+
def __repr__(self) -> str:
143143
args = [repr(self.message)]
144144
if self.locations:
145145
args.append(f"locations={self.locations!r}")
@@ -149,7 +149,7 @@ def __repr__(self):
149149
args.append(f"extensions={self.extensions!r}")
150150
return f"{self.__class__.__name__}({', '.join(args)})"
151151

152-
def __eq__(self, other):
152+
def __eq__(self, other: Any) -> bool:
153153
return (
154154
isinstance(other, GraphQLError)
155155
and self.__class__ == other.__class__
@@ -165,11 +165,11 @@ def __eq__(self, other):
165165
)
166166
)
167167

168-
def __ne__(self, other):
168+
def __ne__(self, other: Any) -> bool:
169169
return not self == other
170170

171171
@property
172-
def formatted(self):
172+
def formatted(self) -> Dict[str, Any]:
173173
"""Get error formatted according to the specification."""
174174
return format_error(self)
175175

src/graphql/error/syntax_error.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1+
from typing import TYPE_CHECKING
2+
13
from .graphql_error import GraphQLError
24

5+
if TYPE_CHECKING:
6+
from ..language.source import Source # noqa: F401
7+
38
__all__ = ["GraphQLSyntaxError"]
49

510

611
class GraphQLSyntaxError(GraphQLError):
712
"""A GraphQLError representing a syntax error."""
813

9-
def __init__(self, source, position, description):
14+
def __init__(self, source: "Source", position: int, description: str) -> None:
1015
super().__init__(
1116
f"Syntax Error: {description}", source=source, positions=[position]
1217
)

src/graphql/execution/execute.py

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ def build_response(
252252
"""
253253
if self.is_awaitable(data):
254254

255-
async def build_response_async():
255+
async def build_response_async() -> ExecutionResult:
256256
return self.build_response(await data) # type: ignore
257257

258258
return build_response_async()
@@ -285,6 +285,7 @@ def execute_operation(
285285
#
286286
# Similar to complete_value_catching_error.
287287
try:
288+
# noinspection PyArgumentList
288289
result = (
289290
self.execute_fields_serially
290291
if operation.operation == OperationType.MUTATION
@@ -296,7 +297,7 @@ def execute_operation(
296297
else:
297298
if self.is_awaitable(result):
298299
# noinspection PyShadowingNames
299-
async def await_result():
300+
async def await_result() -> Any:
300301
try:
301302
return await result # type: ignore
302303
except GraphQLError as error:
@@ -316,7 +317,7 @@ def execute_fields_serially(
316317
317318
Implements the "Evaluating selection sets" section of the spec for "write" mode.
318319
"""
319-
results: Dict[str, Any] = {}
320+
results: AwaitableOrValue[Dict[str, Any]] = {}
320321
is_awaitable = self.is_awaitable
321322
for response_name, field_nodes in fields.items():
322323
field_path = Path(path, response_name)
@@ -327,30 +328,36 @@ def execute_fields_serially(
327328
continue
328329
if is_awaitable(results):
329330
# noinspection PyShadowingNames
330-
async def await_and_set_result(results, response_name, result):
331+
async def await_and_set_result(
332+
results: Awaitable[Dict[str, Any]],
333+
response_name: str,
334+
result: AwaitableOrValue[Any],
335+
) -> Dict[str, Any]:
331336
awaited_results = await results
332337
awaited_results[response_name] = (
333338
await result if is_awaitable(result) else result
334339
)
335340
return awaited_results
336341

337-
# noinspection PyTypeChecker
338342
results = await_and_set_result(
339343
cast(Awaitable, results), response_name, result
340344
)
341345
elif is_awaitable(result):
342346
# noinspection PyShadowingNames
343-
async def set_result(results, response_name, result):
347+
async def set_result(
348+
results: Dict[str, Any], response_name: str, result: Awaitable,
349+
) -> Dict[str, Any]:
344350
results[response_name] = await result
345351
return results
346352

347-
# noinspection PyTypeChecker
348-
results = set_result(results, response_name, result)
353+
results = set_result(
354+
cast(Dict[str, Any], results), response_name, result
355+
)
349356
else:
350-
results[response_name] = result
357+
cast(Dict[str, Any], results)[response_name] = result
351358
if is_awaitable(results):
352359
# noinspection PyShadowingNames
353-
async def get_results():
360+
async def get_results() -> Any:
354361
return await cast(Awaitable, results)
355362

356363
return get_results()
@@ -389,7 +396,7 @@ def execute_fields(
389396
# field, which is possibly a coroutine object. Return a coroutine object that
390397
# will yield this same map, but with any coroutines awaited in parallel and
391398
# replaced with the values they yielded.
392-
async def get_results():
399+
async def get_results() -> Dict[str, Any]:
393400
results.update(
394401
zip(
395402
awaitable_fields,
@@ -579,7 +586,7 @@ def resolve_field_value_or_error(
579586
result = resolve_fn(source, info, **args)
580587
if self.is_awaitable(result):
581588
# noinspection PyShadowingNames
582-
async def await_result():
589+
async def await_result() -> Any:
583590
try:
584591
return await result
585592
except GraphQLError as error:
@@ -607,10 +614,11 @@ def complete_value_catching_error(
607614
This is a small wrapper around completeValue which detects and logs errors in
608615
the execution context.
609616
"""
617+
completed: AwaitableOrValue[Any]
610618
try:
611619
if self.is_awaitable(result):
612620

613-
async def await_result():
621+
async def await_result() -> Any:
614622
value = self.complete_value(
615623
return_type, field_nodes, info, path, await result
616624
)
@@ -625,7 +633,7 @@ async def await_result():
625633
)
626634
if self.is_awaitable(completed):
627635
# noinspection PyShadowingNames
628-
async def await_completed():
636+
async def await_completed() -> Any:
629637
try:
630638
return await completed
631639
except Exception as error:
@@ -783,7 +791,7 @@ def complete_list_value(
783791
return completed_results
784792

785793
# noinspection PyShadowingNames
786-
async def get_completed_results():
794+
async def get_completed_results() -> Any:
787795
for index, result in zip(
788796
awaitable_indices,
789797
await gather(
@@ -828,7 +836,7 @@ def complete_abstract_value(
828836

829837
if self.is_awaitable(runtime_type):
830838

831-
async def await_complete_object_value():
839+
async def await_complete_object_value() -> Any:
832840
value = self.complete_object_value(
833841
self.ensure_valid_runtime_type(
834842
await runtime_type, # type: ignore
@@ -912,14 +920,14 @@ def complete_object_value(
912920

913921
if self.is_awaitable(is_type_of):
914922

915-
async def collect_and_execute_subfields_async():
923+
async def collect_and_execute_subfields_async() -> Dict[str, Any]:
916924
if not await is_type_of: # type: ignore
917925
raise invalid_return_type_error(
918926
return_type, result, field_nodes
919927
)
920928
return self.collect_and_execute_subfields(
921929
return_type, field_nodes, path, result
922-
)
930+
) # type: ignore
923931

924932
return collect_and_execute_subfields_async()
925933

@@ -1158,11 +1166,12 @@ def default_type_resolver(
11581166

11591167
if awaitable_is_type_of_results:
11601168
# noinspection PyShadowingNames
1161-
async def get_type():
1169+
async def get_type() -> Optional[Union[GraphQLObjectType, str]]:
11621170
is_type_of_results = await gather(*awaitable_is_type_of_results)
11631171
for is_type_of_result, type_ in zip(is_type_of_results, awaitable_types):
11641172
if is_type_of_result:
11651173
return type_
1174+
return None
11661175

11671176
return get_type()
11681177

src/graphql/execution/values.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def get_variable_values(
5151
"""
5252
errors: List[GraphQLError] = []
5353

54-
def on_error(error: GraphQLError):
54+
def on_error(error: GraphQLError) -> None:
5555
if max_errors is not None and len(errors) >= max_errors:
5656
raise GraphQLError(
5757
"Too many errors processing variables,"
@@ -74,7 +74,7 @@ def coerce_variable_values(
7474
var_def_nodes: FrozenList[VariableDefinitionNode],
7575
inputs: Dict[str, Any],
7676
on_error: Callable[[GraphQLError], None],
77-
):
77+
) -> Dict[str, Any]:
7878
coerced_values: Dict[str, Any] = {}
7979
for var_def_node in var_def_nodes:
8080
var_name = var_def_node.variable.name.value
@@ -123,7 +123,7 @@ def coerce_variable_values(
123123

124124
def on_input_value_error(
125125
path: List[Union[str, int]], invalid_value: Any, error: GraphQLError
126-
):
126+
) -> None:
127127
invalid_str = inspect(invalid_value)
128128
prefix = f"Variable '${var_name}' got invalid value {invalid_str}"
129129
if path:

0 commit comments

Comments
 (0)