Skip to content

Commit ce6965a

Browse files
authored
Add py.typed, small improvements (#266)
1 parent 5cfa541 commit ce6965a

File tree

13 files changed

+65
-33
lines changed

13 files changed

+65
-33
lines changed

.coveragerc

Lines changed: 0 additions & 12 deletions
This file was deleted.

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ docs/_build
99
__pycache__/
1010
/test-results/
1111
build/
12+
.idea/
13+
.vscode/

pyproject.toml

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,20 +68,50 @@ parse = "version\\s*=\\s*['\"](?P<major>\\d+)\\.(?P<minor>\\d+)\\.(?P<patch>\\d+
6868
[[tool.bumpversion.files]]
6969
filename = "scrapy_zyte_api/__version__.py"
7070

71+
[tool.coverage.run]
72+
branch = true
73+
include = [
74+
"scrapy_zyte_api/*",
75+
]
76+
omit = [
77+
"tests/*",
78+
]
79+
disable_warnings = [
80+
"include-ignored",
81+
]
82+
7183
[tool.hatch.build.targets.sdist]
7284
include = [
73-
"scrapy_zyte_api/*.py",
85+
"/docs",
86+
"/scrapy_zyte_api",
87+
"/tests",
88+
"/CHANGES.rst",
89+
"/tox.ini",
7490
]
7591

7692
[tool.mypy]
7793
check_untyped_defs = true
78-
ignore_missing_imports = true
79-
no_warn_no_return = true
94+
implicit_reexport = false
95+
warn_no_return = false
96+
warn_unused_ignores = true
8097
files = [
8198
"scrapy_zyte_api",
8299
"tests"
83100
]
84101

102+
[[tool.mypy.overrides]]
103+
module = [
104+
"pytest_twisted",
105+
"scrapy_crawlera",
106+
"scrapy_zyte_smartproxy",
107+
]
108+
ignore_missing_imports = true
109+
110+
# to be fixed
111+
[[tool.mypy.overrides]]
112+
module = "scrapy_poet.*"
113+
ignore_missing_imports = true
114+
85115
[tool.pytest.ini_options]
86116
junit_family = "xunit2"
87117
testpaths = [

scrapy_zyte_api/_request_fingerprinter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from w3lib.url import canonicalize_url
2626

2727
from ._params import _REQUEST_PARAMS, _may_use_browser, _ParamParser
28-
from .utils import _build_from_crawler
28+
from .utils import _build_from_crawler # type: ignore[attr-defined]
2929

3030
class ScrapyZyteAPIRequestFingerprinter:
3131
@classmethod

scrapy_zyte_api/_session.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from tenacity import stop_after_attempt
1919
from zyte_api import RequestError, RetryFactory
2020

21-
from .utils import _DOWNLOAD_NEEDS_SPIDER, _build_from_crawler
21+
from .utils import _DOWNLOAD_NEEDS_SPIDER, _build_from_crawler # type: ignore[attr-defined]
2222

2323
logger = getLogger(__name__)
2424
SESSION_INIT_META_KEY = "_is_session_init_request"

scrapy_zyte_api/handler.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
from ._params import _ParamParser
2121
from .responses import ZyteAPIResponse, ZyteAPITextResponse, _process_response
22-
from .utils import (
22+
from .utils import ( # type: ignore[attr-defined]
2323
_AUTOTHROTTLE_DONT_ADJUST_DELAY_SUPPORT,
2424
_X402_SUPPORT,
2525
USER_AGENT,
@@ -82,7 +82,10 @@ class _ScrapyZyteAPIBaseDownloadHandler:
8282
lazy = False
8383

8484
def __init__(
85-
self, settings: Settings, crawler: Crawler, client: AsyncZyteAPI = None
85+
self,
86+
settings: Settings,
87+
crawler: Crawler,
88+
client: Optional[AsyncZyteAPI] = None,
8689
):
8790
if not settings.getbool("ZYTE_API_ENABLED", True):
8891
raise NotConfigured(
@@ -348,7 +351,10 @@ async def _close(self) -> None: # NOQA
348351

349352
class ScrapyZyteAPIDownloadHandler(_ScrapyZyteAPIBaseDownloadHandler):
350353
def __init__(
351-
self, settings: Settings, crawler: Crawler, client: AsyncZyteAPI = None
354+
self,
355+
settings: Settings,
356+
crawler: Crawler,
357+
client: Optional[AsyncZyteAPI] = None,
352358
):
353359
super().__init__(settings, crawler, client)
354360
self._fallback_handler = self._create_handler(
@@ -358,7 +364,10 @@ def __init__(
358364

359365
class ScrapyZyteAPIHTTPDownloadHandler(_ScrapyZyteAPIBaseDownloadHandler):
360366
def __init__(
361-
self, settings: Settings, crawler: Crawler, client: AsyncZyteAPI = None
367+
self,
368+
settings: Settings,
369+
crawler: Crawler,
370+
client: Optional[AsyncZyteAPI] = None,
362371
):
363372
super().__init__(settings, crawler, client)
364373
self._fallback_handler = self._create_handler(
@@ -371,7 +380,10 @@ def __init__(
371380

372381
class ScrapyZyteAPIHTTPSDownloadHandler(_ScrapyZyteAPIBaseDownloadHandler):
373382
def __init__(
374-
self, settings: Settings, crawler: Crawler, client: AsyncZyteAPI = None
383+
self,
384+
settings: Settings,
385+
crawler: Crawler,
386+
client: Optional[AsyncZyteAPI] = None,
375387
):
376388
super().__init__(settings, crawler, client)
377389
self._fallback_handler = self._create_handler(

scrapy_zyte_api/providers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ async def __call__( # noqa: C901
368368
if not kw:
369369
continue
370370
assert issubclass(cls_stripped, Item)
371-
result = cls_stripped.from_dict(api_response.raw_api_response[kw]) # type: ignore[attr-defined]
371+
result = cls_stripped.from_dict(api_response.raw_api_response[kw])
372372
if is_typing_annotated(cls):
373373
result = AnnotatedInstance(result, cls.__metadata__) # type: ignore[attr-defined]
374374
results.append(result)

scrapy_zyte_api/py.typed

Whitespace-only changes.

tests/mockserver.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from __future__ import annotations
12
import argparse
23
import json
34
import socket
@@ -18,7 +19,7 @@
1819
from twisted.web.resource import Resource
1920
from twisted.web.server import NOT_DONE_YET, Site
2021

21-
from scrapy_zyte_api._annotations import _ActionResult
22+
from scrapy_zyte_api._annotations import _ActionResult, ExtractFrom
2223
from scrapy_zyte_api.responses import _API_RESPONSE
2324

2425
from . import SETTINGS, make_handler
@@ -242,8 +243,6 @@ def render_POST(self, request):
242243
assert isinstance(response_data["product"]["name"], str)
243244
extract_from = request_data.get("productOptions", {}).get("extractFrom")
244245
if extract_from:
245-
from scrapy_zyte_api.providers import ExtractFrom
246-
247246
if extract_from == ExtractFrom.httpResponseBody:
248247
response_data["product"]["name"] += " (from httpResponseBody)"
249248

tests/test_api_requests.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
from scrapy.settings.default_settings import USER_AGENT as DEFAULT_USER_AGENT
2222
from scrapy.utils.defer import deferred_f_from_coro_f
2323
from twisted.internet.defer import Deferred
24-
from zyte_api.aio.errors import RequestError
24+
from zyte_api import RequestError
2525

2626
from scrapy_zyte_api._cookies import _get_cookie_jar
27-
from scrapy_zyte_api._params import ANY_VALUE
28-
from scrapy_zyte_api.handler import _ParamParser, _ScrapyZyteAPIBaseDownloadHandler
27+
from scrapy_zyte_api._params import ANY_VALUE, _ParamParser
28+
from scrapy_zyte_api.handler import _ScrapyZyteAPIBaseDownloadHandler
2929
from scrapy_zyte_api.responses import _process_response
3030
from scrapy_zyte_api.utils import _ADDON_SUPPORT
3131

0 commit comments

Comments
 (0)