Skip to content

Commit 54db657

Browse files
committed
Merge pull request #16 from faassen/master
Instead of using is_nullish, use a more Pythonic "is None" check.
2 parents f6791b0 + ff3151e commit 54db657

File tree

9 files changed

+18
-37
lines changed

9 files changed

+18
-37
lines changed

graphql/core/execution/executor.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from ..pyutils.default_ordered_dict import DefaultOrderedDict
1010
from ..type import GraphQLEnumType, GraphQLInterfaceType, GraphQLList, GraphQLNonNull, GraphQLObjectType, \
1111
GraphQLScalarType, GraphQLUnionType
12-
from ..utils.is_nullish import is_nullish
1312
from ..validation import validate
1413
from .base import ExecutionContext, ExecutionResult, ResolveInfo, Undefined, collect_fields, default_resolve_fn, \
1514
get_argument_values, get_field_def, get_operation_root_type
@@ -230,7 +229,7 @@ def complete_value(self, ctx, return_type, field_asts, info, result):
230229
return completed
231230

232231
# If result is null-like, return null.
233-
if is_nullish(result):
232+
if result is None:
234233
return None
235234

236235
# If field type is List, complete each item in the list with the inner type
@@ -254,7 +253,7 @@ def complete_value(self, ctx, return_type, field_asts, info, result):
254253
if isinstance(return_type, (GraphQLScalarType, GraphQLEnumType)):
255254
serialized_result = return_type.serialize(result)
256255

257-
if is_nullish(serialized_result):
256+
if serialized_result is None:
258257
return None
259258

260259
return serialized_result

graphql/core/execution/values.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
GraphQLScalarType,
1212
is_input_type
1313
)
14-
from ..utils.is_nullish import is_nullish
1514
from ..utils.is_valid_value import is_valid_value
1615
from ..utils.type_from_ast import type_from_ast
1716
from ..utils.value_from_ast import value_from_ast
@@ -56,10 +55,10 @@ def get_argument_values(arg_defs, arg_asts, variables):
5655
variables
5756
)
5857

59-
if is_nullish(value):
58+
if value is None:
6059
value = arg_def.default_value
6160

62-
if not is_nullish(value):
61+
if value is not None:
6362
result[name] = value
6463

6564
return result
@@ -81,14 +80,14 @@ def get_variable_value(schema, definition_ast, input):
8180
)
8281

8382
if is_valid_value(type, input):
84-
if is_nullish(input):
83+
if input is None:
8584
default_value = definition_ast.default_value
8685
if default_value:
8786
return value_from_ast(default_value, type)
8887

8988
return coerce_value(type, input)
9089

91-
if is_nullish(input):
90+
if input is None:
9291
raise GraphQLError(
9392
'Variable "${}" of required type "{}" was not provided.'.format(
9493
variable.name.value,
@@ -115,7 +114,7 @@ def coerce_value(type, value):
115114
# We only call this function after calling isValidValue.
116115
return coerce_value(type.of_type, value)
117116

118-
if is_nullish(value):
117+
if value is None:
119118
return None
120119

121120
if isinstance(type, GraphQLList):
@@ -130,19 +129,15 @@ def coerce_value(type, value):
130129
obj = {}
131130
for field_name, field in fields.items():
132131
field_value = coerce_value(field.type, value[field_name])
133-
if is_nullish(field_value):
132+
if field_value is None:
134133
field_value = field.default_value
135134

136-
if not is_nullish(field_value):
135+
if field_value is not None:
137136
obj[field_name] = field_value
138137

139138
return obj
140139

141140
assert isinstance(type, (GraphQLScalarType, GraphQLEnumType)), \
142141
'Must be input type'
143142

144-
parsed = type.parse_value(value)
145-
if not is_nullish(parsed):
146-
return parsed
147-
148-
return None
143+
return type.parse_value(value)

graphql/core/utils/ast_from_value.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,13 @@
1111
GraphQLNonNull,
1212
)
1313
from ..type.scalars import GraphQLFloat
14-
from .is_nullish import is_nullish
1514

1615

1716
def ast_from_value(value, type=None):
1817
if isinstance(type, GraphQLNonNull):
1918
return ast_from_value(value, type.of_type)
2019

21-
if is_nullish(value):
20+
if value is None:
2221
return None
2322

2423
if isinstance(value, list):

graphql/core/utils/is_nullish.py

Lines changed: 0 additions & 2 deletions
This file was deleted.

graphql/core/utils/is_valid_literal_value.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
GraphQLNonNull,
77
GraphQLScalarType,
88
)
9-
from .is_nullish import is_nullish
109

