Skip to content

Commit a642c59

Browse files
committed
Some simplifications with walrus operator
1 parent d09ec6a commit a642c59

File tree

4 files changed

+20
-22
lines changed

4 files changed

+20
-22
lines changed

src/graphql/execution/middleware.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ def get_middleware_resolvers(middlewares: tuple[Any, ...]) -> Iterator[Callable]
6161
for middleware in middlewares:
6262
if isfunction(middleware):
6363
yield middleware
64-
else: # middleware provided as object with 'resolve' method
65-
resolver_func = getattr(middleware, "resolve", None)
66-
if resolver_func is not None:
67-
yield resolver_func
64+
elif resolver_func := getattr(middleware, "resolve", None):
65+
# middleware provided as object with 'resolve' method
66+
yield resolver_func

src/graphql/graphql.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,7 @@ def graphql_impl(
176176
) -> AwaitableOrValue[ExecutionResult]:
177177
"""Execute a query, return asynchronously only if necessary."""
178178
# Validate Schema
179-
schema_validation_errors = validate_schema(schema)
180-
if schema_validation_errors:
179+
if schema_validation_errors := validate_schema(schema):
181180
return ExecutionResult(data=None, errors=schema_validation_errors)
182181

183182
# Parse
@@ -189,8 +188,7 @@ def graphql_impl(
189188
# Validate
190189
from .validation import validate
191190

192-
validation_errors = validate(schema, document)
193-
if validation_errors:
191+
if validation_errors := validate(schema, document):
194192
return ExecutionResult(data=None, errors=validation_errors)
195193

196194
# Execute

src/graphql/validation/rules/fragments_on_composite_types.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,26 @@ class FragmentsOnCompositeTypesRule(ValidationRule):
2525

2626
def enter_inline_fragment(self, node: InlineFragmentNode, *_args: Any) -> None:
2727
type_condition = node.type_condition
28-
if type_condition:
29-
type_ = type_from_ast(self.context.schema, type_condition)
30-
if type_ and not is_composite_type(type_):
31-
type_str = print_ast(type_condition)
32-
self.report_error(
33-
GraphQLError(
34-
"Fragment cannot condition"
35-
f" on non composite type '{type_str}'.",
36-
type_condition,
37-
)
28+
if (
29+
type_condition
30+
and (type_ := type_from_ast(self.context.schema, type_condition))
31+
and not is_composite_type(type_)
32+
):
33+
type_str = print_ast(type_condition)
34+
self.report_error(
35+
GraphQLError(
36+
f"Fragment cannot condition on non composite type '{type_str}'.",
37+
type_condition,
3838
)
39+
)
3940

4041
def enter_fragment_definition(
4142
self, node: FragmentDefinitionNode, *_args: Any
4243
) -> None:
4344
type_condition = node.type_condition
44-
type_ = type_from_ast(self.context.schema, type_condition)
45-
if type_ and not is_composite_type(type_):
45+
if (
46+
type_ := type_from_ast(self.context.schema, type_condition)
47+
) and not is_composite_type(type_):
4648
type_str = print_ast(type_condition)
4749
self.report_error(
4850
GraphQLError(

src/graphql/validation/validation_context.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,7 @@ def get_recursively_referenced_fragments(
166166
frag_name = spread.name.value
167167
if frag_name not in collected_names:
168168
add_name(frag_name)
169-
fragment = get_fragment(frag_name)
170-
if fragment:
169+
if fragment := get_fragment(frag_name):
171170
append_fragment(fragment)
172171
append_node(fragment.selection_set)
173172
self._recursively_referenced_fragments[operation] = fragments

0 commit comments

Comments
 (0)