1
0
mirror of https://github.com/django/django.git synced 2024-12-23 01:25:58 +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 itertools import chain
from django.utils.inspect import func_accepts_kwargs
from django.utils.itercompat import is_iterable from django.utils.itercompat import is_iterable
@ -38,13 +39,17 @@ class CheckRegistry:
registry = CheckRegistry() registry = CheckRegistry()
@registry.register('mytag', 'anothertag') @registry.register('mytag', 'anothertag')
def my_check(apps, **kwargs): def my_check(app_configs, **kwargs):
# ... perform checks and collect `errors` ... # ... perform checks and collect `errors` ...
return errors return errors
# or # or
registry.register(my_check, 'mytag', 'anothertag') registry.register(my_check, 'mytag', 'anothertag')
""" """
def inner(check): def inner(check):
if not func_accepts_kwargs(check):
raise TypeError(
'Check functions must accept keyword arguments (**kwargs).'
)
check.tags = tags check.tags = tags
checks = self.deployment_checks if kwargs.get('deploy') else self.registered_checks checks = self.deployment_checks if kwargs.get('deploy') else self.registered_checks
checks.add(check) checks.add(check)

View File

@ -66,6 +66,14 @@ class SystemCheckFrameworkTests(SimpleTestCase):
self.assertEqual(errors, errors2) self.assertEqual(errors, errors2)
self.assertEqual(sorted(errors), [4, 5]) 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): class MessageTests(SimpleTestCase):