From 455002ba6374692a41d3f3fda3a6c042d8856f8a Mon Sep 17 00:00:00 2001 From: Stuart Mumford Date: Thu, 23 Jun 2022 13:59:26 +0200 Subject: [PATCH] Don't deprecate the headers kwarg (#101) Full API break in 2.0 incoming --- parfive/config.py | 24 ++------------------ parfive/downloader.py | 9 -------- parfive/tests/test_config.py | 43 +++--------------------------------- 3 files changed, 5 insertions(+), 71 deletions(-) diff --git a/parfive/config.py b/parfive/config.py index 457dc6c..ec43748 100644 --- a/parfive/config.py +++ b/parfive/config.py @@ -79,7 +79,7 @@ class SessionConfig: The URL of a proxy to use for HTTPS requests. Will default to the value of the ``HTTPS_PROXY`` env var. """ - headers: Optional[Dict[str, str]] = None + headers: Optional[Dict[str, str]] = field(default_factory=_default_headers) """ Headers to be passed to all requests made by this session. These headers are passed to the `aiohttp.ClientSession` along with @@ -187,17 +187,10 @@ class DownloaderConfig: max_splits: int = 5 progress: bool = True overwrite: Union[bool, Literal["unique"]] = False - # headers is deprecated here. - # The arguments passed to SessionConfig take precedence. - # To make this priority work, the defaults on SessionConfig - # are that these two arguments default to None. - # When it is removed after the deprecation period, the defaults here - # should be moved to SessionConifg - headers: InitVar[Optional[Dict[str, str]]] = None config: Optional[SessionConfig] = field(default_factory=SessionConfig) env: EnvConfig = field(default_factory=EnvConfig) - def __post_init__(self, headers): + def __post_init__(self): if self.config is None: self.config = SessionConfig() @@ -205,24 +198,11 @@ def __post_init__(self, headers): self.max_splits = 1 if self.env.serial_mode or self.env.disable_range else self.max_splits self.progress = False if self.env.hide_progress else self.progress - # Default headers if None - if self.config.headers is None: - if headers is None: - self.config.headers = _default_headers() - elif headers is not None: - self.config.headers = headers - if self.progress is False: self.file_progress = False def __getattr__(self, __name: str): return getattr(self.config, __name) - # Always delegate headers to config even though we have that attribute - def __getattribute__(self, __name): - if __name == "headers": - return self.config.headers - return super().__getattribute__(__name) - def aiohttp_client_session(self): return self.config.aiohttp_session_generator(self.config) diff --git a/parfive/downloader.py b/parfive/downloader.py index 4a8b5a3..e354c68 100644 --- a/parfive/downloader.py +++ b/parfive/downloader.py @@ -76,23 +76,14 @@ def __init__( max_splits: int = 5, progress: bool = True, overwrite: Union[bool, Literal["unique"]] = False, - headers: Optional[Dict[str, str]] = None, config: SessionConfig = None, ): - msg = ( - "The {} keyword argument to Downloader is deprecated. " - "Please instead pass a SessionConfig object to the config keyword argument." - ) - if headers is not None: - warnings.warn(msg.format("headers"), ParfiveFutureWarning) - self.config = DownloaderConfig( max_conn=max_conn, max_splits=max_splits, progress=progress, overwrite=overwrite, - headers=headers, config=config, ) diff --git a/parfive/tests/test_config.py b/parfive/tests/test_config.py index b953001..2d64e5b 100644 --- a/parfive/tests/test_config.py +++ b/parfive/tests/test_config.py @@ -19,10 +19,9 @@ def test_session_config_defaults(): assert c.chunksize == 1024 assert c.use_aiofiles is False - # Deprecated behaviour - assert c.headers is None - # assert isinstance(c.headers, dict) - # assert "User-Agent" in c.headers + assert isinstance(c.headers, dict) + assert "User-Agent" in c.headers + assert "parfive" in c.headers["User-Agent"] def test_session_config_env_defaults(): @@ -34,42 +33,6 @@ def test_session_config_env_defaults(): assert c.env.timeout_sock_read == 90 -def test_headers_deprecated(): - c = DownloaderConfig() - assert isinstance(c.headers, dict) - assert len(c.headers) == 1 - assert "User-Agent" in c.headers - assert "parfive" in c.headers["User-Agent"] - - c = DownloaderConfig(headers=None) - assert isinstance(c.headers, dict) - assert len(c.headers) == 1 - assert "User-Agent" in c.headers - assert "parfive" in c.headers["User-Agent"] - - test_headers = {"spam": "eggs"} - - c = DownloaderConfig(headers=test_headers) - assert c.headers == test_headers - - c = DownloaderConfig(headers={"ordinary": "rabbit"}, config=SessionConfig(headers=test_headers)) - assert c.headers == test_headers - - c = DownloaderConfig(config=SessionConfig(headers=test_headers)) - assert c.headers == test_headers - - # This test should really be on the SessionConfig object - # but because of the deprecation logic it has to be done here. - c = DownloaderConfig(headers={}) - assert not c.headers - - -def test_deprecated_downloader_arguments(): - with pytest.warns(ParfiveFutureWarning, match="headers keyword"): - d = Downloader(headers="ni") - assert d.config.headers == "ni" - - def test_ssl_context(): # Assert that the unpickalable SSL context object doesn't anger the # dataclass gods