Skip to content

Commit 56cfc71

Browse files
authored
Use explicit optional type on arguments (#76)
1 parent 2c8382e commit 56cfc71

22 files changed

+158
-140
lines changed

mypy.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[mypy]
22
python_version = 3.8
33
check_untyped_defs = True
4-
no_implicit_optional = False
4+
no_implicit_optional = True
55
strict_optional = True
66
warn_redundant_casts = True
77
warn_unused_ignores = True

src/graphql/error/graphql_error.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,12 @@ class GraphQLError(Exception):
8585
def __init__(
8686
self,
8787
message: str,
88-
nodes: Union[Collection["Node"], "Node"] = None,
89-
source: "Source" = None,
90-
positions: Collection[int] = None,
91-
path: Collection[Union[str, int]] = None,
92-
original_error: Exception = None,
93-
extensions: Dict[str, Any] = None,
88+
nodes: Union[Collection["Node"], "Node", None] = None,
89+
source: Optional["Source"] = None,
90+
positions: Optional[Collection[int]] = None,
91+
path: Optional[Collection[Union[str, int]]] = None,
92+
original_error: Optional[Exception] = None,
93+
extensions: Optional[Dict[str, Any]] = None,
9494
) -> None:
9595
super().__init__(message)
9696
self.message = message

src/graphql/execution/execute.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,12 @@ def execute(
117117
document: DocumentNode,
118118
root_value: Any = None,
119119
context_value: Any = None,
120-
variable_values: Dict[str, Any] = None,
121-
operation_name: str = None,
122-
field_resolver: GraphQLFieldResolver = None,
123-
type_resolver: GraphQLTypeResolver = None,
124-
middleware: Middleware = None,
125-
execution_context_class: Type["ExecutionContext"] = None,
120+
variable_values: Optional[Dict[str, Any]] = None,
121+
operation_name: Optional[str] = None,
122+
field_resolver: Optional[GraphQLFieldResolver] = None,
123+
type_resolver: Optional[GraphQLTypeResolver] = None,
124+
middleware: Optional[Middleware] = None,
125+
execution_context_class: Optional[Type["ExecutionContext"]] = None,
126126
) -> AwaitableOrValue[ExecutionResult]:
127127
"""Execute a GraphQL operation.
128128
@@ -222,11 +222,11 @@ def build(
222222
document: DocumentNode,
223223
root_value: Any = None,
224224
context_value: Any = None,
225-
raw_variable_values: Dict[str, Any] = None,
226-
operation_name: str = None,
227-
field_resolver: GraphQLFieldResolver = None,
228-
type_resolver: GraphQLTypeResolver = None,
229-
middleware: Middleware = None,
225+
raw_variable_values: Optional[Dict[str, Any]] = None,
226+
operation_name: Optional[str] = None,
227+
field_resolver: Optional[GraphQLFieldResolver] = None,
228+
type_resolver: Optional[GraphQLTypeResolver] = None,
229+
middleware: Optional[Middleware] = None,
230230
) -> Union[List[GraphQLError], "ExecutionContext"]:
231231
"""Build an execution context
232232
@@ -1030,7 +1030,7 @@ def collect_subfields(
10301030
def assert_valid_execution_arguments(
10311031
schema: GraphQLSchema,
10321032
document: DocumentNode,
1033-
raw_variable_values: Dict[str, Any] = None,
1033+
raw_variable_values: Optional[Dict[str, Any]] = None,
10341034
) -> None:
10351035
"""Check that the arguments are acceptable.
10361036

src/graphql/execution/values.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def get_variable_values(
3737
schema: GraphQLSchema,
3838
var_def_nodes: FrozenList[VariableDefinitionNode],
3939
inputs: Dict[str, Any],
40-
max_errors: int = None,
40+
max_errors: Optional[int] = None,
4141
) -> CoercedVariableValues:
4242
"""Get coerced variable values based on provided definitions.
4343
@@ -144,7 +144,7 @@ def on_input_value_error(
144144
def get_argument_values(
145145
type_def: Union[GraphQLField, GraphQLDirective],
146146
node: Union[FieldNode, DirectiveNode],
147-
variable_values: Dict[str, Any] = None,
147+
variable_values: Optional[Dict[str, Any]] = None,
148148
) -> Dict[str, Any]:
149149
"""Get coerced argument values based on provided definitions and nodes.
150150
@@ -221,7 +221,7 @@ def get_argument_values(
221221
def get_directive_values(
222222
directive_def: GraphQLDirective,
223223
node: NodeWithDirective,
224-
variable_values: Dict[str, Any] = None,
224+
variable_values: Optional[Dict[str, Any]] = None,
225225
) -> Optional[Dict[str, Any]]:
226226
"""Get coerced argument values based on provided nodes.
227227

src/graphql/graphql.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from asyncio import ensure_future
22
from inspect import isawaitable
3-
from typing import Any, Awaitable, Dict, Union, Type, cast
3+
from typing import Any, Awaitable, Dict, Optional, Union, Type, cast
44

55
from .error import GraphQLError
66
from .execution import execute, ExecutionResult, ExecutionContext, Middleware
@@ -21,11 +21,11 @@ async def graphql(
2121
source: Union[str, Source],
2222
root_value: Any = None,
2323
context_value: Any = None,
24-
variable_values: Dict[str, Any] = None,
25-
operation_name: str = None,
26-
field_resolver: GraphQLFieldResolver = None,
27-
type_resolver: GraphQLTypeResolver = None,
28-
middleware: Middleware = None,
24+
variable_values: Optional[Dict[str, Any]] = None,
25+
operation_name: Optional[str] = None,
26+
field_resolver: Optional[GraphQLFieldResolver] = None,
27+
type_resolver: Optional[GraphQLTypeResolver] = None,
28+
middleware: Optional[Middleware] = None,
2929
execution_context_class: Type[ExecutionContext] = ExecutionContext,
3030
) -> ExecutionResult:
3131
"""Execute a GraphQL operation asynchronously.
@@ -95,11 +95,11 @@ def graphql_sync(
9595
source: Union[str, Source],
9696
root_value: Any = None,
9797
context_value: Any = None,
98-
variable_values: Dict[str, Any] = None,
99-
operation_name: str = None,
100-
field_resolver: GraphQLFieldResolver = None,
101-
type_resolver: GraphQLTypeResolver = None,
102-
middleware: Middleware = None,
98+
variable_values: Optional[Dict[str, Any]] = None,
99+
operation_name: Optional[str] = None,
100+
field_resolver: Optional[GraphQLFieldResolver] = None,
101+
type_resolver: Optional[GraphQLTypeResolver] = None,
102+
middleware: Optional[Middleware] = None,
103103
execution_context_class: Type[ExecutionContext] = ExecutionContext,
104104
) -> ExecutionResult:
105105
"""Execute a GraphQL operation synchronously.

src/graphql/language/ast.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ def __init__(
9393
end: int,
9494
line: int,
9595
column: int,
96-
prev: "Token" = None,
97-
value: str = None,
96+
prev: Optional["Token"] = None,
97+
value: Optional[str] = None,
9898
) -> None:
9999
self.kind = kind
100100
self.start, self.end = start, end

src/graphql/language/parser.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1025,7 +1025,7 @@ def expect_optional_keyword(self, value: str) -> bool:
10251025

10261026
return False
10271027

1028-
def unexpected(self, at_token: Token = None) -> GraphQLError:
1028+
def unexpected(self, at_token: Optional[Token] = None) -> GraphQLError:
10291029
"""Create an error when an unexpected lexed token is encountered."""
10301030
token = at_token or self._lexer.token
10311031
return GraphQLSyntaxError(

src/graphql/language/source.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Optional
2+
13
from .location import SourceLocation
24

35
__all__ = ["Source"]
@@ -9,7 +11,10 @@ class Source:
911
__slots__ = "body", "name", "location_offset"
1012

1113
def __init__(
12-
self, body: str, name: str = None, location_offset: SourceLocation = None
14+
self,
15+
body: str,
16+
name: Optional[str] = None,
17+
location_offset: Optional[SourceLocation] = None,
1318
) -> None:
1419
"""Initialize source input.
1520

src/graphql/pyutils/did_you_mean.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
from typing import Sequence
1+
from typing import Optional, Sequence
22

33
__all__ = ["did_you_mean"]
44

55
MAX_LENGTH = 5
66

77

8-
def did_you_mean(suggestions: Sequence[str], sub_message: str = None) -> str:
8+
def did_you_mean(suggestions: Sequence[str], sub_message: Optional[str] = None) -> str:
99
"""Given [ A, B, C ] return ' Did you mean A, B, or C?'"""
1010
if not suggestions:
1111
return ""

src/graphql/subscription/map_async_iterator.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from asyncio import Event, ensure_future, Future, wait
22
from concurrent.futures import FIRST_COMPLETED
33
from inspect import isasyncgen, isawaitable
4-
from typing import AsyncIterable, Callable, Set
4+
from typing import AsyncIterable, Callable, Optional, Set
55

66
__all__ = ["MapAsyncIterator"]
77

@@ -21,7 +21,7 @@ def __init__(
2121
self,
2222
iterable: AsyncIterable,
2323
callback: Callable,
24-
reject_callback: Callable = None,
24+
reject_callback: Optional[Callable] = None,
2525
) -> None:
2626
self.iterator = iterable.__aiter__()
2727
self.callback = callback

src/graphql/subscription/subscribe.py

+17-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
from inspect import isawaitable
2-
from typing import Any, AsyncIterable, AsyncIterator, Awaitable, Dict, Union, cast
2+
from typing import (
3+
Any,
4+
AsyncIterable,
5+
AsyncIterator,
6+
Awaitable,
7+
Dict,
8+
Optional,
9+
Union,
10+
cast,
11+
)
312

413
from ..error import GraphQLError, located_error
514
from ..execution.execute import (
@@ -23,10 +32,10 @@ async def subscribe(
2332
document: DocumentNode,
2433
root_value: Any = None,
2534
context_value: Any = None,
26-
variable_values: Dict[str, Any] = None,
27-
operation_name: str = None,
28-
field_resolver: GraphQLFieldResolver = None,
29-
subscribe_field_resolver: GraphQLFieldResolver = None,
35+
variable_values: Optional[Dict[str, Any]] = None,
36+
operation_name: Optional[str] = None,
37+
field_resolver: Optional[GraphQLFieldResolver] = None,
38+
subscribe_field_resolver: Optional[GraphQLFieldResolver] = None,
3039
) -> Union[AsyncIterator[ExecutionResult], ExecutionResult]:
3140
"""Create a GraphQL subscription.
3241
@@ -91,9 +100,9 @@ async def create_source_event_stream(
91100
document: DocumentNode,
92101
root_value: Any = None,
93102
context_value: Any = None,
94-
variable_values: Dict[str, Any] = None,
95-
operation_name: str = None,
96-
field_resolver: GraphQLFieldResolver = None,
103+
variable_values: Optional[Dict[str, Any]] = None,
104+
operation_name: Optional[str] = None,
105+
field_resolver: Optional[GraphQLFieldResolver] = None,
97106
) -> Union[AsyncIterable[Any], ExecutionResult]:
98107
"""Create source even stream
99108

0 commit comments

Comments
 (0)