Skip to content

Comments

swev-id: django__django-15741 fix get_format lazy literal#552

Open
casey-brooks wants to merge 1 commit intodjango__django-15741from
fix/get-format-lazy
Open

swev-id: django__django-15741 fix get_format lazy literal#552
casey-brooks wants to merge 1 commit intodjango__django-15741from
fix/get-format-lazy

Conversation

@casey-brooks
Copy link

Summary

  • normalize `format_type` via `force_str` and treat non-identifier literals as direct values
  • preserve localization module lookups for named keys, including lazy names
  • add regression coverage for lazy literals in `get_format` and the date filter

Testing

  • `PYTHONPATH=$PWD:$PWD/tests:$HOME/.nix-profile/lib/python3.11/site-packages DJANGO_SETTINGS_MODULE=tests.test_sqlite python3 tests/runtests.py i18n --parallel=1`
  • `PYTHONPATH=$PWD:$PWD/tests:$HOME/.nix-profile/lib/python3.11/site-packages DJANGO_SETTINGS_MODULE=tests.test_sqlite python3 tests/runtests.py template_tests.filter_tests.test_date --parallel=1`
  • `/root/.local/bin/flake8 django/utils/formats.py tests/i18n/tests.py tests/template_tests/filter_tests/test_date.py`

Reproduction

  1. Install deps: `pip3 install --user --break-system-packages asgiref sqlparse pytz`
  2. Set `PYTHONPATH=/workspace/django`
  3. Run the script from Allow lazy parameter in django.utils.formats.get_format #551 (see description) rendering {{ d|date:_('Y-m-d') }}

Observed failure (before fix)

Traceback (most recent call last):
  File "reproduce_lazy_date.py", line 22, in <module>
    print(tmpl.render(ctx))
  File "django/template/defaultfilters.py", line 764, in date
    return formats.date_format(value, arg)
  File "django/utils/formats.py", line 128, in get_format
    val = getattr(module, format_type, None)
TypeError: attribute name must be string, not '__proxy__'

Fixes #551

@casey-brooks casey-brooks requested a review from a team January 6, 2026 13:57
@casey-brooks
Copy link
Author

Test & Lint Summary

  • PYTHONPATH=$PWD:$PWD/tests:$HOME/.nix-profile/lib/python3.11/site-packages DJANGO_SETTINGS_MODULE=tests.test_sqlite python3 tests/runtests.py i18n --parallel=1 (217 passed, 0 failed, 74 skipped)
  • PYTHONPATH=$PWD:$PWD/tests:$HOME/.nix-profile/lib/python3.11/site-packages DJANGO_SETTINGS_MODULE=tests.test_sqlite python3 tests/runtests.py template_tests.filter_tests.test_date --parallel=1 (14 passed, 0 failed)
  • /root/.local/bin/flake8 django/utils/formats.py tests/i18n/tests.py tests/template_tests/filter_tests/test_date.py (no issues)

Copy link

@noa-lucent noa-lucent left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great. Thanks for the thorough regression coverage.

@casey-brooks
Copy link
Author

Test & Lint Summary

  • `PYTHONPATH=$PWD:$PWD/tests:$(python3 -c 'import site; print(site.USER_SITE)') DJANGO_SETTINGS_MODULE=tests.test_sqlite python3 tests/runtests.py i18n --parallel=1` (passed; 217 tests, 74 skipped)
  • `PYTHONPATH=$PWD:$PWD/tests:$(python3 -c 'import site; print(site.USER_SITE)') DJANGO_SETTINGS_MODULE=tests.test_sqlite python3 tests/runtests.py template_tests.filter_tests.test_date --parallel=1` (passed; 14 tests)
  • Linting not run (no code changes)

Notes: `tests/runtests.py` rejects labels ending in .py; use the dotted module path `template_tests.filter_tests.test_date` instead.

Copy link

@noa-lucent noa-lucent left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Normalization via force_str fixes the lazy translation regression and the added identifier guard keeps module lookups intact for named settings. Regression tests cover both get_format() and the date filter path. Looks solid.

Copy link
Collaborator

@rowan-stein rowan-stein left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving based on successful local validation and Noa’s review. Fix correctly normalizes lazy literals and preserves named setting lookups; targeted tests (i18n, template date filter) pass.

@rowan-stein rowan-stein changed the base branch from django__django-15741 to main January 11, 2026 19:49
@rowan-stein rowan-stein changed the base branch from main to django__django-15741 January 12, 2026 20:13
Copy link

@noa-lucent noa-lucent left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

force_str coercion keeps get_format working with lazy literals and tests cover literal & setting cases. Looks good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow lazy parameter in django.utils.formats.get_format

3 participants