mirror of https://github.com/scrapy/scrapy.git
Deprecate build_from_crawler() calling from_settings().
This commit is contained in:
parent
eda1a8a7c5
commit
8700a5b7a9
|
|
@ -54,6 +54,14 @@ class MiddlewareManager:
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _build_from_settings(objcls: type[_T], settings: BaseSettings) -> _T:
|
def _build_from_settings(objcls: type[_T], settings: BaseSettings) -> _T:
|
||||||
if hasattr(objcls, "from_settings"):
|
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]
|
instance = objcls.from_settings(settings) # type: ignore[attr-defined]
|
||||||
method_name = "from_settings"
|
method_name = "from_settings"
|
||||||
else:
|
else:
|
||||||
|
|
|
||||||
|
|
@ -185,6 +185,14 @@ def build_from_crawler(
|
||||||
instance = objcls.from_crawler(crawler, *args, **kwargs) # type: ignore[attr-defined]
|
instance = objcls.from_crawler(crawler, *args, **kwargs) # type: ignore[attr-defined]
|
||||||
method_name = "from_crawler"
|
method_name = "from_crawler"
|
||||||
elif hasattr(objcls, "from_settings"):
|
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]
|
instance = objcls.from_settings(crawler.settings, *args, **kwargs) # type: ignore[attr-defined]
|
||||||
method_name = "from_settings"
|
method_name = "from_settings"
|
||||||
else:
|
else:
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ from weakref import WeakKeyDictionary
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
from scrapy.exceptions import ScrapyDeprecationWarning
|
||||||
from scrapy.http import Request
|
from scrapy.http import Request
|
||||||
from scrapy.utils.python import to_bytes
|
from scrapy.utils.python import to_bytes
|
||||||
from scrapy.utils.request import (
|
from scrapy.utils.request import (
|
||||||
|
|
@ -384,7 +385,9 @@ class CustomRequestFingerprinterTestCase(unittest.TestCase):
|
||||||
"REQUEST_FINGERPRINTER_CLASS": RequestFingerprinter,
|
"REQUEST_FINGERPRINTER_CLASS": RequestFingerprinter,
|
||||||
"FINGERPRINT": b"fingerprint",
|
"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")
|
request = Request("http://www.example.com")
|
||||||
fingerprint = crawler.request_fingerprinter.fingerprint(request)
|
fingerprint = crawler.request_fingerprinter.fingerprint(request)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue