Skip to content

Commit 811e1b5

Browse files
committed
Cleanup deprecated header handling
1 parent b387b42 commit 811e1b5

File tree

2 files changed

+2
-63
lines changed

2 files changed

+2
-63
lines changed

scrapy_playwright/handler.py

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import asyncio
2-
import inspect
32
import logging
43
import platform
5-
import warnings
64
from contextlib import suppress
75
from dataclasses import dataclass, field as dataclass_field
86
from functools import partial
@@ -26,7 +24,7 @@
2624
from scrapy import Spider, signals
2725
from scrapy.core.downloader.handlers.http import HTTPDownloadHandler
2826
from scrapy.crawler import Crawler
29-
from scrapy.exceptions import NotSupported, ScrapyDeprecationWarning
27+
from scrapy.exceptions import NotSupported
3028
from scrapy.http import Request, Response
3129
from scrapy.http.headers import Headers
3230
from scrapy.responsetypes import responsetypes
@@ -736,11 +734,7 @@ async def _request_handler(route: Route, playwright_request: PlaywrightRequest)
736734

737735
if self.process_request_headers is None:
738736
final_headers = await playwright_request.all_headers()
739-
elif (sig := inspect.signature(self.process_request_headers)) and (
740-
"browser_type_name" in sig.parameters
741-
and "playwright_request" in sig.parameters
742-
and "scrapy_request_data" in sig.parameters
743-
):
737+
else:
744738
overrides["headers"] = final_headers = await _maybe_await(
745739
self.process_request_headers(
746740
browser_type_name=self.config.browser_type_name,
@@ -754,24 +748,6 @@ async def _request_handler(route: Route, playwright_request: PlaywrightRequest)
754748
},
755749
)
756750
)
757-
else:
758-
warnings.warn(
759-
"Accepting positional arguments in the function passed to the"
760-
" PLAYWRIGHT_PROCESS_REQUEST_HEADERS setting is deprecated. The function"
761-
" should accept three (3) keyword arguments instead:"
762-
" browser_type_name: str,"
763-
" playwright_request: playwright.async_api.Request,"
764-
" scrapy_request_data: dict",
765-
category=ScrapyDeprecationWarning,
766-
stacklevel=1,
767-
)
768-
overrides["headers"] = final_headers = await _maybe_await(
769-
self.process_request_headers(
770-
self.config.browser_type_name,
771-
playwright_request,
772-
headers,
773-
)
774-
)
775751

776752
# if the current request corresponds to the original scrapy one
777753
if (

tests/tests_asyncio/test_headers.py

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -104,43 +104,6 @@ async def important_headers(
104104
assert headers.get("user-agent") not in (self.browser_type, "foobar")
105105
assert "asdf" not in headers
106106

107-
@allow_windows
108-
async def test_use_custom_headers_deprecated_arg_handling(self):
109-
"""Custom header processing function that receives deprecated args"""
110-
111-
async def deprecated_args(
112-
browser_name, pw_req, headers # pylint: disable=unused-argument
113-
) -> dict:
114-
return {"foo": "bar"}
115-
116-
settings_dict = {
117-
"PLAYWRIGHT_BROWSER_TYPE": self.browser_type,
118-
"PLAYWRIGHT_CONTEXTS": {"default": {"user_agent": self.browser_type}},
119-
"PLAYWRIGHT_PROCESS_REQUEST_HEADERS": deprecated_args,
120-
}
121-
async with make_handler(settings_dict) as handler:
122-
with MockServer() as server:
123-
req = Request(
124-
url=server.urljoin("/headers"),
125-
meta={"playwright": True},
126-
headers={"User-Agent": "foobar", "Asdf": "qwerty"},
127-
)
128-
with warnings.catch_warnings(record=True) as warning_list:
129-
resp = await handler._download_request(req, Spider("foo"))
130-
headers = json.loads(resp.css("pre::text").get())
131-
headers = {key.lower(): value for key, value in headers.items()}
132-
assert headers["foo"] == "bar"
133-
assert headers.get("user-agent") not in (self.browser_type, "foobar")
134-
assert "asdf" not in headers
135-
assert str(warning_list[0].message) == (
136-
"Accepting positional arguments in the function passed to the"
137-
" PLAYWRIGHT_PROCESS_REQUEST_HEADERS setting is deprecated. The function"
138-
" should accept three (3) keyword arguments instead:"
139-
" browser_type_name: str,"
140-
" playwright_request: playwright.async_api.Request,"
141-
" scrapy_request_data: dict"
142-
)
143-
144107

145108
class TestProcessHeadersChromium(IsolatedAsyncioTestCase, MixinProcessHeadersTestCase):
146109
browser_type = "chromium"

0 commit comments

Comments
 (0)