Skip to content

Commit

Permalink
Don't deprecate the headers kwarg (#101)
Browse files Browse the repository at this point in the history
Full API break in 2.0 incoming
  • Loading branch information
Cadair committed Jun 23, 2022
1 parent b8d4144 commit 455002b
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 71 deletions.
24 changes: 2 additions & 22 deletions parfive/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -187,42 +187,22 @@ 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()

self.max_conn = 1 if self.env.serial_mode else self.max_conn
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)
9 changes: 0 additions & 9 deletions parfive/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

Expand Down
43 changes: 3 additions & 40 deletions parfive/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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
Expand Down

0 comments on commit 455002b

Please sign in to comment.