Skip to content

Commit b2706dd

Browse files
committed
Make use of structural pattern matching
1 parent 6201d0b commit b2706dd

File tree

4 files changed

+77
-85
lines changed

4 files changed

+77
-85
lines changed

src/graphql/type/introspection.py

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@
1313
GraphQLEnumValue,
1414
GraphQLField,
1515
GraphQLFieldMap,
16+
GraphQLInputObjectType,
17+
GraphQLInterfaceType,
1618
GraphQLList,
1719
GraphQLNamedType,
1820
GraphQLNonNull,
1921
GraphQLObjectType,
22+
GraphQLScalarType,
23+
GraphQLUnionType,
2024
is_abstract_type,
2125
is_enum_type,
2226
is_input_object_type,
2327
is_interface_type,
24-
is_list_type,
25-
is_non_null_type,
2628
is_object_type,
27-
is_scalar_type,
28-
is_union_type,
2929
)
3030
from .scalars import GraphQLBoolean, GraphQLString
3131

@@ -312,26 +312,27 @@ def __new__(cls):
312312

313313
@staticmethod
314314
def kind(type_, _info):
315-
if is_scalar_type(type_):
316-
return TypeKind.SCALAR
317-
if is_object_type(type_):
318-
return TypeKind.OBJECT
319-
if is_interface_type(type_):
320-
return TypeKind.INTERFACE
321-
if is_union_type(type_):
322-
return TypeKind.UNION
323-
if is_enum_type(type_):
324-
return TypeKind.ENUM
325-
if is_input_object_type(type_):
326-
return TypeKind.INPUT_OBJECT
327-
if is_list_type(type_):
328-
return TypeKind.LIST
329-
if is_non_null_type(type_):
330-
return TypeKind.NON_NULL
331-
332-
# Not reachable. All possible types have been considered.
333-
msg = f"Unexpected type: {inspect(type_)}." # pragma: no cover
334-
raise TypeError(msg) # pragma: no cover
315+
match type_:
316+
case GraphQLScalarType():
317+
return TypeKind.SCALAR
318+
case GraphQLObjectType():
319+
return TypeKind.OBJECT
320+
case GraphQLInterfaceType():
321+
return TypeKind.INTERFACE
322+
case GraphQLUnionType():
323+
return TypeKind.UNION
324+
case GraphQLEnumType():
325+
return TypeKind.ENUM
326+
case GraphQLInputObjectType():
327+
return TypeKind.INPUT_OBJECT
328+
case GraphQLList():
329+
return TypeKind.LIST
330+
case GraphQLNonNull():
331+
return TypeKind.NON_NULL
332+
case _: # pragma: no cover
333+
# Not reachable. All possible types have been considered.
334+
msg = f"Unexpected type: {inspect(type_)}."
335+
raise TypeError(msg)
335336

336337
@staticmethod
337338
def name(type_, _info):

src/graphql/utilities/extend_schema.py

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,11 @@
7575
GraphQLUnionTypeKwargs,
7676
assert_schema,
7777
introspection_types,
78-
is_enum_type,
79-
is_input_object_type,
80-
is_interface_type,
8178
is_introspection_type,
8279
is_list_type,
8380
is_non_null_type,
84-
is_object_type,
85-
is_scalar_type,
8681
is_specified_directive,
8782
is_specified_scalar_type,
88-
is_union_type,
8983
specified_scalar_types,
9084
)
9185
from .value_from_ast import value_from_ast
@@ -312,22 +306,23 @@ def extend_named_type(self, type_: GraphQLNamedType) -> GraphQLNamedType:
312306
if is_introspection_type(type_) or is_specified_scalar_type(type_):
313307
# Builtin types are not extended.
314308
return type_
315-
if is_scalar_type(type_):
316-
return self.extend_scalar_type(type_)
317-
if is_object_type(type_):
318-
return self.extend_object_type(type_)
319-
if is_interface_type(type_):
320-
return self.extend_interface_type(type_)
321-
if is_union_type(type_):
322-
return self.extend_union_type(type_)
323-
if is_enum_type(type_):
324-
return self.extend_enum_type(type_)
325-
if is_input_object_type(type_):
326-
return self.extend_input_object_type(type_)
327-
328-
# Not reachable. All possible types have been considered.
329-
msg = f"Unexpected type: {inspect(type_)}." # pragma: no cover
330-
raise TypeError(msg) # pragma: no cover
309+
match type_:
310+
case GraphQLScalarType():
311+
return self.extend_scalar_type(type_)
312+
case GraphQLObjectType():
313+
return self.extend_object_type(type_)
314+
case GraphQLInterfaceType():
315+
return self.extend_interface_type(type_)
316+
case GraphQLUnionType():
317+
return self.extend_union_type(type_)
318+
case GraphQLEnumType():
319+
return self.extend_enum_type(type_)
320+
case GraphQLInputObjectType():
321+
return self.extend_input_object_type(type_)
322+
case _: # pragma: no cover
323+
# Not reachable. All possible types have been considered.
324+
msg = f"Unexpected type: {inspect(type_)}."
325+
raise TypeError(msg)
331326

