From d605acb5b707cbfe2024b5c02de725a9fa0755a9 Mon Sep 17 00:00:00 2001 From: Dave Shawley Date: Sat, 2 Oct 2021 11:26:26 -0400 Subject: [PATCH] Deprecate not configuring a default content type. --- docs/history.rst | 1 + sprockets/mixins/mediatype/content.py | 22 +++++++++++++++++++--- tests.py | 5 +++++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/docs/history.rst b/docs/history.rst index 849713c..ca0cf03 100644 --- a/docs/history.rst +++ b/docs/history.rst @@ -6,6 +6,7 @@ Version History - Add type annotations (see :ref:`type-info`) - Return a "406 Not Acceptable" if the :http:header:`Accept` header values cannot be matched and there is no default content type configured +- Deprecate not having a default content type configured :compare:`3.0.4 <3.0.3...3.0.4>` (2 Nov 2020) --------------------------------------------- diff --git a/sprockets/mixins/mediatype/content.py b/sprockets/mixins/mediatype/content.py index 318721e..2fe6abb 100644 --- a/sprockets/mixins/mediatype/content.py +++ b/sprockets/mixins/mediatype/content.py @@ -31,6 +31,8 @@ from __future__ import annotations import logging import typing +import warnings + try: from typing import Literal except ImportError: # pragma: no cover @@ -90,15 +92,15 @@ class ContentSettings: """ - default_content_type: typing.Union[str, None] default_encoding: typing.Union[str, None] - _handlers: typing.Dict[str, type_info.Transcoder] _available_types: typing.List[datastructures.ContentType] + _default_content_type: typing.Union[str, None] + _handlers: typing.Dict[str, type_info.Transcoder] def __init__(self) -> None: self._handlers = {} self._available_types = [] - self.default_content_type = None + self._default_content_type = None self.default_encoding = None def __getitem__(self, content_type: str) -> type_info.Transcoder: @@ -137,6 +139,20 @@ class ContentSettings: """ return self._available_types + @property + def default_content_type(self) -> typing.Union[str, None]: + return self._default_content_type + + @default_content_type.setter + def default_content_type(self, new_value: typing.Union[str, None]) -> None: + if new_value is None: + warnings.warn( + DeprecationWarning( + 'Using sprockets.mixins.mediatype without a default' + ' content type is deprecated and will become an error' + ' in a future version')) + self._default_content_type = new_value + def install(application: type_info.HasSettings, default_content_type: typing.Optional[str], diff --git a/tests.py b/tests.py index 469798b..5e1e672 100644 --- a/tests.py +++ b/tests.py @@ -285,6 +285,11 @@ class ContentSettingsTests(unittest.TestCase): 'json') self.assertEqual(settings['application/json; charset=utf-8'], handler) + def test_that_setting_no_default_content_type_warns(self): + settings = content.ContentSettings() + with self.assertWarns(DeprecationWarning): + settings.default_content_type = None + class ContentFunctionTests(unittest.TestCase): def setUp(self):