File: test_http_request.mypy-testing

package info (click to toggle)
python-scrapy 2.13.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,664 kB
  • sloc: python: 52,028; xml: 199; makefile: 25; sh: 7
file content (80 lines) | stat: -rw-r--r-- 2,565 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from typing import Any, Dict

import pytest

from scrapy import Request
from scrapy.http import JsonRequest


class MyRequest(Request):
    pass


class MyRequest2(Request):
    pass


@pytest.mark.mypy_testing
def mypy_test_headers():
    Request("data:,", headers=1)  # E: Argument "headers" to "Request" has incompatible type "int"; expected "Union[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")])


@pytest.mark.mypy_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


@pytest.mark.mypy_testing
def mypy_test_copy_subclass():
    req = MyRequest("data:,")
    reveal_type(req)  # R: __main__.MyRequest
    req_copy = req.copy()
    reveal_type(req_copy)  # R: __main__.MyRequest


@pytest.mark.mypy_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
    kwargs: Dict[str, Any] = {}
    req_copy2 = req.replace(body=b"a", **kwargs)
    reveal_type(req_copy2)  # R: Any


@pytest.mark.mypy_testing
def mypy_test_replace_subclass():
    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
    kwargs: Dict[str, Any] = {}
    req_copy3 = req.replace(body=b"a", cls=MyRequest2, **kwargs)
    reveal_type(req_copy3)  # R: __main__.MyRequest2


@pytest.mark.mypy_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__.MyRequest