diff --git a/scrapy/responsetypes.py b/scrapy/responsetypes.py index 180cb094e..46dc28efb 100644 --- a/scrapy/responsetypes.py +++ b/scrapy/responsetypes.py @@ -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, ' diff --git a/tests/test_responsetypes.py b/tests/test_responsetypes.py index 4ce41f086..434993c0b 100644 --- a/tests/test_responsetypes.py +++ b/tests/test_responsetypes.py @@ -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()