Skip to content

Conversation

@tsimoshka
Copy link

@tsimoshka tsimoshka commented Oct 28, 2025

Summary

This PR resolves #1620 and implements chunked upload for anyio.open_file, trio.open_file and aiofiles.open_file when used as content= parameter for post and put request. And implements support for multipart file upload for the same libraries. Most of the code for multipart file upload copied from my old stale PR #3339.

changes:

  • _compat.py file added to define TypeIs and aclosing for the range of supported python versions
  • _types.AsyncReadableFile protocol was added along with is_async_readable_file type predicate function to detect and perform type narrowing for trio/asyncio/aiofiles async files
  • _types.FileContent was extended to include the. _types.AsyncReadableFile protocol
  • _content.AsyncIteratorByteStream updated to use async read methods for async files instead of looping over lines
  • _content.encode_content updated to attach content length header for _types.AsyncReadableFile type
  • _multipart.FileField updated to include async versions of render and render_data
  • _multipart.MultipartStream updated with async version of iter_data and __aiter__ method updated to use it
  • test_chunked_async_file_multipart and test_chunked_async_file_content tests are added to test the new functionality
  • aiofiles and types-aiofiles added in requirements to test aiofiles (can be removed with test if aiofiles support is not desirable)

Checklist

  • I understand that this PR may be closed in case there was no previous discussion. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly. (can update if PR is ok otherwise)

@tsimoshka tsimoshka changed the title support chunked upload in async file-like interfaces WIP: support chunked upload in async file-like interfaces Oct 28, 2025
@tsimoshka tsimoshka marked this pull request as draft October 28, 2025 13:15
@tsimoshka tsimoshka changed the title WIP: support chunked upload in async file-like interfaces support chunked upload in async file-like interfaces Oct 28, 2025
@runtime_checkable doesn't work with trio async file since python3.12
because hasattr in isinstance was replaced with
`inspect.getattr_static()`
@tsimoshka tsimoshka marked this pull request as ready for review October 28, 2025 13:57
@tsimoshka tsimoshka marked this pull request as draft October 28, 2025 14:46
@tsimoshka tsimoshka marked this pull request as ready for review October 28, 2025 16:43
@tsimoshka tsimoshka marked this pull request as draft October 29, 2025 08:39
@tsimoshka tsimoshka marked this pull request as ready for review October 29, 2025 11:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support async file types in files = {} and content = ...

1 participant