mirror of
				https://github.com/django/django.git
				synced 2025-10-31 01:25:32 +00:00 
			
		
		
		
	Ensured that registered checks accept keyword arguments.
This commit is contained in:
		
				
					committed by
					
						 Mariusz Felisiak
						Mariusz Felisiak
					
				
			
			
				
	
			
			
			
						parent
						
							ef39a8829b
						
					
				
				
					commit
					cf2ca22a57
				
			| @@ -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) | ||||
|   | ||||
| @@ -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): | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user