DEV Community

Kamal Mustafa
Kamal Mustafa

Posted on

Django: _ (underscore) settings gotchas

It so happened that after we add some localization setup in our app, running our tests error out with:-

ackages/django/conf/__init__.py", line 122, in configure
INTERNALERROR>     raise TypeError('Setting %r must be uppercase.' % name)
INTERNALERROR> TypeError: Setting '_' must be uppercase.
Enter fullscreen mode Exit fullscreen mode

The settings in question that we have added is this:-

try:
    from django.utils.translation import gettext_lazy as _
except ImportError:
    _ = lambda s: s
...
...
LANGUAGES = [
    ("en", _("English")),
    ("ja", _("Japanese")),
]
Enter fullscreen mode Exit fullscreen mode

This however only happened when we run tests, it didn't happened in normal django run. Adding some breakpoint() in configure() function proved that it never get called in normal django run (such as through runserver).

My initial gut feeling tell that this is a problem with pytest_djangoapp but after I did some digging, my thinking a bit skewed.

Firstly I looked into the commit that introduced this check (not allowed _ as settings attribute). You can find it using blame on github.

Alt Text

So it brought me to issue #30234 on django project issue tracker. At this point I thought Django is at fault as the localization docs still showing example of using _ in settings.py. It doesn't make sense, if you already disallowed using _ in settings, why still show it in official docs?

Fortunately some wise guy on the issue's comment replied to my comment that this has nothing to do with Django or its docs. The problem is with pytest_djangoapp which called settings.configure() and passing _ as part of the settings dict.

I can't tell how embarrassed I am. I should have check pytest_djangoapp first before posting the comment in the issue tracker.

Top comments (0)