Skip to content

Don't call .format() on lazy-translated strings during Field.__init__ #3354

Closed

Description

@lovelydinosaur

This from IRC...

[17:04:05] <__zer01>     after adding the `min_value` kwarg to a serializer.DecimalField I get a `django.core.exceptions.AppRegistryNotReady: The translation infrastructure cannot be initialized before the apps registry is ready.` error when running tests, any ideas?
[17:07:57] <__zer01>     here's the end of the stack trace http://pastebin.com/B7ECdL6w

Which is:

File "/home/eugenio/spartan/rest/serializers.py", line 80, in MySerializer min_value=1.01)
File "/home/eugenio/Envs/spartan/lib/python3.4/site-packages/rest_framework/fields.py", line 906, in __init__
message = self.error_messages['min_value'].format(min_value=self.min_value)
File "/home/eugenio/Envs/spartan/lib/python3.4/site-packages/django/utils/functional.py", line 136, in __wrapper__ res = func(*self.__args, **self.__kw)
File "/home/eugenio/Envs/spartan/lib/python3.4/site-packages/django/utils/translation/__init__.py", line 84, in ugettext
return _trans.ugettext(message)
File "/home/eugenio/Envs/spartan/lib/python3.4/site-packages/django/utils/translation/trans_real.py", line 321, in gettext
return do_translate(message, 'gettext')
File "/home/eugenio/Envs/spartan/lib/python3.4/site-packages/django/utils/translation/trans_real.py", line 304, in do_translate
_default = _default or translation(settings.LANGUAGE_CODE)
File "/home/eugenio/Envs/spartan/lib/python3.4/site-packages/django/utils/translation/trans_real.py", line 206, in translation
_translations[language] = DjangoTranslation(language)
File "/home/eugenio/Envs/spartan/lib/python3.4/site-packages/django/utils/translation/trans_real.py", line 116, in __init__
self._add_installed_apps_translations()
File "/home/eugenio/Envs/spartan/lib/python3.4/site-packages/django/utils/translation/trans_real.py", line 164, in _add_installed_apps_translations
"The translation infrastructure cannot be initialized before the "
django.core.exceptions.AppRegistryNotReady: The translation infrastructure cannot be initialized before the apps registry is ready. Check that you don't make non-lazy gettext calls at import time.

And...

[17:08:41]   yuriheupa ([email protected]) joined the channel.
[17:09:26]   yuriheupa ([email protected]) left IRC. (Remote host closed the connection)
[17:11:54] <tomchristie>     Hrm
[17:13:14] <__zer01>     I'm running djangorestframework==3.2.3 and Django==1.8.4
[17:13:28] <tomchristie>     I think that may need raising as an issue
[17:14:13] <tomchristie>     The error is being raised because we're calling .format on a lazy-translated error message
[17:14:34] <tomchristie>     Due to it being passed a min_value which is included in the message
[17:15:00] <tomchristie>     Prob is, that causes the translation machinary to attempt to run, and produce the translated output
[17:15:12] <tomchristie>     But that can't happen yet because the app isn't setup and running
[17:15:23] <tomchristie>     Looks like Django internally handles this a little diff, eg
[17:15:34] <tomchristie>     This… https://github.com/django/django/blob/master/django/forms/fields.py#L258
[17:15:43] <tomchristie>     vs
[17:16:09] <tomchristie>     This: https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/fields.py#L820-L821
[17:16:23] <tomchristie>     Notive that we're calling .format when the field is instantiated
[17:16:31] <tomchristie>     But same is not happening in Django
[17:16:37] <tomchristie>     You may be able to resolve
[17:16:53] <tomchristie>     by ensuring that you're not importing the serializers prior to the app setup
[17:16:54]   marc_v92 (~marc_v92@unaffiliated/marc-v92/x-6202358) joined the channel.
[17:17:07] <tomchristie>     (Eg if you import them in your settings I guess that's when it blows up)

Activity

  1. nryoung commented on Sep 11, 2015

    @nryoung
    Contributor

    Really all that needs to be changed here is that these particular lines

    message = self.error_messages['max_value'].format(max_value=self.max_value)

    need to be removed?

    Doing this and running the tests does not yield any test failures, but then again I am not sure where else message is used.

  2. nryoung commented on Sep 15, 2015

    @nryoung
    Contributor

    ping @tomchristie or @kevin-brown for the above. If this is the right approach and I have already made the changes and could push them out. Let me know when you get a chance 😄

  3. xordoquy commented on Sep 15, 2015

    @xordoquy
    Contributor

    @nryoung we may lack a test for that particular string. It looks like the omitting the format will result in the max_value not being correctly filled whenever an error occurs.

  4. nryoung commented on Sep 15, 2015

    @nryoung
    Contributor

    @nryoung we may lack a test for that particular string. It looks like the omitting the format will result in the max_value not being correctly filled whenever an error occurs.

    This does seem to be the case. It seems like we could use the message class attribute of Django's MaxValueValidator since it is a simple inheritance of this class: https://github.com/django/django/blob/master/django/core/validators.py#L318

  5. nryoung commented on Sep 16, 2015

    @nryoung
    Contributor

    I have been doing some research on this.

    For Django versions >= 1.8 message is optional in the __init__ method:

    but, message is also defaulted as a class attribute here:

    For Django versions < 1.8 message is not expected:

    thus, that is why it is popped off here:

    but, message is also defaulted here:

    I believe, that removing the message assignments here and other similar ones:

    and not passing message in to the instantiation of the validator, it should default to the Django class attribute:

    I will make a PR for what I outline above. As always, let me know if I am wrong here.

  6. added 3 commits that reference this issue on Sep 16, 2015
    e5e1f4f
    db16728
    2cd4162
  7. added this to the 3.2.5 Release milestone on Sep 23, 2015
  8. added a commit that references this issue on Sep 23, 2015
    1f20cb7
  9. 86 remaining items

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions