Skip to content

Scrapy, a fast high-level web crawling & scraping framework for Python.

Add mypy tests.

Andrey Rakhmatullin5d55e4f56b77
diff --git a/tests_typing/test_http_request.mypy-testing b/tests_typing/test_http_request.mypy-testing
new file mode 100644
index 000000000..a306b15fe
--- /dev/null
+++ b/tests_typing/test_http_request.mypy-testing
@@ -0,0 +1,66 @@
+import pytest
+
+from scrapy import Request
+from scrapy.http import JsonRequest
+
+
+class MyRequest(Request):
+ pass
+
+
+class MyRequest2(Request):
+ pass
+
+
+def mypy_test_headers():
+ Request("data:,", headers=1) # E: Argument "headers" to "Request" has incompatible type "int"; expected "Mapping[str, Any] | Iterable[tuple[str, Any]] | None"
+ Request("data:,", headers=None)
+ Request("data:,", headers={})
+ Request("data:,", headers=[])
+ Request("data:,", headers={"foo": "bar"})
+ Request("data:,", headers={b"foo": "bar"})
+ Request("data:,", headers={"foo": b"bar"})
+ Request("data:,", headers=[("foo", "bar")])
+ Request("data:,", headers=[(b"foo", "bar")])
+ Request("data:,", headers=[("foo", b"bar")])
+
+
+def mypy_test_copy():
+ req = Request("data:,")
+ reveal_type(req) # R: scrapy.http.request.Request
+ req_copy = req.copy()
+ reveal_type(req_copy) # R: scrapy.http.request.Request
+
+ req = MyRequest("data:,")
+ reveal_type(req) # R: __main__.MyRequest
+ req_copy = req.copy()
+ reveal_type(req_copy) # R: __main__.MyRequest
+
+
+def mypy_test_replace():
+ req = Request("data:,")
+ reveal_type(req) # R: scrapy.http.request.Request
+ req_copy = req.replace(body=b"a")
+ reveal_type(req_copy) # R: scrapy.http.request.Request
+
+ req = MyRequest("data:,")
+ reveal_type(req) # R: __main__.MyRequest
+ req_copy = req.replace(body=b"a")
+ reveal_type(req_copy) # R: __main__.MyRequest
+ req_copy2 = req.replace(body=b"a", cls=MyRequest2)
+ reveal_type(req_copy2) # R: __main__.MyRequest2
+
+
+def mypy_test_jsonrequest_copy_replace():
+ req = JsonRequest("data:,")
+ reveal_type(req) # R: scrapy.http.request.json_request.JsonRequest
+ req_copy = req.copy()
+ reveal_type(req_copy) # R: scrapy.http.request.json_request.JsonRequest
+ req_copy = req.replace(body=b"a")
+ reveal_type(req_copy) # R: scrapy.http.request.json_request.JsonRequest
+ req_copy_my = req.replace(body=b"a", cls=MyRequest)
+ reveal_type(req_copy_my) # R: __main__.MyRequest
diff --git a/tests_typing/test_http_response.mypy-testing b/tests_typing/test_http_response.mypy-testing
new file mode 100644
index 000000000..66ac6ad1d
--- /dev/null
+++ b/tests_typing/test_http_response.mypy-testing
@@ -0,0 +1,45 @@
+import pytest
+
+from scrapy.http import HtmlResponse, Response, TextResponse
+
+
+def mypy_test_headers():
+ Response("data:,", headers=1) # E: Argument "headers" to "Response" has incompatible type "int"; expected "Mapping[str, Any] | Iterable[tuple[str, Any]] | None"
+ Response("data:,", headers=None)
+ Response("data:,", headers={})
+ Response("data:,", headers=[])
+ Response("data:,", headers={"foo": "bar"})
+ Response("data:,", headers={b"foo": "bar"})
+ Response("data:,", headers={"foo": b"bar"})
+ Response("data:,", headers=[("foo", "bar")])
+ Response("data:,", headers=[(b"foo", "bar")])
+ Response("data:,", headers=[("foo", b"bar")])
+
+
+def mypy_test_copy():
+ resp = Response("data:,")
+ reveal_type(resp) # R: scrapy.http.response.Response
+ resp_copy = resp.copy()
+ reveal_type(resp_copy) # R: scrapy.http.response.Response
+
+ resp = HtmlResponse("data:,")
+ reveal_type(resp) # R: scrapy.http.response.html.HtmlResponse
+ resp_copy = resp.copy()
+ reveal_type(resp_copy) # R: scrapy.http.response.html.HtmlResponse
+
+
+def mypy_test_replace():
+ resp = Response("data:,")
+ reveal_type(resp) # R: scrapy.http.response.Response
+ resp_copy = resp.replace(body=b"a")
+ reveal_type(resp_copy) # R: scrapy.http.response.Response
+
+ resp = HtmlResponse("data:,")
+ reveal_type(resp) # R: scrapy.http.response.html.HtmlResponse
+ resp_copy = resp.replace(body=b"a")
+ reveal_type(resp_copy) # R: scrapy.http.response.html.HtmlResponse
+ resp_copy2 = resp.replace(body=b"a", cls=TextResponse)
+ reveal_type(resp_copy2) # R: scrapy.http.response.text.TextResponse
diff --git a/tox.ini b/tox.ini
index 932c0b805..c3fa54339 100644
--- a/tox.ini
+++ b/tox.ini
@@ -46,6 +46,14 @@ deps =
commands =
mypy {posargs: scrapy tests}
+[testenv:typing-tests]
+deps =
+ {[testenv]deps}
+ {[testenv:typing]deps}
+ pytest-mypy-testing==0.1.1
+commands =
+ pytest {posargs: tests_typing}
+
[testenv:pre-commit]
basepython = python3
deps =