332327
def extend_input_object_type_fields(
333328
self, kwargs: GraphQLInputObjectTypeKwargs, extensions: tuple[Any, ...]

src/graphql/utilities/find_breaking_changes.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
GraphQLInterfaceType,
1616
GraphQLNamedType,
1717
GraphQLObjectType,
18+
GraphQLScalarType,
1819
GraphQLSchema,
1920
GraphQLType,
2021
GraphQLUnionType,
@@ -27,7 +28,6 @@
2728
is_object_type,
2829
is_required_argument,
2930
is_required_input_field,
30-
is_scalar_type,
3131
is_specified_scalar_type,
3232
is_union_type,
3333
)
@@ -543,22 +543,23 @@ def is_change_safe_for_input_object_field_or_field_arg(
543543

544544

545545
def type_kind_name(type_: GraphQLNamedType) -> str:
546-
if is_scalar_type(type_):
547-
return "a Scalar type"
548-
if is_object_type(type_):
549-
return "an Object type"
550-
if is_interface_type(type_):
551-
return "an Interface type"
552-
if is_union_type(type_):
553-
return "a Union type"
554-
if is_enum_type(type_):
555-
return "an Enum type"
556-
if is_input_object_type(type_):
557-
return "an Input type"
558-
559-
# Not reachable. All possible output types have been considered.
560-
msg = f"Unexpected type {inspect(type_)}" # pragma: no cover
561-
raise TypeError(msg) # pragma: no cover
546+
match type_:
547+
case GraphQLScalarType():
548+
return "a Scalar type"
549+
case GraphQLObjectType():
550+
return "an Object type"
551+
case GraphQLInterfaceType():
552+
return "an Interface type"
553+
case GraphQLUnionType():
554+
return "a Union type"
555+
case GraphQLEnumType():
556+
return "an Enum type"
557+
case GraphQLInputObjectType():
558+
return "an Input type"
559+
case _: # pragma: no cover
560+
# Not reachable. All possible named types have been considered.
561+
msg = f"Unexpected type {inspect(type_)}"
562+
raise TypeError(msg)
562563

563564

564565
def stringify_value(value: Any, type_: GraphQLInputType) -> str:

src/graphql/utilities/print_schema.py

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,8 @@
2121
GraphQLScalarType,
2222
GraphQLSchema,
2323
GraphQLUnionType,
24-
is_enum_type,
25-
is_input_object_type,
26-
is_interface_type,
2724
is_introspection_type,
28-
is_object_type,
29-
is_scalar_type,
3025
is_specified_directive,
31-
is_union_type,
3226
)
3327
from .ast_from_value import ast_from_value
3428

@@ -138,22 +132,23 @@ def has_default_root_operation_types(schema: GraphQLSchema) -> bool:
138132

139133
def print_type(type_: GraphQLNamedType) -> str:
140134
"""Print a named GraphQL type."""
141-
if is_scalar_type(type_):
142-
return print_scalar(type_)
143-
if is_object_type(type_):
144-
return print_object(type_)
145-
if is_interface_type(type_):
146-
return print_interface(type_)
147-
if is_union_type(type_):
148-
return print_union(type_)
149-
if is_enum_type(type_):
150-
return print_enum(type_)
151-
if is_input_object_type(type_):
152-
return print_input_object(type_)
153-
154-
# Not reachable. All possible types have been considered.
155-
msg = f"Unexpected type: {inspect(type_)}." # pragma: no cover
156-
raise TypeError(msg) # pragma: no cover
135+
match type_:
136+
case GraphQLScalarType():
137+
return print_scalar(type_)
138+
case GraphQLObjectType():
139+
return print_object(type_)
140+
case GraphQLInterfaceType():
141+
return print_interface(type_)
142+
case GraphQLUnionType():
143+
return print_union(type_)
144+
case GraphQLEnumType():
145+
return print_enum(type_)
146+
case GraphQLInputObjectType():
147+
return print_input_object(type_)
148+
case _: # pragma: no cover
149+
# Not reachable. All possible types have been considered.
150+
msg = f"Unexpected type: {inspect(type_)}."
151+
raise TypeError(msg)
157152

158153

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

0 commit comments

Comments
 (0)