Typing for callable inputs in itertools.starmap
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
調査の方向性
まず stdlib/itertools.pyi の 112-116 行付近にある itertools.starmap のアノテーションを読み、次に stdlib/builtins.pyi の 1629-1670 行付近にある map の callable オーバーロードと比較します。要求されている callable と入力の関係を現在の Python の型付けで表現できるかどうかを判断し、正しいアノテーションが保証すべき内容を定義します。
索引モデルが 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日 1時間
- マージ済み PR(30日)
- 115
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
python/typeshed のほかの issue
-
`networkx.set_edge_attributes` should support scalar value + name対応中かも @Fire162 が 10 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 67/100
メンテナーはふだん 1 日以内に返信
-
false alarm on decode call対応中かも @jw9829 が 38 日前に担当しました。 オープン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 が 74 日前に担当しました。 オープン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 日以内に返信
python/typeshed の issue をすべて見る
似ている issue
-
DBFS append mode silently overwrites existing files対応中かも @IlyaKhalafi が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 77/100
fsspec/filesystem_spec#2429 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
volcengine/OpenViking#7014 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
fedora-copr/copr#4600 ·
メンテナーはふだん 2 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
ansys/pydpf-core#3620 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信