Skip to content

Commit 2c8382e

Browse files
committed
coverage: Clearly mark not reachable places in the code
Replicates graphql/graphql-js@635a13c
1 parent 6aed87d commit 2c8382e

9 files changed

+23
-26
lines changed

src/graphql/utilities/lexicographic_sort_schema.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -93,28 +93,28 @@ def sort_types(arr: FrozenList[GraphQLNamedType]) -> List[GraphQLNamedType]:
9393
def sort_named_type(type_: GraphQLNamedType) -> GraphQLNamedType:
9494
if is_scalar_type(type_) or is_introspection_type(type_):
9595
return type_
96-
elif is_object_type(type_):
96+
if is_object_type(type_):
9797
kwargs = type_.to_kwargs()
9898
type_ = cast(GraphQLObjectType, type_)
9999
kwargs.update(
100100
interfaces=lambda: sort_types(type_.interfaces),
101101
fields=lambda: sort_fields(type_.fields),
102102
)
103103
return GraphQLObjectType(**kwargs)
104-
elif is_interface_type(type_):
104+
if is_interface_type(type_):
105105
kwargs = type_.to_kwargs()
106106
type_ = cast(GraphQLInterfaceType, type_)
107107
kwargs.update(
108108
interfaces=lambda: sort_types(type_.interfaces),
109109
fields=lambda: sort_fields(type_.fields),
110110
)
111111
return GraphQLInterfaceType(**kwargs)
112-
elif is_union_type(type_):
112+
if is_union_type(type_):
113113
kwargs = type_.to_kwargs()
114114
type_ = cast(GraphQLUnionType, type_)
115115
kwargs.update(types=lambda: sort_types(type_.types))
116116
return GraphQLUnionType(**kwargs)
117-
elif is_enum_type(type_):
117+
if is_enum_type(type_):
118118
kwargs = type_.to_kwargs()
119119
type_ = cast(GraphQLEnumType, type_)
120120
kwargs.update(
@@ -129,7 +129,7 @@ def sort_named_type(type_: GraphQLNamedType) -> GraphQLNamedType:
129129
}
130130
)
131131
return GraphQLEnumType(**kwargs)
132-
elif is_input_object_type(type_):
132+
if is_input_object_type(type_):
133133
kwargs = type_.to_kwargs()
134134
type_ = cast(GraphQLInputObjectType, type_)
135135
kwargs.update(fields=lambda: sort_input_fields(type_.fields))

src/graphql/utilities/schema_printer.py

+1
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ def print_type(type_: GraphQLNamedType) -> str:
140140
if is_input_object_type(type_):
141141
type_ = cast(GraphQLInputObjectType, type_)
142142
return print_input_object(type_)
143+
143144
# Not reachable. All possible types have been considered.
144145
raise TypeError(f"Unexpected type: {inspect(type_)}.") # pragma: no cover
145146

src/graphql/utilities/type_info.py

+1-9
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
Node,
1111
ObjectFieldNode,
1212
OperationDefinitionNode,
13-
OperationType,
1413
SelectionSetNode,
1514
VariableDefinitionNode,
1615
Visitor,
@@ -157,14 +156,7 @@ def enter_directive(self, node: DirectiveNode):
157156
self._directive = self._schema.get_directive(node.name.value)
158157

159158
def enter_operation_definition(self, node: OperationDefinitionNode):
160-
if node.operation == OperationType.QUERY:
161-
type_ = self._schema.query_type
162-
elif node.operation == OperationType.MUTATION:
163-
type_ = self._schema.mutation_type
164-
elif node.operation == OperationType.SUBSCRIPTION:
165-
type_ = self._schema.subscription_type
166-
else:
167-
type_ = None
159+
type_ = getattr(self._schema, f"{node.operation.value}_type")
168160
self._type_stack.append(type_ if is_object_type(type_) else None)
169161

170162
def enter_inline_fragment(self, node: InlineFragmentNode):

src/graphql/validation/rules/known_directives.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def get_directive_location_for_ast_path(ancestors):
9595
kind = applied_to.kind
9696
if kind == "operation_definition":
9797
applied_to = cast(OperationDefinitionNode, applied_to)
98-
return _operation_location.get(applied_to.operation.value)
98+
return _operation_location[applied_to.operation.value]
9999
elif kind == "input_value_definition":
100100
parent_node = ancestors[-3]
101101
return (

src/graphql/validation/rules/possible_type_extensions.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from ...error import GraphQLError
66
from ...language import TypeDefinitionNode, TypeExtensionNode
7-
from ...pyutils import did_you_mean, suggestion_list
7+
from ...pyutils import did_you_mean, inspect, suggestion_list
88
from ...type import (
99
is_enum_type,
1010
is_input_object_type,
@@ -80,18 +80,19 @@ def check_extension(self, node: TypeExtensionNode, *_args):
8080
def type_to_ext_kind(type_: Any) -> str:
8181
if is_scalar_type(type_):
8282
return "scalar_type_extension"
83-
elif is_object_type(type_):
83+
if is_object_type(type_):
8484
return "object_type_extension"
85-
elif is_interface_type(type_):
85+
if is_interface_type(type_):
8686
return "interface_type_extension"
87-
elif is_union_type(type_):
87+
if is_union_type(type_):
8888
return "union_type_extension"
89-
elif is_enum_type(type_):
89+
if is_enum_type(type_):
9090
return "enum_type_extension"
91-
elif is_input_object_type(type_):
91+
if is_input_object_type(type_):
9292
return "input_object_type_extension"
93-
else:
94-
return "unknown_type_extension"
93+
94+
# Not reachable. All possible types have been considered.
95+
raise TypeError(f"Unexpected type: {inspect(type_)}.") # pragma: no cover
9596

9697

9798
_type_names_for_extension_kinds = {

tests/execution/test_abstract.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def is_type_of(obj, _info):
4040

4141
def get_type_resolver(types):
4242
def resolve(obj, _info, _type):
43-
return resolve_thunk(types).get(obj.__class__)
43+
return resolve_thunk(types)[obj.__class__]
4444

4545
return resolve
4646

tests/execution/test_abstract_async.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ async def is_type_of(obj, _info):
4646

4747
def get_type_resolver(types):
4848
async def resolve(obj, _info, _type):
49-
return resolve_thunk(types).get(obj.__class__)
49+
return resolve_thunk(types)[obj.__class__]
5050

5151
return resolve
5252

tests/execution/test_union_interface.py

+3
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ def resolve_pet_type(value, _info, _type):
112112
if isinstance(value, Cat):
113113
return CatType
114114

115+
# Not reachable. All possible types have been considered.
116+
raise TypeError # pragma: no cover
117+
115118

116119
PetType = GraphQLUnionType("Pet", [DogType, CatType], resolve_type=resolve_pet_type)
117120

tests/star_wars_schema.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@
116116
resolve_type=lambda character, _info, _type: {
117117
"Human": human_type,
118118
"Droid": droid_type,
119-
}.get(character.type),
119+
}[character.type],
120120
description="A character in the Star Wars Trilogy",
121121
)
122122

0 commit comments

Comments
 (0)