Deprecate ResponseTypes itself

This commit is contained in:
Adrián Chaves 2022-06-30 15:29:33 +02:00
parent fd2317bd6d
commit 1ea5a8e9e0
2 changed files with 27 additions and 3 deletions

View File

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

View File

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