diff --git a/graphql/pyutils/ordereddict.py b/graphql/pyutils/ordereddict.py index 5a6b2b69..3bf82177 100644 --- a/graphql/pyutils/ordereddict.py +++ b/graphql/pyutils/ordereddict.py @@ -1,8 +1,16 @@ -try: - # Try to load the Cython performant OrderedDict (C) - # as is more performant than collections.OrderedDict (Python) - from cyordereddict import OrderedDict # type: ignore -except ImportError: - from collections import OrderedDict +import sys + +if sys.version_info >= (3, 7): + # As of Python 3.7, dictionaries are specified to preserve insertion order + OrderedDict = dict + +else: + try: + # Try to load the Cython performant OrderedDict (C) + # as is more performant than collections.OrderedDict (Python) + from cyordereddict import OrderedDict # type: ignore + except ImportError: + from collections import OrderedDict + __all__ = ["OrderedDict"] diff --git a/graphql/type/definition.py b/graphql/type/definition.py index 2f499aec..e1cee1b3 100644 --- a/graphql/type/definition.py +++ b/graphql/type/definition.py @@ -404,6 +404,7 @@ def __init__( @cached_property def fields(self): # type: () -> Dict[str, GraphQLField] + assert self._fields is not None, '"fields" cannot be None' return define_field_map(self, self._fields) diff --git a/graphql/type/tests/test_enum_type.py b/graphql/type/tests/test_enum_type.py index 989fac82..3feb2fdd 100644 --- a/graphql/type/tests/test_enum_type.py +++ b/graphql/type/tests/test_enum_type.py @@ -1,3 +1,4 @@ +import sys from collections import OrderedDict from rx import Observable from graphql import graphql @@ -11,6 +12,7 @@ GraphQLSchema, GraphQLString, ) +import pytest ColorType = GraphQLEnumType( name="Color", @@ -263,6 +265,10 @@ def test_presents_a_get_value_api(): assert badUsage is None +@pytest.mark.skipif( + sys.version_info >= (3, 7), + reason="As of Python 3.7 we use built-in dicts, which preserve order", +) def test_sorts_values_if_not_using_ordered_dict(): enum = GraphQLEnumType( name="Test",