openapi-generators / openapi-generators/openapi-python-client

Missing typehint of File payload for async requests

Đang mở
#1,239 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Ngôn ngữ chính
Python
Star
2k
Fork
293
Merge trung bình
34 phút
Pull request đã merge (30 ngày)
1

Mô tả

Describe the bug

LOW PRIORITY

If an API provides an "upload_file" endpoint, when using async, one has to use an asynchronous way to open a file.

Using anyio from httpx, it would require an "AsyncFile" object:
https://github.com/agronholm/anyio/blob/2a63fd0bef1575e583c4cf63cebe7d76a218c83e/src/anyio/_core/_fileio.py#L38C49-L39C5

This doesn't match with File payload typehint:
openapi_python_client/templates/types.py.jinja, line 25
and results with an incorrect IDE warning.

OpenAPI Spec File
https://github.com/openapi-generators/openapi-python-client/blob/main/openapi_python_client/templates/types.py.jinja#L25

Example async usage:

def send_file(client: AuthenticatedClient, my_filepath: str):
    async with await anyio.open_file(my_filepath, mode="rb") as async_file:
        file = types.File(async_file, my_filepath, "text/html; charset=utf-8")
        return await upload.asyncio(client=client, body=file)

(This works fine, but IDE will complain about async_file type)

Desktop (please complete the following information):

  • OS: Ubuntu 22.04
  • Python Version: 3.11
  • openapi-python-client version: 0.24.1

Additional context

Quick solution:

@define
class File:
    """ Contains information for file uploads """

    payload: BinaryIO | AsyncFile
    file_name: Optional[str] = None
    mime_type: Optional[str] = None

But:
Running upload.asyncio function with payload of type BinaryIO will result in the following exception:
Runtime Error: Attempted to send an sync request with an AsyncClient instance.

So, to prevent users from getting this a little bit misleading exception, they should get an IDE warning. To do that, asyncio functions should require a different type of payload than sync, when it comes to IO objects:

@define
class File:
    """ Contains information for file uploads """

    payload: BinaryIO
    file_name: Optional[str] = None
    mime_type: Optional[str] = None

[.....]

from anyio import AsyncFile

@define
class AsyncFile_:
    """ Contains information for file uploads """

    payload: AsyncFile
    file_name: Optional[str] = None
    mime_type: Optional[str] = None

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu với openapi_python_client/templates/types.py.jinja ở dòng 25 và kiểm tra cách các kiểu payload tệp được sử dụng bởi các hàm upload đồng bộ và asyncio được sinh ra. So sánh các trường hợp BinaryIO và anyio AsyncFile được mô tả trong issue; công việc được xem là hoàn tất khi các type hint được sinh ra phân biệt payload hợp lệ cho từng kiểu request và tránh đưa ra hướng dẫn gây hiểu nhầm cho IDE.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
developer-experience, tooling
Loại issue
Lỗi
Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
45/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.