mirror of https://github.com/scrapy/scrapy.git
Merge pull request #6134 from wRAR/obsolete-setter
Remove obsolete setters for body and url on Request and Response.
This commit is contained in:
commit
c31e09d709
|
|
@ -1,10 +0,0 @@
|
|||
from typing import Any, Callable, NoReturn
|
||||
|
||||
|
||||
def obsolete_setter(setter: Callable, attrname: str) -> Callable[[Any, Any], NoReturn]:
|
||||
def newsetter(self: Any, value: Any) -> NoReturn:
|
||||
c = self.__class__.__name__
|
||||
msg = f"{c}.{attrname} is not modifiable, use {c}.replace() instead"
|
||||
raise AttributeError(msg)
|
||||
|
||||
return newsetter
|
||||
|
|
@ -25,7 +25,6 @@ from typing import (
|
|||
from w3lib.url import safe_url_string
|
||||
|
||||
import scrapy
|
||||
from scrapy.http.common import obsolete_setter
|
||||
from scrapy.http.headers import Headers
|
||||
from scrapy.utils.curl import curl_to_request_kwargs
|
||||
from scrapy.utils.python import to_bytes
|
||||
|
|
@ -142,7 +141,8 @@ class Request(object_ref):
|
|||
self._meta = {}
|
||||
return self._meta
|
||||
|
||||
def _get_url(self) -> str:
|
||||
@property
|
||||
def url(self) -> str:
|
||||
return self._url
|
||||
|
||||
def _set_url(self, url: str) -> None:
|
||||
|
|
@ -159,16 +159,13 @@ class Request(object_ref):
|
|||
):
|
||||
raise ValueError(f"Missing scheme in request url: {self._url}")
|
||||
|
||||
url = property(_get_url, obsolete_setter(_set_url, "url"))
|
||||
|
||||
def _get_body(self) -> bytes:
|
||||
@property
|
||||
def body(self) -> bytes:
|
||||
return self._body
|
||||
|
||||
def _set_body(self, body: Optional[Union[str, bytes]]) -> None:
|
||||
self._body = b"" if body is None else to_bytes(body, self.encoding)
|
||||
|
||||
body = property(_get_body, obsolete_setter(_set_body, "body"))
|
||||
|
||||
@property
|
||||
def encoding(self) -> str:
|
||||
return self._encoding
|
||||
|
|
|
|||
|
|
@ -27,7 +27,6 @@ from urllib.parse import urljoin
|
|||
from twisted.internet.ssl import Certificate
|
||||
|
||||
from scrapy.exceptions import NotSupported
|
||||
from scrapy.http.common import obsolete_setter
|
||||
from scrapy.http.headers import Headers
|
||||
from scrapy.http.request import Request
|
||||
from scrapy.link import Link
|
||||
|
|
@ -102,7 +101,8 @@ class Response(object_ref):
|
|||
"is not tied to any request"
|
||||
)
|
||||
|
||||
def _get_url(self) -> str:
|
||||
@property
|
||||
def url(self) -> str:
|
||||
return self._url
|
||||
|
||||
def _set_url(self, url: str) -> None:
|
||||
|
|
@ -113,9 +113,8 @@ class Response(object_ref):
|
|||
f"{type(self).__name__} url must be str, " f"got {type(url).__name__}"
|
||||
)
|
||||
|
||||
url = property(_get_url, obsolete_setter(_set_url, "url"))
|
||||
|
||||
def _get_body(self) -> bytes:
|
||||
@property
|
||||
def body(self) -> bytes:
|
||||
return self._body
|
||||
|
||||
def _set_body(self, body: Optional[bytes]) -> None:
|
||||
|
|
@ -130,8 +129,6 @@ class Response(object_ref):
|
|||
else:
|
||||
self._body = body
|
||||
|
||||
body = property(_get_body, obsolete_setter(_set_body, "body"))
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"<{self.status} {self.url}>"
|
||||
|
||||
|
|
@ -149,7 +146,7 @@ class Response(object_ref):
|
|||
def urljoin(self, url: str) -> str:
|
||||
"""Join this Response's url with a possible relative url to form an
|
||||
absolute interpretation of the latter."""
|
||||
return urljoin(cast(str, self.url), url)
|
||||
return urljoin(self.url, url)
|
||||
|
||||
@property
|
||||
def text(self) -> str:
|
||||
|
|
|
|||
|
|
@ -217,10 +217,10 @@ def _body_or_str(
|
|||
)
|
||||
if isinstance(obj, Response):
|
||||
if not unicode:
|
||||
return cast(bytes, obj.body)
|
||||
return obj.body
|
||||
if isinstance(obj, TextResponse):
|
||||
return obj.text
|
||||
return cast(bytes, obj.body).decode("utf-8")
|
||||
return obj.body.decode("utf-8")
|
||||
if isinstance(obj, str):
|
||||
return obj if unicode else obj.encode("utf-8")
|
||||
return obj.decode("utf-8") if unicode else obj
|
||||
|
|
|
|||
Loading…
Reference in New Issue