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-testingnew file mode 100644index 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++[email protected]_testing+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")])++[email protected]_testing+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++[email protected]_testing+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++[email protected]_testing+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__.MyRequestdiff --git a/tests_typing/test_http_response.mypy-testing b/tests_typing/test_http_response.mypy-testingnew file mode 100644index 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++[email protected]_testing+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")])++[email protected]_testing+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++[email protected]_testing+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.TextResponsediff --git a/tox.ini b/tox.iniindex 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 = python3deps =