scrapy / scrapy

@@ -4,7 +4,7 @@
Loading
4 4
5 5
See documentation in docs/topics/request-response.rst
6 6
"""
7 -
from typing import Generator
7 +
from typing import Generator, Tuple
8 8
from urllib.parse import urljoin
9 9
10 10
from scrapy.exceptions import NotSupported
@@ -16,6 +16,19 @@
Loading
16 16
17 17
18 18
class Response(object_ref):
19 +
    """An object that represents an HTTP response, which is usually
20 +
    downloaded (by the Downloader) and fed to the Spiders for processing.
21 +
    """
22 +
23 +
    attributes: Tuple[str, ...] = (
24 +
        "url", "status", "headers", "body", "flags", "request", "certificate", "ip_address", "protocol",
25 +
    )
26 +
    """A tuple of :class:`str` objects containing the name of all public
27 +
    attributes of the class that are also keyword parameters of the
28 +
    ``__init__`` method.
29 +
30 +
    Currently used by :meth:`Response.replace`.
31 +
    """
19 32
20 33
    def __init__(
21 34
        self,
@@ -97,12 +110,8 @@
Loading
97 110
        return self.replace()
98 111
99 112
    def replace(self, *args, **kwargs):
100 -
        """Create a new Response with the same attributes except for those
101 -
        given new values.
102 -
        """
103 -
        for x in [
104 -
            "url", "status", "headers", "body", "request", "flags", "certificate", "ip_address", "protocol",
105 -
        ]:
113 +
        """Create a new Response with the same attributes except for those given new values"""
114 +
        for x in self.attributes:
106 115
            kwargs.setdefault(x, getattr(self, x))
107 116
        cls = kwargs.pop('cls', self.__class__)
108 117
        return cls(*args, **kwargs)

@@ -8,7 +8,7 @@
Loading
8 8
import json
9 9
import warnings
10 10
from contextlib import suppress
11 -
from typing import Generator
11 +
from typing import Generator, Tuple
12 12
from urllib.parse import urljoin
13 13
14 14
import parsel
@@ -30,6 +30,8 @@
Loading
30 30
    _DEFAULT_ENCODING = 'ascii'
31 31
    _cached_decoded_json = _NONE
32 32
33 +
    attributes: Tuple[str, ...] = Response.attributes + ("encoding",)
34 +
33 35
    def __init__(self, *args, **kwargs):
34 36
        self._encoding = kwargs.pop('encoding', None)
35 37
        self._cached_benc = None
@@ -53,10 +55,6 @@
Loading
53 55
        else:
54 56
            super()._set_body(body)
55 57
56 -
    def replace(self, *args, **kwargs):
57 -
        kwargs.setdefault('encoding', self.encoding)
58 -
        return Response.replace(self, *args, **kwargs)
59 -
60 58
    @property
61 59
    def encoding(self):
62 60
        return self._declared_encoding() or self._body_inferred_encoding()
Files Coverage
scrapy 88.42%
Project Totals (162 files) 88.42%
Sunburst
The inner-most circle is the entire project, moving away from the center are folders then, finally, a single file. The size and color of each slice is representing the number of statements and the coverage, respectively.
Icicle
The top section represents the entire project. Proceeding with folders and finally individual files. The size and color of each slice is representing the number of statements and the coverage, respectively.
Grid
Each block represents a single file in the project. The size and color of each block is represented by the number of statements and the coverage, respectively.
Loading