Skip to content

Commit

Permalink
fix bug with ChoiceField
Browse files Browse the repository at this point in the history
  • Loading branch information
Friskes committed May 21, 2024
1 parent aee1cff commit f17acff
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 5 deletions.
7 changes: 5 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ extend-select = [
"TID", # https://docs.astral.sh/ruff/rules/#flake8-tidy-imports-tid
"TCH", # https://docs.astral.sh/ruff/rules/#flake8-type-checking-tch
"INT", # https://docs.astral.sh/ruff/rules/#flake8-gettext-int
"TD", # https://docs.astral.sh/ruff/rules/#flake8-todos-td
"FLY", # https://docs.astral.sh/ruff/rules/#flynt-fly
"PERF", # https://docs.astral.sh/ruff/rules/#perflint-perf
"LOG", # https://docs.astral.sh/ruff/rules/#flake8-logging-log
Expand All @@ -188,8 +189,8 @@ extend-select = [
]
ignore = [
# https://docs.astral.sh/ruff/rules/#pyflakes-f
"F401", # (не ругаться на неиспользуемые импорты)
"F841", # (не ругаться на неиспользуемые переменные)
# "F401", # (не ругаться на неиспользуемые импорты)
# "F841", # (не ругаться на неиспользуемые переменные)
"F403", # (не ругаться на использование from ... import *)
# https://docs.astral.sh/ruff/rules/#pyupgrade-up
"UP031", # (не ругаться на форматирование с помощью %s)
Expand Down Expand Up @@ -221,6 +222,8 @@ ignore = [
# https://docs.astral.sh/ruff/rules/#flake8-comprehensions-c4
# {} VS dict() # https://switowski.com/blog/dict-function-vs-literal-syntax/
"C408", # (не ругаться на использование классов коллекций вместо их литералов)
"TD001", # (не ругаться на использование FIXME и XXX)
"TD003", # (не ругаться на отсутствие ссылки на issues)
]
# Не давать исправлять эти ошибки в тултипе, и в том числе автоматически при линте через команду
unfixable = [
Expand Down
1 change: 1 addition & 0 deletions src/drf_serializer_dumps/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .decorators import serializer_dumps # noqa: F401
7 changes: 6 additions & 1 deletion src/drf_serializer_dumps/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ def your_api_method(self, request, *args, **kwargs):
_now = timezone.now()

field_type_mapping = {
serializers.ChoiceField: 'string',
serializers.CharField: 'string',
serializers.FloatField: 1.0,
serializers.BooleanField: False,
Expand Down Expand Up @@ -196,7 +197,11 @@ def _walk_fields_recursively(
if base_field_cls is not serializers.Field and not issubclass(
base_field_cls, serializers.Serializer
):
example_val[field_name] = base_field_cls().to_representation(
kwargs = {}
if isinstance(field_instance, serializers.ChoiceField):
kwargs.update({'choices': ()})

example_val[field_name] = base_field_cls(**kwargs).to_representation(
type_mapping.get(base_field_cls)
)
elif isinstance(field_instance, serializers.Serializer):
Expand Down
1 change: 1 addition & 0 deletions tests/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class PersonCars(serializers.Serializer):

class PersonHouse(serializers.Serializer):
address = serializers.CharField()
cfield = serializers.ChoiceField(choices=())


class PersonSerializer1(serializers.Serializer):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def test_serializer_dumps() -> None:
'height': 1,
'weight': 1,
'cars': [{'car_name': 'string', 'car_price': 1}],
'house': {'address': 'string'},
'house': {'address': 'string', 'cfield': 'string'},
}


Expand All @@ -38,7 +38,7 @@ def test_renew_type_value() -> None:
'height': 1,
'weight': 1,
'cars': [{'car_name': 'string', 'car_price': 1}],
'house': {'address': 'string'},
'house': {'address': 'string', 'cfield': 'string'},
}

result1 = serializer_dumps(PersonSerializer1, renew_type_value=True)
Expand Down

0 comments on commit f17acff

Please sign in to comment.