1
0
mirror of https://github.com/django/django.git synced 2024-12-22 17:16:24 +00:00

Ensured that registered checks accept keyword arguments.

This commit is contained in:
Adam Johnson 2020-12-13 16:56:04 +00:00 committed by Mariusz Felisiak
parent ef39a8829b
commit cf2ca22a57
2 changed files with 14 additions and 1 deletions

View File

@ -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)

View File

@ -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):