From 1ea5a8e9e0f9922218cba77724744af85eb321b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Chaves?= Date: Thu, 30 Jun 2022 15:29:33 +0200 Subject: [PATCH] Deprecate ResponseTypes itself --- scrapy/responsetypes.py | 17 +++++++++++++++-- tests/test_responsetypes.py | 13 ++++++++++++- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/scrapy/responsetypes.py b/scrapy/responsetypes.py index 7308e0350..180cb094e 100644 --- a/scrapy/responsetypes.py +++ b/scrapy/responsetypes.py @@ -2,7 +2,7 @@ This module implements a class which returns the appropriate Response class based on different criteria. """ -from warnings import warn +from warnings import catch_warnings, simplefilter, warn from scrapy.exceptions import ScrapyDeprecationWarning from scrapy.http import Response @@ -30,6 +30,17 @@ class ResponseTypes: 'text/*': 'scrapy.http.TextResponse', } + def __new__(cls, *args, **kwargs): + warn( + ( + 'scrapy.responsetypes.ResponseTypes is deprecated, use ' + 'scrapy.utils.response.get_response_class instead' + ), + ScrapyDeprecationWarning, + stacklevel=2, + ) + return super().__new__(cls) + def __init__(self): self.classes = {} self.mimetypes = _MIME_TYPES @@ -131,4 +142,6 @@ class ResponseTypes: return cls -responsetypes = ResponseTypes() +with catch_warnings(): + simplefilter("ignore") + responsetypes = ResponseTypes() diff --git a/tests/test_responsetypes.py b/tests/test_responsetypes.py index 3d5217385..4ce41f086 100644 --- a/tests/test_responsetypes.py +++ b/tests/test_responsetypes.py @@ -1,4 +1,5 @@ import unittest +from warnings import catch_warnings import pytest @@ -9,7 +10,7 @@ from scrapy.http import ( TextResponse, XmlResponse, ) -from scrapy.responsetypes import responsetypes +from scrapy.responsetypes import responsetypes, ResponseTypes from .test_utils_response import ( POST_XTRACTMIME_SCENARIOS, PRE_XTRACTMIME_SCENARIOS, @@ -116,6 +117,16 @@ class ResponseTypesTest(unittest.TestCase): # check that mime.types files shipped with scrapy are loaded self.assertEqual(responsetypes.mimetypes.guess_type('x.scrapytest')[0], 'x-scrapy/test') + def test_class_deprecation(self): + with catch_warnings(record=True) as warnings: + ResponseTypes() + expected_message = ( + 'scrapy.responsetypes.ResponseTypes is deprecated, use ' + 'scrapy.utils.response.get_response_class instead' + ) + messages = {str(warning.message) for warning in warnings} + self.assertIn(expected_message, messages) + if __name__ == "__main__": unittest.main()