Skip to content

Commit d209e61

Browse files
authored
Merge pull request #128 from praw-dev/update-dependencies
Update dependencies and pass files to aiohttp correctly again
2 parents 0787499 + 88b9e51 commit d209e61

File tree

4 files changed

+53
-68
lines changed

4 files changed

+53
-68
lines changed

CHANGES.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ Unreleased
99
**Changed**
1010

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

1316
3.0.2 (2025/08/06)
1417
------------------

asyncprawcore/sessions.py

Lines changed: 18 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,14 @@ def _log_request(
153153
log.debug("Params: %s", pformat(params))
154154

155155
@staticmethod
156-
def _preprocess_dict(data: dict[str, object]) -> dict[str, object]:
156+
def _preprocess_dict(data: dict[str, object] | None) -> dict[str, object]:
157157
new_data = {}
158-
for key, value in data.items():
159-
if isinstance(value, bool):
160-
new_data[key] = str(value).lower()
161-
elif value is not None:
162-
new_data[key] = str(value) if not isinstance(value, str) else value
158+
if data:
159+
for key, value in data.items():
160+
if isinstance(value, bool):
161+
new_data[key] = str(value).lower()
162+
elif value is not None:
163+
new_data[key] = str(value) if not isinstance(value, str) else value
163164
return new_data
164165

165166
@property
@@ -196,6 +197,7 @@ async def _do_retry(
196197
self,
197198
*,
198199
data: list[tuple[str, object]] | None,
200+
files: dict[str, BinaryIO | TextIO] | None,
199201
json: dict[str, object] | None,
200202
method: str,
201203
params: dict[str, object],
@@ -207,6 +209,7 @@ async def _do_retry(
207209
log.warning("Retrying due to %s: %s %s", status, method, url)
208210
return await self._request_with_retries(
209211
data=data,
212+
files=files,
210213
json=json,
211214
method=method,
212215
params=params,
@@ -220,6 +223,7 @@ async def _do_retry(
220223
async def _make_request(
221224
self,
222225
data: list[tuple[str, object]] | None,
226+
files: dict[str, BinaryIO | TextIO] | None,
223227
json: dict[str, object] | None,
224228
method: str,
225229
params: dict[str, object],
@@ -233,6 +237,7 @@ async def _make_request(
233237
url,
234238
allow_redirects=False,
235239
data=data,
240+
files=files,
236241
json=json,
237242
params=params,
238243
timeout=timeout,
@@ -248,36 +253,7 @@ async def _make_request(
248253
)
249254
yield response
250255

251-
def _preprocess_data(
252-
self,
253-
data: dict[str, object],
254-
files: dict[str, BinaryIO | TextIO] | None,
255-
) -> dict[str, object]:
256-
"""Preprocess data and files before request.
257-
258-
This is to convert requests that are formatted for the ``requests`` package to
259-
be compatible with the ``aiohttp`` package. The motivation for this is so that
260-
``praw`` and ``asyncpraw`` can remain as similar as possible and thus making
261-
contributions to ``asyncpraw`` simpler.
262-
263-
This method does the following:
264-
265-
- Removes keys that have a value of ``None`` from ``data``.
266-
- Moves ``files`` into ``data``.
267-
268-
:param data: Dictionary, bytes, or file-like object to send in the body of the
269-
request.
270-
:param files: Dictionary, mapping ``filename`` to file-like object to add to
271-
``data``.
272-
273-
"""
274-
if isinstance(data, dict):
275-
data = self._preprocess_dict(data)
276-
if files is not None:
277-
data.update(files)
278-
return data
279-
280-
def _preprocess_params(self, params: dict[str, object]) -> dict[str, object]:
256+
def _preprocess_params(self, params: dict[str, object] | None) -> dict[str, object]:
281257
"""Preprocess params before request.
282258
283259
This is to convert requests that are formatted for the ``requests`` package to
@@ -299,6 +275,7 @@ async def _request_with_retries( # noqa: PLR0912
299275
self,
300276
*,
301277
data: list[tuple[str, object]] | None,
278+
files: dict[str, BinaryIO | TextIO] | None,
302279
json: dict[str, object] | None,
303280
method: str,
304281
params: dict[str, object],
@@ -315,6 +292,7 @@ async def _request_with_retries( # noqa: PLR0912
315292
try:
316293
async with self._make_request(
317294
data=data,
295+
files=files,
318296
json=json,
319297
method=method,
320298
params=params,
@@ -332,6 +310,7 @@ async def _request_with_retries( # noqa: PLR0912
332310
if retry_status is not None and retry_strategy_state.should_retry_on_failure():
333311
return await self._do_retry(
334312
data=data,
313+
files=files,
335314
json=json,
336315
method=method,
337316
params=params,
@@ -361,6 +340,7 @@ async def _request_with_retries( # noqa: PLR0912
361340
):
362341
return await self._do_retry(
363342
data=data,
343+
files=files,
364344
json=json,
365345
method=method,
366346
params=params,
@@ -413,7 +393,7 @@ async def request(
413393
params = self._preprocess_params(deepcopy(params) or {})
414394
params["raw_json"] = "1"
415395
if isinstance(data, dict):
416-
data = self._preprocess_data(deepcopy(data), files)
396+
data = self._preprocess_dict(deepcopy(data))
417397
data["api_type"] = "json"
418398
data_list = sorted(data.items())
419399
else:
@@ -424,6 +404,7 @@ async def request(
424404
url = urljoin(self._requestor.oauth_url, path)
425405
return await self._request_with_retries(
426406
data=data_list,
407+
files=files,
427408
json=json,
428409
method=method,
429410
params=params,

pyproject.toml

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,16 @@ lint = [
1313
test = [
1414
"aiohttp <4",
1515
"coverage>=7.6.10",
16-
"pyright",
1716
"pytest>=8.3.4",
18-
"pytest-asyncio ==0.18.*",
19-
"pytest-vcr ==1.*",
20-
"urllib3 ==1.*",
21-
"vcrpy ==4.2.1"
17+
"pytest-asyncio==1.2.*",
18+
"pytest-vcr==1.*",
19+
"vcrpy==7.0.0"
2220
]
2321
type = [
24-
"aiohttp <4",
22+
"aiohttp<4",
2523
"pyright>=1.1.393",
2624
"pytest>=8.3.4",
27-
"pytest-asyncio ==0.18.*"
25+
"pytest-asyncio==1.2.*"
2826
]
2927

3028
[project]

uv.lock

Lines changed: 27 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)