Skip to content

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

Merge branch 'master' into request-types

Eugenio Lacuestac92e8ad0a556
diff --cc scrapy/http/request/__init__.py
index 3cce9f501,ad884feac..8f00c20b7
--- a/scrapy/http/request/__init__.py
+++ b/scrapy/http/request/__init__.py
@@@ -4,7 -4,8 +4,8 @@@ requests in Scrapy
See documentation in docs/topics/request-response.rst
"""
- from typing import Callable, List, Optional, Type, TypeVar, Union
+ import inspect
-from typing import Optional, Tuple
++from typing import Callable, List, Optional, Tuple, Type, TypeVar, Union
from w3lib.url import safe_url_string
@@@ -16,26 -18,28 +18,43 @@@ from scrapy.utils.trackref import objec
from scrapy.utils.url import escape_ajax
+RequestTypeVar = TypeVar("RequestTypeVar", bound="Request")
+
+
class Request(object_ref):
+ """Represents an HTTP request, which is usually generated in a Spider and
+ executed by the Downloader, thus generating a :class:`Response`.
+ """
+
+ attributes: Tuple[str, ...] = (
+ "url", "callback", "method", "headers", "body",
+ "cookies", "meta", "encoding", "priority",
+ "dont_filter", "errback", "flags", "cb_kwargs",
+ )
+ """A tuple of :class:`str` objects containing the name of all public
+ attributes of the class that are also keyword parameters of the
+ ``__init__`` method.
+
+ Currently used by :meth:`Request.replace`, :meth:`Request.to_dict` and
+ :func:`~scrapy.utils.request.request_from_dict`.
+ """
+
- def __init__(self, url, callback=None, method='GET', headers=None, body=None,
- cookies=None, meta=None, encoding='utf-8', priority=0,
- dont_filter=False, errback=None, flags=None, cb_kwargs=None):
-
+ def __init__(
+ self,
+ url: str,
+ callback: Optional[Callable] = None,
+ method: str = "GET",
+ headers: Optional[dict] = None,
+ body: Optional[Union[bytes, str]] = None,
- cookies: Optional[Union[dict, List[dict]]]=None,
++ cookies: Optional[Union[dict, List[dict]]] = None,
+ meta: Optional[dict] = None,
+ encoding: str = "utf-8",
+ priority: int = 0,
+ dont_filter: bool = False,
+ errback: Optional[Callable] = None,
+ flags: Optional[List[str]] = None,
+ cb_kwargs: Optional[dict] = None,
+ ) -> None:
self._encoding = encoding # this one has to be set first
self.method = str(method).upper()
self._set_url(url)
@@@ -107,13 -114,13 +126,12 @@@
__repr__ = __str__
- def copy(self):
- """Return a copy of this Request"""
+ def copy(self) -> RequestTypeVar:
return self.replace()
- def replace(self, *args, **kwargs):
+ def replace(self, *args, **kwargs) -> RequestTypeVar:
"""Create a new Request with the same attributes except for those given new values"""
- for x in ['url', 'method', 'headers', 'body', 'cookies', 'meta', 'flags',
- 'encoding', 'priority', 'dont_filter', 'callback', 'errback', 'cb_kwargs']:
+ for x in self.attributes:
kwargs.setdefault(x, getattr(self, x))
cls = kwargs.pop('cls', self.__class__)
return cls(*args, **kwargs)