1110

1211
def is_valid_literal_value(type, value_ast):
@@ -49,4 +48,4 @@ def is_valid_literal_value(type, value_ast):
4948

5049
assert isinstance(type, (GraphQLScalarType, GraphQLEnumType)), 'Must be input type'
5150

52-
return not is_nullish(type.parse_literal(value_ast))
51+
return type.parse_literal(value_ast) is not None

graphql/core/utils/is_valid_value.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,17 @@
1111
GraphQLNonNull,
1212
GraphQLScalarType,
1313
)
14-
from .is_nullish import is_nullish
1514

1615

1716
def is_valid_value(type, value):
1817
"""Given a type and any value, return True if that value is valid."""
1918
if isinstance(type, GraphQLNonNull):
20-
if is_nullish(value):
19+
if value is None:
2120
return False
2221

2322
return is_valid_value(type.of_type, value)
2423

25-
if is_nullish(value):
24+
if value is None:
2625
return True
2726

2827
if isinstance(type, GraphQLList):
@@ -54,4 +53,4 @@ def is_valid_value(type, value):
5453

5554
# Scalar/Enum input checks to ensure the type can parse the value to
5655
# a non-null value.
57-
return not is_nullish(type.parse_value(value))
56+
return type.parse_value(value) is not None

graphql/core/utils/schema_printer.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
GraphQLUnionType
99
)
1010
from .ast_from_value import ast_from_value
11-
from .is_nullish import is_nullish
1211

1312

1413
def print_schema(schema):
@@ -118,7 +117,7 @@ def _print_args(field):
118117

119118

120119
def _print_input_value(arg):
121-
if not is_nullish(arg.default_value):
120+
if arg.default_value is not None:
122121
default_value = ' = ' + print_ast(ast_from_value(arg.default_value, arg.type))
123122
else:
124123
default_value = ''

graphql/core/utils/value_from_ast.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from ..language import ast
22
from ..type import (GraphQLEnumType, GraphQLInputObjectType, GraphQLList, GraphQLNonNull, GraphQLScalarType)
3-
from .is_nullish import is_nullish
43

54

65
def value_from_ast(value_ast, type, variables=None):
@@ -63,8 +62,4 @@ def value_from_ast(value_ast, type, variables=None):
6362
assert isinstance(type, (GraphQLScalarType, GraphQLEnumType)), \
6463
'Must be input type'
6564

66-
parsed = type.parse_literal(value_ast)
67-
if not is_nullish(parsed):
68-
return parsed
69-
70-
return None
65+
return type.parse_literal(value_ast)

tests/core_utils/test_schema_printer.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
from collections import OrderedDict
22
from graphql.core.type.definition import GraphQLField, GraphQLArgument, GraphQLInputObjectField, GraphQLEnumValue
33
from graphql.core.utils.schema_printer import print_schema, print_introspection_schema
4-
from graphql.core.utils.is_nullish import is_nullish
5-
from graphql.core.language.printer import print_ast
64
from graphql.core.type import (
75
GraphQLSchema,
86
GraphQLInputObjectType,
@@ -526,4 +524,4 @@ def test_prints_introspection_schema():
526524
LIST
527525
NON_NULL
528526
}
529-
'''
527+
'''

0 commit comments

Comments
 (0)