Deprecate ResponseTypes.CLASSES

This commit is contained in:
Adrián Chaves 2022-06-30 16:24:42 +02:00
parent 1ea5a8e9e0
commit 54a0248301
2 changed files with 40 additions and 1 deletions

View File

@ -11,7 +11,19 @@ from scrapy.utils.python import binary_is_text, to_bytes, to_unicode
from scrapy.utils.response import _MIME_TYPES
class ResponseTypes:
class _ResponseTypesMeta(type):
def __getattribute__(self, name):
if name == 'CLASSES':
warn(
'scrapy.responsetypes.ResponseTypes.CLASSES is deprecated',
ScrapyDeprecationWarning,
stacklevel=2,
)
return type.__getattribute__(self, name)
class ResponseTypes(metaclass=_ResponseTypesMeta):
CLASSES = {
'text/html': 'scrapy.http.HtmlResponse',
@ -47,6 +59,15 @@ class ResponseTypes:
for mimetype, cls in self.CLASSES.items():
self.classes[mimetype] = load_object(cls)
def __getattribute__(self, name):
if name == 'CLASSES':
warn(
'scrapy.responsetypes.ResponseTypes.CLASSES is deprecated',
ScrapyDeprecationWarning,
stacklevel=2,
)
return super().__getattribute__(name)
def from_mimetype(self, mimetype):
"""Return the most appropriate Response class for the given mimetype"""
warn('ResponseTypes.from_mimetype is deprecated, '

View File

@ -127,6 +127,24 @@ class ResponseTypesTest(unittest.TestCase):
messages = {str(warning.message) for warning in warnings}
self.assertIn(expected_message, messages)
def test_class_classes_deprecation(self):
with catch_warnings(record=True) as warnings:
ResponseTypes.CLASSES
expected_message = (
'scrapy.responsetypes.ResponseTypes.CLASSES is deprecated'
)
messages = {str(warning.message) for warning in warnings}
self.assertIn(expected_message, messages)
def test_instance_classes_deprecation(self):
with catch_warnings(record=True) as warnings:
responsetypes.CLASSES
expected_message = (
'scrapy.responsetypes.ResponseTypes.CLASSES is deprecated'
)
messages = {str(warning.message) for warning in warnings}
self.assertIn(expected_message, messages)
if __name__ == "__main__":
unittest.main()