Skip to content

Commit

Permalink
🐛 Handle gettext wrapped default values in example generator
Browse files Browse the repository at this point in the history
  • Loading branch information
swrichards committed Jan 22, 2025
1 parent d7d4fe8 commit 79f19ef
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
23 changes: 18 additions & 5 deletions django_setup_configuration/documentation/setup_config_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
get_args,
get_origin,
)
from django.utils.encoding import force_str
from django.utils.functional import Promise


import ruamel.yaml
from docutils import nodes
Expand All @@ -31,6 +34,13 @@
NO_EXAMPLE = object()


class LazyEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, Promise):
return force_str(obj)
return super().default(obj)


def extract_literal_values(annotation: Type | None) -> list:
"""
Checks if the given type annotation is a Literal or contains Literals.
Expand Down Expand Up @@ -68,11 +78,14 @@ def _get_default_from_field_info(field_info: FieldInfo) -> Any:
"""
if field_info.default_factory:
return field_info.default_factory()
return (
field_info.default.value
if isinstance(field_info.default, Enum)
else field_info.default
)

match field_info.default:
case Promise():
return force_str(field_info.default)
case Enum():
return field_info.default.value
case _:
return field_info.default


def _yaml_set_wrapped_comment(
Expand Down
2 changes: 2 additions & 0 deletions testapp/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.contrib.postgres.fields import ArrayField
from django.db import models
from django.utils.translation import gettext_lazy as _


class StrChoices(models.TextChoices):
Expand Down Expand Up @@ -60,3 +61,4 @@ class DjangoModel(models.Model):
models.JSONField(), default=lambda: [{"foo": "bar"}, {"foo": "baz"}]
)
array_field = ArrayField(models.JSONField(), null=True, blank=True)
str_with_localized_default = models.TextField(default=_("Localized default"))
6 changes: 6 additions & 0 deletions tests/test_documentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import Literal, Union
from unittest.mock import patch

from django.utils.translation import gettext_lazy as _
import pytest
from docutils import nodes
from docutils.frontend import get_default_settings
Expand Down Expand Up @@ -78,6 +79,7 @@ class Meta:
"json_with_default_factory",
"nullable_str",
"int_with_choices_and_blank_and_non_choice_default",
"str_with_localized_default",
)
}
extra_kwargs = {
Expand Down Expand Up @@ -259,6 +261,10 @@ def test_directive_output(register_directive, docutils_document):
# DEFAULT VALUE: 42
# REQUIRED: false
int_with_choices_and_blank_and_non_choice_default: 42
# DEFAULT VALUE: "Localized default"
# REQUIRED: false
str_with_localized_default: Localized default
"""
)

Expand Down

0 comments on commit 79f19ef

Please sign in to comment.