Skip to content

Commit efaad2e

Browse files
committed
Make all "not reachable" look and work the same
Replicates graphql/graphql-js@d55f6d3
1 parent f76de17 commit efaad2e

11 files changed

+72
-33
lines changed

graphql/execution/execute.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
OperationType,
2727
SelectionSetNode,
2828
)
29-
from .middleware import MiddlewareManager
3029
from ..pyutils import inspect, is_invalid, is_nullish, MaybeAwaitable
3130
from ..utilities import get_operation_root_type, type_from_ast
3231
from ..type import (
@@ -54,6 +53,7 @@
5453
is_non_null_type,
5554
is_object_type,
5655
)
56+
from .middleware import MiddlewareManager
5757
from .values import get_argument_values, get_directive_values, get_variable_values
5858

5959
__all__ = [
@@ -786,7 +786,8 @@ def complete_value(
786786

787787
# Not reachable. All possible output types have been considered.
788788
raise TypeError( # pragma: no cover
789-
f"Cannot complete value of unexpected type '{inspect(return_type)}'."
789+
"Cannot complete value of unexpected output type:"
790+
f" '{inspect(return_type)}'."
790791
)
791792

792793
def complete_list_value(

graphql/type/introspection.py

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020
is_scalar_type,
2121
is_union_type,
2222
)
23-
from .scalars import GraphQLBoolean, GraphQLString
2423
from ..language import DirectiveLocation, print_ast
24+
from ..pyutils import inspect
25+
from .scalars import GraphQLBoolean, GraphQLString
2526

2627
__all__ = [
2728
"SchemaMetaFieldDef",
@@ -258,7 +259,9 @@ def kind(type_, _info):
258259
return TypeKind.LIST
259260
if is_non_null_type(type_):
260261
return TypeKind.NON_NULL
261-
raise TypeError(f"Unknown kind of type: {type_}")
262+
263+
# Not reachable. All possible types have been considered.
264+
raise TypeError(f"Unexpected type: '{inspect(type_)}'.") # pragma: no cover
262265

263266
@staticmethod
264267
def name(type_, _info):
@@ -336,39 +339,53 @@ def of_type(type_, _info):
336339
)
337340

338341

339-
def _resolve_input_value_default_value(item, _info):
340-
# Since ast_from_value needs graphql.type, it can only be imported later
341-
from ..utilities import ast_from_value
342-
343-
value_ast = ast_from_value(item[1].default_value, item[1].type)
344-
return print_ast(value_ast) if value_ast else None
345-
346-
347342
__InputValue: GraphQLObjectType = GraphQLObjectType(
348343
name="__InputValue",
349344
description="Arguments provided to Fields or Directives and the input"
350345
" fields of an InputObject are represented as Input Values"
351346
" which describe their type and optionally a default value.",
352347
fields=lambda: {
353348
"name": GraphQLField(
354-
GraphQLNonNull(GraphQLString), resolve=lambda item, _info: item[0]
349+
GraphQLNonNull(GraphQLString), resolve=InputValueFieldResolvers.name
355350
),
356351
"description": GraphQLField(
357-
GraphQLString, resolve=lambda item, _info: item[1].description
352+
GraphQLString, resolve=InputValueFieldResolvers.description
358353
),
359354
"type": GraphQLField(
360-
GraphQLNonNull(__Type), resolve=lambda item, _info: item[1].type
355+
GraphQLNonNull(__Type), resolve=InputValueFieldResolvers.type
361356
),
362357
"defaultValue": GraphQLField(
363358
GraphQLString,
364359
description="A GraphQL-formatted string representing"
365360
" the default value for this input value.",
366-
resolve=_resolve_input_value_default_value,
361+
resolve=InputValueFieldResolvers.default_value,
367362
),
368363
},
369364
)
370365

371366

367+
class InputValueFieldResolvers:
368+
@staticmethod
369+
def name(item, _info):
370+
return item[0]
371+
372+
@staticmethod
373+
def description(item, _info):
374+
return item[1].description
375+
376+
@staticmethod
377+
def type(item, _info):
378+
return item[1].type
379+
380+
@staticmethod
381+
def default_value(item, _info):
382+
# Since ast_from_value needs graphql.type, it can only be imported later
383+
from ..utilities import ast_from_value
384+
385+
value_ast = ast_from_value(item[1].default_value, item[1].type)
386+
return print_ast(value_ast) if value_ast else None
387+
388+
372389
__EnumValue: GraphQLObjectType = GraphQLObjectType(
373390
name="__EnumValue",
374391
description="One possible value for a given Enum. Enum values are unique"

graphql/utilities/ast_from_value.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,5 @@ def ast_from_value(value: Any, type_: GraphQLInputType) -> Optional[ValueNode]:
126126

127127
raise TypeError(f"Cannot convert value to AST: {inspect(serialized)}")
128128

129-
raise TypeError(f"Unknown type: {inspect(type_)}.")
129+
# Not reachable. All possible input types have been considered.
130+
raise TypeError(f"Unexpected input type: '{inspect(type_)}'.") # pragma: no cover

graphql/utilities/build_ast_schema.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
parse,
2525
Node,
2626
)
27+
from ..pyutils import inspect
2728
from ..type import (
2829
GraphQLArgument,
2930
GraphQLDeprecatedDirective,
@@ -277,12 +278,13 @@ def build_type(self, ast_node: TypeDefinitionNode) -> GraphQLNamedType:
277278
"scalar_type_definition": self._make_scalar_def,
278279
"input_object_type_definition": self._make_input_object_def,
279280
}.get(ast_node.kind)
280-
if not method:
281-
# Not reachable. All possible type definition nodes have been considered.
282-
raise TypeError( # pragma: no cover
283-
f"Type kind '{ast_node.kind}' not supported."
284-
)
285-
return method(ast_node) # type: ignore
281+
if method:
282+
return method(ast_node) # type: ignore
283+
284+
# Not reachable. All possible type definition nodes have been considered.
285+
raise TypeError( # pragma: no cover
286+
f"Unexpected type definition node: '{inspect(ast_node)}'."
287+
)
286288

287289
def _make_type_def(self, ast_node: ObjectTypeDefinitionNode) -> GraphQLObjectType:
288290
interface_nodes = ast_node.interfaces

graphql/utilities/coerce_value.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from ..error import GraphQLError, INVALID
44
from ..language import Node
5-
from ..pyutils import is_invalid, or_list, suggestion_list
5+
from ..pyutils import inspect, is_invalid, or_list, suggestion_list
66
from ..type import (
77
GraphQLEnumType,
88
GraphQLInputObjectType,
@@ -175,7 +175,8 @@ def coerce_value(
175175

176176
return of_errors(errors) if errors else of_value(coerced_value_dict)
177177

178-
raise TypeError("Unexpected type: {type_}.")
178+
# Not reachable. All possible input types have been considered.
179+
raise TypeError(f"Unexpected input type: '{inspect(type_)}'.") # pragma: no cover
179180

180181

181182
def of_value(value: Any) -> CoercedValue:

graphql/utilities/extend_schema.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
TypeDefinitionNode,
1212
TypeExtensionNode,
1313
)
14+
from ..pyutils import inspect
1415
from ..type import (
1516
GraphQLArgument,
1617
GraphQLDirective,
@@ -162,8 +163,9 @@ def extend_named_type(type_: GraphQLNamedType) -> GraphQLNamedType:
162163
if is_input_object_type(type_):
163164
type_ = cast(GraphQLInputObjectType, type_)
164165
return extend_input_object_type(type_)
166+
165167
# Not reachable. All possible types have been considered.
166-
raise TypeError(f"Type {type_} not supported.") # pragma: no cover
168+
raise TypeError(f"Unexpected type: '{inspect(type_)}'.") # pragma: no cover
167169

168170
def extend_directive(directive: GraphQLDirective) -> GraphQLDirective:
169171
kwargs = directive.to_kwargs()

graphql/utilities/lexicographic_sort_schema.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from typing import Dict, List, Tuple, cast
22

3+
from ..pyutils import inspect
34
from ..type import (
45
GraphQLArgument,
56
GraphQLDirective,
@@ -129,8 +130,9 @@ def sort_named_type(type_: GraphQLNamedType) -> GraphQLNamedType:
129130
input_object_type = cast(GraphQLInputObjectType, type_)
130131
kwargs.update(fields=lambda: sort_input_fields(input_object_type.fields))
131132
return GraphQLInputObjectType(**kwargs)
132-
# Not reachable. All possible type definition nodes have been considered.
133-
raise TypeError(f"Unknown type: '{type_}'") # pragma: no cover
133+
134+
# Not reachable. All possible types have been considered.
135+
raise TypeError(f"Unexpected type: '{inspect(type_)}'.") # pragma: no cover
134136

135137
type_map: Dict[str, GraphQLNamedType] = {
136138
type_.name: sort_named_type(type_)

graphql/utilities/schema_printer.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@ def print_type(type_: GraphQLNamedType) -> str:
139139
if is_input_object_type(type_):
140140
type_ = cast(GraphQLInputObjectType, type_)
141141
return print_input_object(type_)
142-
raise TypeError(f"Unknown type: {inspect(type_)}")
142+
# Not reachable. All possible types have been considered.
143+
raise TypeError(f"Unexpected type: '{inspect(type_)}'.") # pragma: no cover
143144

144145

145146
def print_scalar(type_: GraphQLScalarType) -> str:

graphql/utilities/type_from_ast.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from typing import Optional, overload
22

33
from ..language import TypeNode, NamedTypeNode, ListTypeNode, NonNullTypeNode
4+
from ..pyutils import inspect
45
from ..type import (
56
GraphQLType,
67
GraphQLSchema,
@@ -55,4 +56,8 @@ def type_from_ast(schema, type_node): # noqa: F811
5556
return GraphQLNonNull(inner_type) if inner_type else None
5657
if isinstance(type_node, NamedTypeNode):
5758
return schema.get_type(type_node.name.value)
58-
raise TypeError(f"Unexpected type kind: {type_node.kind}")
59+
60+
# Not reachable. All possible type nodes have been considered.
61+
raise TypeError( # pragma: no cover
62+
f"Unexpected type node: '{inspect(type_node)}'."
63+
)

graphql/utilities/value_from_ast.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
ValueNode,
1010
VariableNode,
1111
)
12-
from ..pyutils import is_invalid
12+
from ..pyutils import inspect, is_invalid
1313
from ..type import (
1414
GraphQLEnumType,
1515
GraphQLInputObjectType,
@@ -148,6 +148,9 @@ def value_from_ast(
148148
return INVALID
149149
return result
150150

151+
# Not reachable. All possible input types have been considered.
152+
raise TypeError(f"Unexpected input type: '{inspect(type_)}'.") # pragma: no cover
153+
151154

152155
def is_missing_variable(
153156
value_node: ValueNode, variables: Dict[str, Any] = None

0 commit comments

Comments
 (0)