Skip to content

Commit 4a8911c

Browse files
committed
Pylint check
1 parent fe9244a commit 4a8911c

File tree

9 files changed

+63
-28
lines changed

9 files changed

+63
-28
lines changed

.github/workflows/checks.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ jobs:
2121
- python-version: 3.8
2222
env:
2323
TOXENV: typing
24+
- python-version: 3.8
25+
env:
26+
TOXENV: pylint
2427

2528
steps:
2629
- uses: actions/checkout@v2

pylintrc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
[MESSAGES CONTROL]
2+
disable=
3+
attribute-defined-outside-init,
4+
import-error,
5+
invalid-name,
6+
logging-fstring-interpolation,
7+
missing-class-docstring,
8+
missing-function-docstring,
9+
missing-module-docstring,
10+
too-few-public-methods,
11+
too-many-instance-attributes,
12+
too-many-locals,
13+
unused-argument,
14+
# tests
15+
abstract-method,
16+
bare-except,
17+
duplicate-code,
18+
import-outside-toplevel,
19+
inconsistent-return-statements,
20+
protected-access,
21+
22+
23+
[FORMAT]
24+
expected-line-ending-format=LF
25+
max-line-length=99

scrapy_playwright/handler.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ def __init__(self, crawler: Crawler) -> None:
106106
self.context_kwargs[name].update(kwargs)
107107
if "default" not in self.context_kwargs and default_context_kwargs:
108108
self.context_kwargs["default"] = default_context_kwargs
109-
self.contexts: Dict[str, BrowserContext] = dict()
110-
self.context_semaphores: Dict[str, asyncio.Semaphore] = dict()
109+
self.contexts: Dict[str, BrowserContext] = {}
110+
self.context_semaphores: Dict[str, asyncio.Semaphore] = {}
111111

112112
@classmethod
113113
def from_crawler(cls: Type[PlaywrightHandler], crawler: Crawler) -> PlaywrightHandler:
@@ -132,8 +132,7 @@ async def _launch_browser(self) -> None:
132132
)
133133
self.contexts = dict(zip(self.context_kwargs.keys(), contexts))
134134
self.context_semaphores = {
135-
name: asyncio.Semaphore(value=self.max_pages_per_context)
136-
for name in self.contexts.keys()
135+
name: asyncio.Semaphore(value=self.max_pages_per_context) for name in self.contexts
137136
}
138137

139138
async def _create_browser_context(self, name: str, context_kwargs: dict) -> BrowserContext:

scrapy_playwright/headers.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
from urllib.parse import urlparse
2-
3-
from playwright.async_api import Request as PlaywrightRequest
4-
from scrapy.http.headers import Headers
5-
6-
71
"""
82
This module includes functions to process request headers.
93
Refer to the PLAYWRIGHT_PROCESS_REQUEST_HEADERS setting for more information.
104
"""
115

6+
from urllib.parse import urlparse
7+
8+
from playwright.async_api import Request as PlaywrightRequest
9+
from scrapy.http.headers import Headers
10+
1211

