Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ Unreleased
**Changed**

- Improved exception message when ``asyncio.TimeoutError`` is raised.
- Fixed type hinting for ``data`` and ``params`` in ``Requestor.request()``.
- Pass ``files`` to ``aiohttp.ClientSession.request()`` in ``Requestor.request()``
correctly again.

3.0.2 (2025/08/06)
------------------
Expand Down
55 changes: 18 additions & 37 deletions asyncprawcore/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,14 @@ def _log_request(
log.debug("Params: %s", pformat(params))

@staticmethod
def _preprocess_dict(data: dict[str, object]) -> dict[str, object]:
def _preprocess_dict(data: dict[str, object] | None) -> dict[str, object]:
new_data = {}
for key, value in data.items():
if isinstance(value, bool):
new_data[key] = str(value).lower()
elif value is not None:
new_data[key] = str(value) if not isinstance(value, str) else value
if data:
for key, value in data.items():
if isinstance(value, bool):
new_data[key] = str(value).lower()
elif value is not None:
new_data[key] = str(value) if not isinstance(value, str) else value
return new_data

@property
Expand Down Expand Up @@ -196,6 +197,7 @@ async def _do_retry(
self,
*,
data: list[tuple[str, object]] | None,
files: dict[str, BinaryIO | TextIO] | None,
json: dict[str, object] | None,
method: str,
params: dict[str, object],
Expand All @@ -207,6 +209,7 @@ async def _do_retry(
log.warning("Retrying due to %s: %s %s", status, method, url)
return await self._request_with_retries(
data=data,
files=files,
json=json,
method=method,
params=params,
Expand All @@ -220,6 +223,7 @@ async def _do_retry(
async def _make_request(
self,
data: list[tuple[str, object]] | None,
files: dict[str, BinaryIO | TextIO] | None,
json: dict[str, object] | None,
method: str,
params: dict[str, object],
Expand All @@ -233,6 +237,7 @@ async def _make_request(
url,
allow_redirects=False,
data=data,
files=files,
json=json,
params=params,
timeout=timeout,
Expand All @@ -248,36 +253,7 @@ async def _make_request(
)
yield response

def _preprocess_data(
self,
data: dict[str, object],
files: dict[str, BinaryIO | TextIO] | None,
) -> dict[str, object]:
"""Preprocess data and files before request.

This is to convert requests that are formatted for the ``requests`` package to
be compatible with the ``aiohttp`` package. The motivation for this is so that
``praw`` and ``asyncpraw`` can remain as similar as possible and thus making
contributions to ``asyncpraw`` simpler.

This method does the following:

- Removes keys that have a value of ``None`` from ``data``.
- Moves ``files`` into ``data``.

:param data: Dictionary, bytes, or file-like object to send in the body of the
request.
:param files: Dictionary, mapping ``filename`` to file-like object to add to
``data``.

"""
if isinstance(data, dict):
data = self._preprocess_dict(data)
if files is not None:
data.update(files)
return data

def _preprocess_params(self, params: dict[str, object]) -> dict[str, object]:
def _preprocess_params(self, params: dict[str, object] | None) -> dict[str, object]:
"""Preprocess params before request.

This is to convert requests that are formatted for the ``requests`` package to
Expand All @@ -299,6 +275,7 @@ async def _request_with_retries( # noqa: PLR0912
self,
*,
data: list[tuple[str, object]] | None,
files: dict[str, BinaryIO | TextIO] | None,
json: dict[str, object] | None,
method: str,
params: dict[str, object],
Expand All @@ -315,6 +292,7 @@ async def _request_with_retries( # noqa: PLR0912
try:
async with self._make_request(
data=data,
files=files,
json=json,
method=method,
params=params,
Expand All @@ -332,6 +310,7 @@ async def _request_with_retries( # noqa: PLR0912
if retry_status is not None and retry_strategy_state.should_retry_on_failure():
return await self._do_retry(
data=data,
files=files,
json=json,
method=method,
params=params,
Expand Down Expand Up @@ -361,6 +340,7 @@ async def _request_with_retries( # noqa: PLR0912
):
return await self._do_retry(
data=data,
files=files,
json=json,
method=method,
params=params,
Expand Down Expand Up @@ -413,7 +393,7 @@ async def request(
params = self._preprocess_params(deepcopy(params) or {})
params["raw_json"] = "1"
if isinstance(data, dict):
data = self._preprocess_data(deepcopy(data), files)
data = self._preprocess_dict(deepcopy(data))
data["api_type"] = "json"
data_list = sorted(data.items())
else:
Expand All @@ -424,6 +404,7 @@ async def request(
url = urljoin(self._requestor.oauth_url, path)
return await self._request_with_retries(
data=data_list,
files=files,
json=json,
method=method,
params=params,
Expand Down
12 changes: 5 additions & 7 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,16 @@ lint = [
test = [
"aiohttp <4",
"coverage>=7.6.10",
"pyright",
"pytest>=8.3.4",
"pytest-asyncio ==0.18.*",
"pytest-vcr ==1.*",
"urllib3 ==1.*",
"vcrpy ==4.2.1"
"pytest-asyncio==1.2.*",
"pytest-vcr==1.*",
"vcrpy==7.0.0"
]
type = [
"aiohttp <4",
"aiohttp<4",
"pyright>=1.1.393",
"pytest>=8.3.4",
"pytest-asyncio ==0.18.*"
"pytest-asyncio==1.2.*"
]

[project]
Expand Down
51 changes: 27 additions & 24 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading