Skip to content

Commit

Permalink
Fixed #32285 -- Raised ImproperlyConfigured when AppConfig.label is n…
Browse files Browse the repository at this point in the history
…ot a valid Python identifier.
  • Loading branch information
hramezani authored and felixxm committed Dec 22, 2020
1 parent 110001d commit 8b2a30f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
4 changes: 4 additions & 0 deletions django/apps/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ def __init__(self, app_name, app_module):
# This value must be unique across a Django project.
if not hasattr(self, 'label'):
self.label = app_name.rpartition(".")[2]
if not self.label.isidentifier():
raise ImproperlyConfigured(
"The app label '%s' is not a valid Python identifier." % self.label
)

# Human-readable name for the application e.g. "Admin".
if not hasattr(self, 'verbose_name'):
Expand Down
8 changes: 8 additions & 0 deletions tests/apps/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,14 @@ def test_repr(self):
ac = AppConfig('label', Stub(__path__=['a']))
self.assertEqual(repr(ac), '<AppConfig: label>')

def test_invalid_label(self):
class MyAppConfig(AppConfig):
label = 'invalid.label'

msg = "The app label 'invalid.label' is not a valid Python identifier."
with self.assertRaisesMessage(ImproperlyConfigured, msg):
MyAppConfig('test_app', Stub())

@override_settings(
INSTALLED_APPS=['apps.apps.ModelPKAppsConfig'],
DEFAULT_AUTO_FIELD='django.db.models.SmallAutoField',
Expand Down

0 comments on commit 8b2a30f

Please sign in to comment.