openapi-generators / openapi-generators/openapi-python-client
Missing typehint of File payload for async requests
還沒有人認領這個 Issue。
- 主要語言
- Python
- 星號
- 2k
- 分支
- 293
- 平均合併
- 34 分鐘
- 30 天內合併 PR
- 1
描述
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
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
從 openapi_python_client/templates/types.py.jinja 第 25 行開始,檢查產生的同步和 asyncio upload 函式如何使用檔案 payload 型別。比較 issue 中描述的 BinaryIO 和 anyio AsyncFile 情況;完成的標準是,產生的型別提示能區分每種請求樣式的有效 payload,並避免向 IDE 提供誤導性的指引。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- python
- 領域
- developer-experience, tooling
- Issue 類型
- 缺陷
- 難度
- 3/5
- 預估耗時
- 1-2 天
- 活躍度
- 停滯
- 描述清晰度
- 基本清楚
- 新手友好度
- 45/100