Skip to content

Commit

Permalink
Remove deprecation warning from ChoicesMeta in favor of ChoicesType
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Buck committed Aug 21, 2024
1 parent 823254c commit 8885ef7
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
9 changes: 8 additions & 1 deletion drf_orjson_renderer/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@
from decimal import Decimal
from typing import Any, Optional

import django
import orjson

if django.VERSION < (5, 0):
from django.db.models.enums import ChoicesMeta as ChoicesType
elif django.VERSION <= (6, 0):
from django.db.models.enums import ChoicesType

from django.utils.functional import Promise
from rest_framework.renderers import BaseRenderer
from rest_framework.settings import api_settings
Expand Down Expand Up @@ -50,7 +57,7 @@ def default(obj: Any) -> Any:
return str(obj)
else:
return float(obj)
elif isinstance(obj, (str, uuid.UUID, Promise)):
elif isinstance(obj, (str, uuid.UUID, Promise, ChoicesType)):
return str(obj)
elif hasattr(obj, "tolist"):
return obj.tolist()
Expand Down
2 changes: 1 addition & 1 deletion requirements/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ ipdb
ipython
isort
mypy==0.910
numpy==1.21.4
numpy==1.26.0
pre-commit
pytest-cov==3.0.0
twine==4.0.1
14 changes: 12 additions & 2 deletions tests/test_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
import numpy
import orjson
import pytest
from django.utils.functional import Promise, lazy
from django.db.models import TextChoices
from django.utils.functional import lazy
from rest_framework import status
from rest_framework.exceptions import ErrorDetail, ParseError
from rest_framework.settings import api_settings
Expand Down Expand Up @@ -38,6 +39,10 @@ def tolist(self):
return [1]


class ChoiceObj(TextChoices):
FIELD = "option-one", "Option One"


UUID_PARAM = uuid.uuid4()
string_doubler = lazy(lambda i: i + i, str)

Expand All @@ -53,10 +58,15 @@ def tolist(self):
(IterObj(1), [1], False),
(ReturnList([{"1": 1}], serializer=None), [{"1": 1}], False),
(ReturnDict({"a": "b"}, serializer=None), {"a": "b"}, False),
(ChoiceObj.FIELD, "option-one", False,)
]


@pytest.mark.parametrize("test_input,expected,coerce_decimal", DATA_PARAMS)
@pytest.mark.parametrize(
"test_input,expected,coerce_decimal",
DATA_PARAMS,
ids=[type(item[0]) for item in DATA_PARAMS],
)
def test_built_in_default_method(test_input, expected, coerce_decimal):
"""Ensure that the built-in default method works for all data types."""
api_settings.COERCE_DECIMAL_TO_STRING = True if coerce_decimal else False
Expand Down
2 changes: 1 addition & 1 deletion version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.7.1"
__version__ = "1.7.3"

0 comments on commit 8885ef7

Please sign in to comment.