1312
async def use_scrapy_headers(
1413
browser_type: str,
@@ -28,11 +27,11 @@ async def use_scrapy_headers(
2827
# otherwise this fails with playwright.helper.Error: NS_ERROR_NET_RESET
2928
headers["host"] = urlparse(playwright_request.url).netloc
3029
return headers
31-
else:
32-
# override user agent, for consistency with other requests
33-
if headers.get("user-agent"):
34-
playwright_request.headers["user-agent"] = headers["user-agent"]
35-
return playwright_request.headers
30+
31+
# override user agent, for consistency with other requests
32+
if headers.get("user-agent"):
33+
playwright_request.headers["user-agent"] = headers["user-agent"]
34+
return playwright_request.headers
3635

3736

3837
async def use_playwright_headers(

scrapy_playwright/page.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ def __init__(self, method: str, *args, **kwargs) -> None:
1111
self.result = None
1212

1313
def __str__(self):
14-
return "<%s for method '%s'>" % (self.__class__.__name__, self.method)
14+
return f"<{self.__class__.__name__} for method '{self.method}'>"
1515

1616
__repr__ = __str__

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from scrapy_playwright import __version__
44

55

6-
with open("README.md", "r") as fh:
6+
with open("README.md", "r", encoding="utf-8") as fh:
77
long_description = fh.read()
88

99

tests/mockserver.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def __exit__(self, exc_type, exc_value, traceback):
2727
self.proc.communicate()
2828

2929
def urljoin(self, url):
30-
return urljoin("http://{}:{}".format(self.address, self.port), url)
30+
return urljoin(f"http://{self.address}:{self.port}", url)
3131

3232

3333
class _RequestHandler(BaseHTTPRequestHandler):
@@ -70,7 +70,7 @@ def __exit__(self, exc_type, exc_value, traceback):
7070
self.thread.join()
7171

7272
def urljoin(self, url: str) -> str:
73-
return urljoin("http://{}:{}".format(self.address, self.port), url)
73+
return urljoin(f"http://{self.address}:{self.port}", url)
7474

7575

7676
if __name__ == "__main__":

tests/test_playwright_requests.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
from tempfile import NamedTemporaryFile
77

88
import pytest
9-
from playwright.async_api import Dialog, Page as PlaywrightPage, TimeoutError
9+
from playwright.async_api import (
10+
Dialog,
11+
Page as PlaywrightPage,
12+
TimeoutError as PlaywrightTimeoutError,
13+
)
1014
from scrapy import Spider, Request, FormRequest
1115
from scrapy.http.response.html import HtmlResponse
1216

@@ -21,6 +25,7 @@ def get_mimetype(file):
2125
["file", "--mime-type", "--brief", file.name],
2226
stdout=subprocess.PIPE,
2327
universal_newlines=True,
28+
check=False,
2429
).stdout.strip()
2530

2631

@@ -162,7 +167,7 @@ async def test_timeout(self):
162167
async with make_handler(settings_dict) as handler:
163168
with MockServer() as server:
164169
req = Request(server.urljoin("/delay/2"), meta={"playwright": True})
165-
with pytest.raises(TimeoutError):
170+
with pytest.raises(PlaywrightTimeoutError):
166171
await handler._download_request(req, Spider("foo"))
167172

168173
@pytest.mark.asyncio
@@ -184,7 +189,7 @@ async def test_context_kwargs(self):
184189
],
185190
},
186191
)
187-
with pytest.raises(TimeoutError):
192+
with pytest.raises(PlaywrightTimeoutError):
188193
await handler._download_request(req, Spider("foo"))
189194

190195
@pytest.mark.asyncio
@@ -263,10 +268,12 @@ async def test_user_agent(self):
263268
@pytest.mark.asyncio
264269
async def test_use_playwright_headers(self):
265270
"""Ignore Scrapy headers"""
271+
from scrapy_playwright.headers import use_playwright_headers
272+
266273
settings_dict = {
267274
"PLAYWRIGHT_BROWSER_TYPE": self.browser_type,
268275
"PLAYWRIGHT_CONTEXTS": {"default": {"user_agent": self.browser_type}},
269-
"PLAYWRIGHT_PROCESS_REQUEST_HEADERS": "scrapy_playwright.headers.use_playwright_headers", # noqa: E501
276+
"PLAYWRIGHT_PROCESS_REQUEST_HEADERS": use_playwright_headers,
270277
}
271278
async with make_handler(settings_dict) as handler:
272279
with MockServer() as server:

tox.ini

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[tox]
2-
envlist = bandit,black,flake8,typing,py
2+
envlist = bandit,black,flake8,typing,pylint,py
33

44
[testenv]
55
deps =
@@ -21,30 +21,32 @@ setenv =
2121
basepython = python3
2222

2323
[testenv:bandit]
24-
basepython = python3
2524
deps =
2625
bandit
2726
commands =
2827
bandit -r {posargs: scrapy_playwright setup.py examples}
2928

3029
[testenv:black]
31-
basepython = python3.8
3230
deps =
3331
black==20.8b1
3432
commands =
3533
black --check {posargs: scrapy_playwright setup.py tests examples}
3634

3735
[testenv:flake8]
38-
basepython = python3.8
3936
deps =
4037
flake8>=3.7.9
4138
commands =
4239
flake8 --exclude=.git,.tox,venv* {posargs: scrapy_playwright setup.py tests examples}
4340

4441
[testenv:typing]
45-
basepython = python3.8
4642
deps =
4743
mypy==0.790
4844
commands =
4945
mypy --show-error-codes --ignore-missing-imports \
5046
--follow-imports=skip {posargs: scrapy_playwright setup.py tests examples}
47+
48+
[testenv:pylint]
49+
deps =
50+
pylint==2.12.2
51+
commands =
52+
pylint {posargs: scrapy_playwright setup.py tests}

0 commit comments

Comments
 (0)