Type safety: resource methods unpack dict | list HTTP responses with **
まだ誰も着手していません。
評価
- 難易度
- 2/5
- 見積もり時間
- 1〜3時間
- 初心者へのやさしさ
- 65/100
- issue の種類
- リファクタリング
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
- 技術スタック
- python
調査の方向性
Agent、Contact、Conversation などのモデル内のリソースメソッドを確認してください。これらは self._http.get を呼び出し、Model(**response) で応答をアンパックしています。これらを Model.model_validate(response) に置き換えてください。また、言及されている2つのテストファイルで型の絞り込みを確認してください。ty check . を実行して、診断が解決されたことを確認します。
索引モデルが issue の本文から書いたものです。
説明
Context
This follows #11 (ty as a dev dependency) and #12 (builtins.list annotations), as part of re-enabling ty in CI. Once #12 is in, ty check . reports 42 diagnostics. 40 of them come from the issue below, and I'd like your preference on the fix before opening a PR.
Problem
HTTPClient / AsyncHTTPClient methods return dict[str, Any] | list[Any], which is accurate: _unwrap_response returns a list or a single object depending on the endpoint. Single-object resource methods then unpack the result without checking which one it is:
response = self._http.get(f"/api/v1/accounts/{account_id}/agents/{agent_id}")
return Agent(**response) # ty: argument after ** must be a mapping, found `dict[str, Any] | list[Any]`
This return Model(**response) pattern appears 40 times across 9 models (Agent, Contact, Conversation, Inbox, Team, Message, Profile, ContactCreateResponse, ConversationToggleStatusResponse). It isn't a bug today, since those endpoints do return objects. But the types don't show that, and if a list ever came back, the user would get a bare TypeError. List methods already narrow with isinstance(response, list) and aren't affected.
Options
Model.model_validate(response)(my preference)- One-line change at each of the 40 sites, and
_httpstays untouched. - pydantic already validates the input at runtime, and
model_validateacceptsAny, so the type checker is satisfied. - It behaves the same as today for dict responses, because these models use no aliases or custom config. An unexpected list would raise a pydantic
ValidationErrorinstead of aTypeError.
- One-line change at each of the 40 sites, and
- Split the HTTP methods by response shape, e.g.
get_object() -> dict[str, Any]andget_list() -> list[Any], which check the shape and raise aChatwootAPIErrorotherwise.- This gives the most precise types.
- But it redesigns the internal HTTP API in both clients and touches every call site, including the list ones.
cast(dict[str, Any], response)at each site.- This silences the checker without any runtime check, so I'd rather avoid it.
Also in scope
Two small narrowing fixes in tests:
tests/smoke/test_smoke.py:240:contact.nameisstr | None.tests/unit/test_inbox_agent_bot.py:59: subscripting a possibly-Noneresult.
Once this and #11/#12 are merged, a small follow-up PR would re-enable the ty job in CI.
Note on overlap
#5, #8 and #9 touch some of the same resource files (#8 specifically changes response handling). Happy to rebase onto whichever lands first. Just let me know if one of them should go in before this.
Which option would you prefer?
- 主要言語
- Python
- スター
- 8
- フォーク
- 5
- PR マージ指標
- 30日以内にマージされた PR はありません
環境構築
このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
chatwoot/chatwoot-sdk-python のほかの issue
-
難易度 3/5 1〜2日 初心者へのやさしさ 35/100
chatwoot/chatwoot-sdk-python の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 73/100
Netcracker/qubership-ai-packages#266 ·
メンテナーはふだん 1 日以内に返信
-
PROCESSED_DATA_FOLDER: strip whitespace and treat empty as unset対応中かも @TorecLuik が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 84/100
NL-BioImaging/BIOMERO.importer#63 ·
メンテナーはふだん 1 日以内に返信
-
MLX training-complete chat/merge suggestions omit --backend mlx対応中かも @vaputa が今日担当しました。 オープンneeds-triage
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
MakazhanAlpamys/Soup#2913 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
area:security effort:S priority:P3 status:needs-triage type:chore
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
randomparity/kdive#3657 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
aws/mcp-proxy-for-aws#476 ·
メンテナーはふだん 1 日以内に返信