Skip to content

Commit

Permalink
Update mypy, use new semantic analyzer (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
Cito committed Nov 30, 2019
1 parent 07a4cfa commit 8158da5
Show file tree
Hide file tree
Showing 28 changed files with 194 additions and 138 deletions.
2 changes: 0 additions & 2 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,3 @@ python_version = 3.8
check_untyped_defs = True
warn_redundant_casts = True
warn_unused_ignores = True
# https://github.com/python/mypy/issues/7203
new_semantic_analyzer = False
57 changes: 30 additions & 27 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pytest-describe = ">=0.12"
pyyaml = "^5.1"
black = ">=19.10b0"
flake8 = "^3.7"
mypy = ">=0.720,<0.730"
mypy = ">=0.750,<0.760"
codecov = "^2"
sphinx = "^2.2"
sphinx_rtd_theme = ">=0.4"
Expand Down
3 changes: 2 additions & 1 deletion src/graphql/execution/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
FrozenList,
Path,
)
from ..utilities import get_operation_root_type, type_from_ast
from ..utilities.get_operation_root_type import get_operation_root_type
from ..utilities.type_from_ast import type_from_ast
from ..type import (
GraphQLAbstractType,
GraphQLField,
Expand Down
4 changes: 3 additions & 1 deletion src/graphql/execution/values.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
is_input_type,
is_non_null_type,
)
from ..utilities import coerce_input_value, type_from_ast, value_from_ast
from ..utilities.coerce_input_value import coerce_input_value
from ..utilities.type_from_ast import type_from_ast
from ..utilities.value_from_ast import value_from_ast

__all__ = ["get_variable_values", "get_argument_values", "get_directive_values"]

Expand Down
2 changes: 1 addition & 1 deletion src/graphql/language/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ def __init_subclass__(cls):
keys: List[str] = []
for base in cls.__bases__:
# noinspection PyUnresolvedReferences
keys.extend(base.keys)
keys.extend(base.keys) # type: ignore
keys.extend(cls.__slots__)
cls.keys = keys

Expand Down
8 changes: 6 additions & 2 deletions src/graphql/language/visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Callable,
List,
NamedTuple,
Optional,
Sequence,
Tuple,
Union,
Expand Down Expand Up @@ -157,8 +158,11 @@ def __init_subclass__(cls):
for attr, val in cls.__dict__.items():
if attr.startswith("_"):
continue
attr = attr.split("_", 1)
attr, kind = attr if len(attr) > 1 else (attr[0], None)
attr_kind = attr.split("_", 1)
if len(attr_kind) < 2:
kind: Optional[str] = None
else:
attr, kind = attr_kind
if attr in ("enter", "leave"):
if kind:
name = snake_to_camel(kind) + "Node"
Expand Down
8 changes: 5 additions & 3 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, wait
from asyncio import Event, ensure_future, Future, wait
from concurrent.futures import FIRST_COMPLETED
from inspect import isasyncgen, isawaitable
from typing import AsyncIterable, Callable
from typing import AsyncIterable, Callable, Set

__all__ = ["MapAsyncIterator"]

Expand Down Expand Up @@ -42,7 +42,9 @@ async def __anext__(self):
aclose = ensure_future(self._close_event.wait())
anext = ensure_future(self.iterator.__anext__())

done, pending = await wait([aclose, anext], return_when=FIRST_COMPLETED)
pending: Set[Future] = (
await wait([aclose, anext], return_when=FIRST_COMPLETED)
)[1]
for task in pending:
task.cancel()

