Deprecate not configuring a default content type.

This commit is contained in:
Dave Shawley 2021-10-02 11:26:26 -04:00
parent 4dd46eda5b
commit d605acb5b7
No known key found for this signature in database
GPG key ID: F41A8A99298F8EED
3 changed files with 25 additions and 3 deletions

View file

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

View file

@ -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],

View file

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