diff --git a/rest_framework/exceptions.py b/rest_framework/exceptions.py index 710da96b8..9b324a743 100644 --- a/rest_framework/exceptions.py +++ b/rest_framework/exceptions.py @@ -146,6 +146,3 @@ class Throttled(APIException): self.extra_detail % {'wait': self.wait} ) - -class RESTFrameworkSettingHasUnexpectedClassWarning(Warning): - pass diff --git a/rest_framework/settings.py b/rest_framework/settings.py index a5b28fa19..7e8ea7449 100644 --- a/rest_framework/settings.py +++ b/rest_framework/settings.py @@ -19,11 +19,10 @@ back to the defaults. """ from __future__ import unicode_literals from collections import Iterable -import warnings +from django.core.exceptions import ImproperlyConfigured from django.conf import settings from django.utils import importlib, six from rest_framework import ISO_8601 -from rest_framework.exceptions import RESTFrameworkSettingHasUnexpectedClassWarning USER_SETTINGS = getattr(settings, 'REST_FRAMEWORK', None) @@ -197,17 +196,14 @@ class APISettings(object): if issubclass(val.__class__, Iterable) \ and (not issubclass(default.__class__, Iterable) or isinstance(val, six.string_types)): - warnings.warn( - "The `{attr}` setting must be iterable".format(**locals()), - RESTFrameworkSettingHasUnexpectedClassWarning, - stacklevel=3 + raise ImproperlyConfigured( + 'The "{attr}" setting must be a list or a tuple' + .format(attr=attr) ) elif isinstance(default, six.string_types) and not \ isinstance(val, six.string_types): - warnings.warn( - "The `{attr}` setting must be a string".format(**locals()), - RESTFrameworkSettingHasUnexpectedClassWarning, - stacklevel=3 + raise ImproperlyConfigured( + 'The "{attr}" setting must be a string'.format(attr=attr) ) # Coerce import strings into classes diff --git a/tests/test_settings.py b/tests/test_settings.py index 337759726..cabc9a056 100644 --- a/tests/test_settings.py +++ b/tests/test_settings.py @@ -1,8 +1,8 @@ from __future__ import unicode_literals +from django.core.exceptions import ImproperlyConfigured +import pytest import warnings from django.test import TestCase -from rest_framework.exceptions import \ - RESTFrameworkSettingHasUnexpectedClassWarning from rest_framework.settings import APISettings @@ -21,37 +21,35 @@ class TestSettings(TestCase): def test_bad_iterable_setting_class_raises_warning(self): """ - Make sure warnings are emitted when settings which should be iterable - are not. + Make sure errors are raised when settings which should be iterable are + not. """ settings = APISettings({ 'DEFAULT_RENDERER_CLASSES': 'rest_framework.renderers.JSONRenderer' }) - with warnings.catch_warnings(record=True) as w: - # Trigger a warning. + # Trigger the exception + with pytest.raises(ImproperlyConfigured) as exc_info: settings.DEFAULT_RENDERER_CLASSES - # Verify that a warning is thrown - assert len(w) == 1 - assert issubclass( - w[-1].category, RESTFrameworkSettingHasUnexpectedClassWarning - ) + expected_error = ( + u'The "DEFAULT_RENDERER_CLASSES" setting must be a list or a tuple' + ) + assert exc_info.value[0] == expected_error def test_bad_string_setting_class_raises_warning(self): """ - Make sure warnings are emitted when settings which should be strings are + Make sure errors are raised when settings which should be strings are not. """ settings = APISettings({ 'DEFAULT_METADATA_CLASS': [] }) - with warnings.catch_warnings(record=True) as w: - # Trigger a warning. + # Trigger the exception + with pytest.raises(ImproperlyConfigured) as exc_info: settings.DEFAULT_METADATA_CLASS - # Verify that a warning is thrown - assert len(w) == 1 - assert issubclass( - w[-1].category, RESTFrameworkSettingHasUnexpectedClassWarning - ) + expected_error = ( + u'The "DEFAULT_METADATA_CLASS" setting must be a string' + ) + assert exc_info.value[0] == expected_error