Expand Down
4 changes: 2 additions & 2 deletions src/graphql/subscription/subscribe.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ async def subscribe(
if isinstance(result_or_stream, ExecutionResult):
return result_or_stream

async def map_source_to_response(payload):
async def map_source_to_response(payload) -> ExecutionResult:
"""Map source to response.
For each payload yielded from a subscription, map it over the normal GraphQL
Expand All @@ -81,7 +81,7 @@ async def map_source_to_response(payload):
operation_name,
field_resolver,
)
return await result if isawaitable(result) else result
return await result if isawaitable(result) else result # type: ignore

return MapAsyncIterator(result_or_stream, map_source_to_response)

Expand Down
12 changes: 5 additions & 7 deletions src/graphql/type/definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,11 +361,11 @@ def __init__(
" as a sequence of ScalarTypeExtensionNode instances."
)
if serialize is not None:
self.serialize = serialize
self.serialize = serialize # type: ignore
if parse_value is not None:
self.parse_value = parse_value
self.parse_value = parse_value # type: ignore
if parse_literal is not None:
self.parse_literal = parse_literal
self.parse_literal = parse_literal # type: ignore

def __repr__(self):
return f"<{self.__class__.__name__} {self.name!r}>"
Expand All @@ -391,9 +391,7 @@ def parse_value(value: Any) -> Any:
"""
return value

def parse_literal( # type: ignore
self, node: ValueNode, _variables: Dict[str, Any] = None
) -> Any:
def parse_literal(self, node: ValueNode, _variables: Dict[str, Any] = None) -> Any:
"""Parses an externally provided literal value to use as an input.
This default method uses the parse_value method and should be replaced
Expand Down Expand Up @@ -1247,7 +1245,7 @@ def __init__(
)
self._fields = fields
if out_type is not None:
self.out_type = out_type
self.out_type = out_type # type: ignore

@staticmethod
def out_type(value: Dict[str, Any]) -> Any:
Expand Down
2 changes: 1 addition & 1 deletion src/graphql/type/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def validate_directives(self):
arg_names: Set[str] = set()
for arg_name, arg in directive.args.items():
# Ensure they are named correctly.
self.validate_name(arg_name, arg)
self.validate_name(arg, arg_name)

# Ensure they are unique per directive.
if arg_name in arg_names:
Expand Down
2 changes: 1 addition & 1 deletion src/graphql/utilities/coerce_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from ..language import Node
from ..pyutils import Path, inspect, print_path_list
from ..type import GraphQLInputType
from . import coerce_input_value
from .coerce_input_value import coerce_input_value

__all__ = ["coerce_value", "CoercedValue"]

Expand Down
34 changes: 22 additions & 12 deletions src/graphql/utilities/extend_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ def extend_named_type(type_: GraphQLNamedType) -> GraphQLNamedType:

def extend_directive(directive: GraphQLDirective) -> GraphQLDirective:
kwargs = directive.to_kwargs()
return GraphQLDirective( # type: ignore
**{
return GraphQLDirective(
**{ # type: ignore
**kwargs,
"args": {name: extend_arg(arg) for name, arg in kwargs["args"].items()},
}
Expand All @@ -188,8 +188,11 @@ def extend_input_object_type(
**kwargs,
"fields": lambda: {
**{
name: GraphQLInputField( # type: ignore
**{**field.to_kwargs(), "type_": replace_type(field.type)}
name: GraphQLInputField(
**{ # type: ignore
**field.to_kwargs(),
"type_": replace_type(field.type),
}
)
for name, field in kwargs["fields"].items()
},
Expand Down Expand Up @@ -306,17 +309,20 @@ def extend_union_type(type_: GraphQLUnionType) -> GraphQLUnionType:
)

def extend_field(field: GraphQLField) -> GraphQLField:
return GraphQLField( # type: ignore
**{
return GraphQLField(
**{ # type: ignore
**field.to_kwargs(),
"type_": replace_type(field.type),
"args": {name: extend_arg(arg) for name, arg in field.args.items()},
}
)

def extend_arg(arg: GraphQLArgument) -> GraphQLArgument:
return GraphQLArgument( # type: ignore
**{**arg.to_kwargs(), "type_": replace_type(arg.type)}
return GraphQLArgument(
**{ # type: ignore
**arg.to_kwargs(),
"type_": replace_type(arg.type),
}
)

# noinspection PyShadowingNames
Expand Down Expand Up @@ -358,13 +364,17 @@ def resolve_type(type_name: str) -> GraphQLNamedType:
operation_types[operation] = operation_type.type.name.value

# Then produce and return a Schema with these types.
return GraphQLSchema( # type: ignore
return GraphQLSchema(
# Note: While this could make early assertions to get the correctly
# typed values, that would throw immediately while type system
# validation with validateSchema() will produce more actionable results.
query=get_maybe_type_by_name(operation_types[OperationType.QUERY]),
mutation=get_maybe_type_by_name(operation_types[OperationType.MUTATION]),
subscription=get_maybe_type_by_name(
query=get_maybe_type_by_name( # type: ignore
operation_types[OperationType.QUERY]
),
mutation=get_maybe_type_by_name( # type: ignore
operation_types[OperationType.MUTATION]
),
subscription=get_maybe_type_by_name( # type: ignore
operation_types[OperationType.SUBSCRIPTION]
),
types=list(type_map.values()),
Expand Down
6 changes: 2 additions & 4 deletions src/graphql/utilities/find_breaking_changes.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,11 @@ def find_breaking_changes(
Given two schemas, returns a list containing descriptions of all the types of
breaking changes covered by the other functions down below.
"""
breaking_changes = [
return [
change
for change in find_schema_changes(old_schema, new_schema)
if isinstance(change.type, BreakingChangeType)
]
return cast(List[BreakingChange], breaking_changes)


def find_dangerous_changes(
Expand All @@ -105,12 +104,11 @@ def find_dangerous_changes(
Given two schemas, returns a list containing descriptions of all the types of
potentially dangerous changes covered by the other functions down below.
"""
dangerous_changes = [
return [
change
for change in find_schema_changes(old_schema, new_schema)
if isinstance(change.type, DangerousChangeType)
]
return cast(List[DangerousChange], dangerous_changes)


def find_schema_changes(
Expand Down
Loading

0 comments on commit 8158da5

Please sign in to comment.