Skip to content

Commit

Permalink
Use explicit optional type on arguments (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
hoefling authored Feb 6, 2020
1 parent 2c8382e commit 56cfc71
Show file tree
Hide file tree
Showing 22 changed files with 158 additions and 140 deletions.
2 changes: 1 addition & 1 deletion mypy.ini
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[mypy]
python_version = 3.8
check_untyped_defs = True
no_implicit_optional = False
no_implicit_optional = True
strict_optional = True
warn_redundant_casts = True
warn_unused_ignores = True
12 changes: 6 additions & 6 deletions src/graphql/error/graphql_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ class GraphQLError(Exception):
def __init__(
self,
message: str,
nodes: Union[Collection["Node"], "Node"] = None,
source: "Source" = None,
positions: Collection[int] = None,
path: Collection[Union[str, int]] = None,
original_error: Exception = None,
extensions: Dict[str, Any] = None,
nodes: Union[Collection["Node"], "Node", None] = None,
source: Optional["Source"] = None,
positions: Optional[Collection[int]] = None,
path: Optional[Collection[Union[str, int]]] = None,
original_error: Optional[Exception] = None,
extensions: Optional[Dict[str, Any]] = None,
) -> None:
super().__init__(message)
self.message = message
Expand Down
24 changes: 12 additions & 12 deletions src/graphql/execution/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,12 @@ def execute(
document: DocumentNode,
root_value: Any = None,
context_value: Any = None,
variable_values: Dict[str, Any] = None,
operation_name: str = None,
field_resolver: GraphQLFieldResolver = None,
type_resolver: GraphQLTypeResolver = None,
middleware: Middleware = None,
execution_context_class: Type["ExecutionContext"] = None,
variable_values: Optional[Dict[str, Any]] = None,
operation_name: Optional[str] = None,
field_resolver: Optional[GraphQLFieldResolver] = None,
type_resolver: Optional[GraphQLTypeResolver] = None,
middleware: Optional[Middleware] = None,
execution_context_class: Optional[Type["ExecutionContext"]] = None,
) -> AwaitableOrValue[ExecutionResult]:
"""Execute a GraphQL operation.
Expand Down Expand Up @@ -222,11 +222,11 @@ def build(
document: DocumentNode,
root_value: Any = None,
context_value: Any = None,
raw_variable_values: Dict[str, Any] = None,
operation_name: str = None,
field_resolver: GraphQLFieldResolver = None,
type_resolver: GraphQLTypeResolver = None,
middleware: Middleware = None,
raw_variable_values: Optional[Dict[str, Any]] = None,
operation_name: Optional[str] = None,
field_resolver: Optional[GraphQLFieldResolver] = None,
type_resolver: Optional[GraphQLTypeResolver] = None,
middleware: Optional[Middleware] = None,
) -> Union[List[GraphQLError], "ExecutionContext"]:
"""Build an execution context
Expand Down Expand Up @@ -1030,7 +1030,7 @@ def collect_subfields(
def assert_valid_execution_arguments(
schema: GraphQLSchema,
document: DocumentNode,
raw_variable_values: Dict[str, Any] = None,
raw_variable_values: Optional[Dict[str, Any]] = None,
) -> None:
"""Check that the arguments are acceptable.
Expand Down
6 changes: 3 additions & 3 deletions src/graphql/execution/values.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def get_variable_values(
schema: GraphQLSchema,
var_def_nodes: FrozenList[VariableDefinitionNode],
inputs: Dict[str, Any],
max_errors: int = None,
max_errors: Optional[int] = None,
) -> CoercedVariableValues:
"""Get coerced variable values based on provided definitions.
Expand Down Expand Up @@ -144,7 +144,7 @@ def on_input_value_error(
def get_argument_values(
type_def: Union[GraphQLField, GraphQLDirective],
node: Union[FieldNode, DirectiveNode],
variable_values: Dict[str, Any] = None,
variable_values: Optional[Dict[str, Any]] = None,
) -> Dict[str, Any]:
"""Get coerced argument values based on provided definitions and nodes.
Expand Down Expand Up @@ -221,7 +221,7 @@ def get_argument_values(
def get_directive_values(
directive_def: GraphQLDirective,
node: NodeWithDirective,
variable_values: Dict[str, Any] = None,
variable_values: Optional[Dict[str, Any]] = None,
) -> Optional[Dict[str, Any]]:
"""Get coerced argument values based on provided nodes.
Expand Down
22 changes: 11 additions & 11 deletions src/graphql/graphql.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from asyncio import ensure_future
from inspect import isawaitable
from typing import Any, Awaitable, Dict, Union, Type, cast
from typing import Any, Awaitable, Dict, Optional, Union, Type, cast

from .error import GraphQLError
from .execution import execute, ExecutionResult, ExecutionContext, Middleware
Expand All @@ -21,11 +21,11 @@ async def graphql(
source: Union[str, Source],
root_value: Any = None,
context_value: Any = None,
variable_values: Dict[str, Any] = None,
operation_name: str = None,
field_resolver: GraphQLFieldResolver = None,
type_resolver: GraphQLTypeResolver = None,
middleware: Middleware = None,
variable_values: Optional[Dict[str, Any]] = None,
operation_name: Optional[str] = None,
field_resolver: Optional[GraphQLFieldResolver] = None,
type_resolver: Optional[GraphQLTypeResolver] = None,
middleware: Optional[Middleware] = None,
execution_context_class: Type[ExecutionContext] = ExecutionContext,
) -> ExecutionResult:
"""Execute a GraphQL operation asynchronously.
Expand Down Expand Up @@ -95,11 +95,11 @@ def graphql_sync(
source: Union[str, Source],
root_value: Any = None,
context_value: Any = None,
variable_values: Dict[str, Any] = None,
operation_name: str = None,
field_resolver: GraphQLFieldResolver = None,
type_resolver: GraphQLTypeResolver = None,
middleware: Middleware = None,
variable_values: Optional[Dict[str, Any]] = None,
operation_name: Optional[str] = None,
field_resolver: Optional[GraphQLFieldResolver] = None,
type_resolver: Optional[GraphQLTypeResolver] = None,
middleware: Optional[Middleware] = None,
execution_context_class: Type[ExecutionContext] = ExecutionContext,
) -> ExecutionResult:
"""Execute a GraphQL operation synchronously.
Expand Down
4 changes: 2 additions & 2 deletions src/graphql/language/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ def __init__(
end: int,
line: int,
column: int,
prev: "Token" = None,
value: str = None,
prev: Optional["Token"] = None,
value: Optional[str] = None,
) -> None:
self.kind = kind
self.start, self.end = start, end
Expand Down
2 changes: 1 addition & 1 deletion src/graphql/language/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1025,7 +1025,7 @@ def expect_optional_keyword(self, value: str) -> bool:

return False

def unexpected(self, at_token: Token = None) -> GraphQLError:
def unexpected(self, at_token: Optional[Token] = None) -> GraphQLError:
"""Create an error when an unexpected lexed token is encountered."""
token = at_token or self._lexer.token
return GraphQLSyntaxError(
Expand Down
7 changes: 6 additions & 1 deletion src/graphql/language/source.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Optional

from .location import SourceLocation

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

def __init__(
self, body: str, name: str = None, location_offset: SourceLocation = None
self,
body: str,
name: Optional[str] = None,
location_offset: Optional[SourceLocation] = None,
) -> None:
"""Initialize source input.
Expand Down
4 changes: 2 additions & 2 deletions src/graphql/pyutils/did_you_mean.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from typing import Sequence
from typing import Optional, Sequence

__all__ = ["did_you_mean"]

MAX_LENGTH = 5


def did_you_mean(suggestions: Sequence[str], sub_message: str = None) -> str:
def did_you_mean(suggestions: Sequence[str], sub_message: Optional[str] = None) -> str:
"""Given [ A, B, C ] return ' Did you mean A, B, or C?'"""
if not suggestions:
return ""
Expand Down
4 changes: 2 additions & 2 deletions src/graphql/subscription/map_async_iterator.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from asyncio import Event, ensure_future, Future, wait
from concurrent.futures import FIRST_COMPLETED
from inspect import isasyncgen, isawaitable
from typing import AsyncIterable, Callable, Set
from typing import AsyncIterable, Callable, Optional, Set

__all__ = ["MapAsyncIterator"]

Expand All @@ -21,7 +21,7 @@ def __init__(
self,
iterable: AsyncIterable,
callback: Callable,
reject_callback: Callable = None,
reject_callback: Optional[Callable] = None,
) -> None:
self.iterator = iterable.__aiter__()
self.callback = callback
Expand Down
25 changes: 17 additions & 8 deletions src/graphql/subscription/subscribe.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
from inspect import isawaitable
from typing import Any, AsyncIterable, AsyncIterator, Awaitable, Dict, Union, cast
from typing import (
Any,
AsyncIterable,
AsyncIterator,
Awaitable,
Dict,
Optional,
Union,
cast,
)

from ..error import GraphQLError, located_error
from ..execution.execute import (
Expand All @@ -23,10 +32,10 @@ async def subscribe(
document: DocumentNode,
root_value: Any = None,
context_value: Any = None,
variable_values: Dict[str, Any] = None,
operation_name: str = None,
field_resolver: GraphQLFieldResolver = None,
subscribe_field_resolver: GraphQLFieldResolver = None,
variable_values: Optional[Dict[str, Any]] = None,
operation_name: Optional[str] = None,
field_resolver: Optional[GraphQLFieldResolver] = None,
subscribe_field_resolver: Optional[GraphQLFieldResolver] = None,
) -> Union[AsyncIterator[ExecutionResult], ExecutionResult]:
"""Create a GraphQL subscription.
Expand Down Expand Up @@ -91,9 +100,9 @@ async def create_source_event_stream(
document: DocumentNode,
root_value: Any = None,
context_value: Any = None,
variable_values: Dict[str, Any] = None,
operation_name: str = None,
field_resolver: GraphQLFieldResolver = None,
variable_values: Optional[Dict[str, Any]] = None,
operation_name: Optional[str] = None,
field_resolver: Optional[GraphQLFieldResolver] = None,
) -> Union[AsyncIterable[Any], ExecutionResult]:
"""Create source even stream
Expand Down
Loading

0 comments on commit 56cfc71

Please sign in to comment.