Typing for callable inputs in itertools.starmap
維護者通常 1 天內回覆
還沒有人認領這個 Issue。
評估
研究方向
先閱讀 stdlib/itertools.pyi 中第 112-116 行附近的 itertools.starmap 註解,然後將它與 stdlib/builtins.pyi 中第 1629-1670 行附近 map 的 callable 多載進行比較。判斷目前的 Python 型別系統是否能表達所要求的 callable 與輸入之間的關係,並定義正確的註解應保證什麼。
由索引模型根據 Issue 內容生成。
描述
The type annotations for itertools.starmap allow any iterables for any function arguments:
https://github.com/python/typeshed/blob/11c7821a79a8ab7e1982f3ab506db16f1c4a22a9/stdlib/itertools.pyi#L112-L116
This means I can do something like:
def myfunc(x: int, y: int):
return x + y
itertools.starmap(myfunc, [["foo, "bar"]])
and type checkers won't complain.
map gets around this to some extent with overloads for callables with up to 5 arguments (but if you need 6 or more, you're out of luck!)
https://github.com/python/typeshed/blob/11c7821a79a8ab7e1982f3ab506db16f1c4a22a9/stdlib/builtins.pyi#L1629-L1670
I couldn't figure out a way to ge that to work with starmap, though, because as far as I can tell the python type system doesn't have any way of specifying an iterable that returns a particular sequence of types. The best you can do is something like def __new__(cls, function: Callable[[_T1, _T2], T], iterable: Iterable[Iterable[_T1 | _T2]], /), which is still better in my opinion (it prevents invalid argument types), but it doesn't stop you from swapping function arguments.
Is there a way to do this? Or does it just need a new language feature? It would be great if we could do something like
P = ParamSpec("P")
def __new__(cls, function: Callable[P, T], iterable: Iterable[P.args], /)
- 主要語言
- Python
- 星號
- 5.1k
- 分支
- 2.1k
- 平均合併
- 2 天 5 小時
- 30 天內合併 PR
- 122
環境準備
- 沒有 Dockerfile 或 Docker Compose 檔案
- 沒有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
python/typeshed 的其他 Issue
-
`networkx.set_edge_attributes` should support scalar value + name可能已有人在做 @Fire162 於 11 天前認領。 未關閉
難度 2/5 1-3 小時 新手友好度 67/100
維護者通常 1 天內回覆
-
false alarm on decode call可能已有人在做 @jw9829 於 39 天前認領。 未關閉help wanted stubs: false positive
難度 2/5 1-3 小時 新手友好度 76/100
python/typeshed#16441 · 1 則留言 ·
維護者通常 1 天內回覆
-
Don't run pyright basic checks on projects with strict pyright settings in CI可能已有人在做 @Mohith1612 於 75 天前認領。 未關閉project: infrastructure
難度 2/5 1-3 小時 新手友好度 68/100
python/typeshed#16005 · 1 則留言 ·
維護者通常 1 天內回覆
-
stubs: improvement
難度 2/5 1-3 小時 新手友好度 72/100
python/typeshed#15548 · 5 則留言 ·
維護者通常 1 天內回覆
-
stubs: improvement
難度 2/5 1-3 小時 新手友好度 64/100
python/typeshed#15529 · 5 則留言 ·
維護者通常 1 天內回覆
相似的 Issue
-
難度 2/5 1-3 小時 新手友好度 76/100
autowarefoundation/autoware_carla_scenario#1485 ·
維護者通常 1 天內回覆
-
needs-area-label
難度 2/5 1-3 小時 新手友好度 66/100
維護者通常 2 天內回覆
-
enhancement good first issue
難度 2/5 1-3 小時 新手友好度 84/100
Official-Space-AI/OpenPhysGraspGen#114 ·
維護者通常 2 天內回覆
-
changelog investigate
難度 2/5 1-3 小時 新手友好度 62/100
ramnes/notion-sdk-py#441 ·
-
copy_tree splits same-file errors into characters可能已有人在做 @Gambit-Checkmate 今天認領。 未關閉
難度 2/5 1-3 小時 新手友好度 68/100
維護者通常 14 天內回覆