From cf2ca22a5797cdd53a10954e9811893325b24a30 Mon Sep 17 00:00:00 2001 From: Adam Johnson Date: Sun, 13 Dec 2020 16:56:04 +0000 Subject: [PATCH] Ensured that registered checks accept keyword arguments. --- django/core/checks/registry.py | 7 ++++++- tests/check_framework/tests.py | 8 ++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/django/core/checks/registry.py b/django/core/checks/registry.py index 699bf9ff30..46a04d78ff 100644 --- a/django/core/checks/registry.py +++ b/django/core/checks/registry.py @@ -1,5 +1,6 @@ from itertools import chain +from django.utils.inspect import func_accepts_kwargs from django.utils.itercompat import is_iterable @@ -38,13 +39,17 @@ class CheckRegistry: registry = CheckRegistry() @registry.register('mytag', 'anothertag') - def my_check(apps, **kwargs): + def my_check(app_configs, **kwargs): # ... perform checks and collect `errors` ... return errors # or registry.register(my_check, 'mytag', 'anothertag') """ def inner(check): + if not func_accepts_kwargs(check): + raise TypeError( + 'Check functions must accept keyword arguments (**kwargs).' + ) check.tags = tags checks = self.deployment_checks if kwargs.get('deploy') else self.registered_checks checks.add(check) diff --git a/tests/check_framework/tests.py b/tests/check_framework/tests.py index cc1718fac8..e669b11c2b 100644 --- a/tests/check_framework/tests.py +++ b/tests/check_framework/tests.py @@ -66,6 +66,14 @@ class SystemCheckFrameworkTests(SimpleTestCase): self.assertEqual(errors, errors2) self.assertEqual(sorted(errors), [4, 5]) + def test_register_no_kwargs_error(self): + registry = CheckRegistry() + msg = 'Check functions must accept keyword arguments (**kwargs).' + with self.assertRaisesMessage(TypeError, msg): + @registry.register + def no_kwargs(app_configs, databases): + pass + class MessageTests(SimpleTestCase):