Skip to content

Commit 2823dd5

Browse files
necarisCito
authored andcommitted
If using Python >=3.7, use built-in dict as OrderedDict (#248)
If using Python >=3.7, use built-in dict as OrderedDict Since dictionaries are specified to preserve insertion order in Python 3.7 and up (see https://docs.python.org/3/whatsnew/3.7.html), it seems simplest and most performant to use the built-in type where possible.
1 parent cf221fd commit 2823dd5

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

graphql/pyutils/ordereddict.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1-
try:
2-
# Try to load the Cython performant OrderedDict (C)
3-
# as is more performant than collections.OrderedDict (Python)
4-
from cyordereddict import OrderedDict # type: ignore
5-
except ImportError:
6-
from collections import OrderedDict
1+
import sys
2+
3+
if sys.version_info >= (3, 7):
4+
# As of Python 3.7, dictionaries are specified to preserve insertion order
5+
OrderedDict = dict
6+
7+
else:
8+
try:
9+
# Try to load the Cython performant OrderedDict (C)
10+
# as is more performant than collections.OrderedDict (Python)
11+
from cyordereddict import OrderedDict # type: ignore
12+
except ImportError:
13+
from collections import OrderedDict
14+
715

816
__all__ = ["OrderedDict"]

graphql/type/definition.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,7 @@ def __init__(
404404
@cached_property
405405
def fields(self):
406406
# type: () -> Dict[str, GraphQLField]
407+
assert self._fields is not None, '"fields" cannot be None'
407408
return define_field_map(self, self._fields)
408409

409410

graphql/type/tests/test_enum_type.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import sys
12
from collections import OrderedDict
23
from rx import Observable
34
from graphql import graphql
@@ -11,6 +12,7 @@
1112
GraphQLSchema,
1213
GraphQLString,
1314
)
15+
import pytest
1416

1517
ColorType = GraphQLEnumType(
1618
name="Color",
@@ -263,6 +265,10 @@ def test_presents_a_get_value_api():
263265
assert badUsage is None
264266

265267

268+
@pytest.mark.skipif(
269+
sys.version_info >= (3, 7),
270+
reason="As of Python 3.7 we use built-in dicts, which preserve order",
271+
)
266272
def test_sorts_values_if_not_using_ordered_dict():
267273
enum = GraphQLEnumType(
268274
name="Test",

0 commit comments

Comments
 (0)