Skip to content

Commit

Permalink
Keep dict key order for Python 3.7
Browse files Browse the repository at this point in the history
  • Loading branch information
axnsan12 committed Jan 14, 2019
1 parent 0e62fd6 commit 7624672
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/drf_yasg/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from django.utils.functional import Promise
from inflection import camelize

from .utils import filter_none, force_real_str
from .utils import dict_has_ordered_keys, filter_none, force_real_str

try:
from collections import abc as collections_abc
Expand Down Expand Up @@ -145,7 +145,7 @@ def _as_odict(obj, memo):
result = OrderedDict()
memo[id(obj)] = result
items = obj.items()
if not isinstance(obj, OrderedDict):
if not dict_has_ordered_keys(obj):
items = sorted(items)
for attr, val in items:
result[attr] = SwaggerDict._as_odict(val, memo)
Expand Down
20 changes: 16 additions & 4 deletions src/drf_yasg/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import inspect
import logging
import sys
from collections import OrderedDict

from django.db import models
Expand Down Expand Up @@ -389,8 +390,7 @@ def get_produces(renderer_classes):


def decimal_as_float(field):
"""
Returns true if ``field`` is a django-rest-framework DecimalField and its ``coerce_to_string`` attribute or the
"""Returns true if ``field`` is a django-rest-framework DecimalField and its ``coerce_to_string`` attribute or the
``COERCE_DECIMAL_TO_STRING`` setting is set to ``False``.
:rtype: bool
Expand All @@ -401,8 +401,7 @@ def decimal_as_float(field):


def get_serializer_ref_name(serializer):
"""
Get serializer's ref_name (or None for ModelSerializer if it is named 'NestedSerializer')
"""Get serializer's ref_name (or None for ModelSerializer if it is named 'NestedSerializer')
:param serializer: Serializer instance
:return: Serializer's ``ref_name`` or ``None`` for inline serializer
Expand Down Expand Up @@ -469,3 +468,16 @@ def get_field_default(field):
default = serializers.empty

return default


def dict_has_ordered_keys(obj):
"""Check if a given object is a dict that maintains insertion order.
:param obj: the dict object to check
:rtype: bool
"""
if sys.version_info >= (3, 7):
# the Python 3.7 language spec says that dict must maintain insertion order.
return isinstance(obj, dict)

return isinstance(obj, OrderedDict)

0 comments on commit 7624672

Please sign in to comment.