Scrapy, a fast high-level web crawling & scraping framework for Python.
Deprecate build_from_crawler() calling from_settings().
Andrey Rakhmatullin8700a5b7a925
diff --git a/scrapy/middleware.py b/scrapy/middleware.pyindex 2b67dcd21..91411506f 100644--- a/scrapy/middleware.py+++ b/scrapy/middleware.py@@ -54,6 +54,14 @@ class MiddlewareManager:@staticmethoddef _build_from_settings(objcls: type[_T], settings: BaseSettings) -> _T:if hasattr(objcls, "from_settings"):+ warnings.warn(+ f"{objcls.__qualname__} has from_settings() but not from_crawler()."+ " This is deprecated and calling from_settings() will be removed in a future"+ " Scrapy version. You can implement a simple from_crawler() that calls"+ " from_settings() with crawler.settings.",+ category=ScrapyDeprecationWarning,+ stacklevel=2,+ )instance = objcls.from_settings(settings) # type: ignore[attr-defined]method_name = "from_settings"else:diff --git a/scrapy/utils/misc.py b/scrapy/utils/misc.pyindex efb475131..a408a205d 100644--- a/scrapy/utils/misc.py+++ b/scrapy/utils/misc.py@@ -185,6 +185,14 @@ def build_from_crawler(instance = objcls.from_crawler(crawler, *args, **kwargs) # type: ignore[attr-defined]method_name = "from_crawler"elif hasattr(objcls, "from_settings"):+ warnings.warn(+ f"{objcls.__qualname__} has from_settings() but not from_crawler()."+ " This is deprecated and calling from_settings() will be removed in a future"+ " Scrapy version. You can implement a simple from_crawler() that calls"+ " from_settings() with crawler.settings.",+ category=ScrapyDeprecationWarning,+ stacklevel=2,+ )instance = objcls.from_settings(crawler.settings, *args, **kwargs) # type: ignore[attr-defined]method_name = "from_settings"else:diff --git a/tests/test_utils_request.py b/tests/test_utils_request.pyindex 965d050a4..0a3e3b00b 100644--- a/tests/test_utils_request.py+++ b/tests/test_utils_request.py@@ -8,6 +8,7 @@ from weakref import WeakKeyDictionaryimport pytest+from scrapy.exceptions import ScrapyDeprecationWarningfrom scrapy.http import Requestfrom scrapy.utils.python import to_bytesfrom scrapy.utils.request import (@@ -384,7 +385,9 @@ class CustomRequestFingerprinterTestCase(unittest.TestCase):"REQUEST_FINGERPRINTER_CLASS": RequestFingerprinter,"FINGERPRINT": b"fingerprint",}- crawler = get_crawler(settings_dict=settings)+ with warnings.catch_warnings():+ warnings.simplefilter("ignore", ScrapyDeprecationWarning)+ crawler = get_crawler(settings_dict=settings)request = Request("http://www.example.com")fingerprint = crawler.request_fingerprinter.fingerprint(request)