python / python/typing

Fixed-size, multi-type sequence

オープン
#592 コメント 3 件 リアクション 13 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

topic: feature
主要言語
Python
スター
1.8k
フォーク
302
平均マージ
23時間
マージ済み PR(30日)
8

説明

This came up in python/typeshed#2287, but I have encountered the same problem myself. As far as I know, currently the only way to annotate a multi-type, fixed length sequence is using Tuple. But for input types this is often too strict:

def foo(x: Tuple[int, str]) -> None:
    a, b = x

def bar(x: Tuple[int, str]) -> None:
    a = x[0]
    b = x[1]

The implementations accept any iterable (first case) or sequence (second case) that yield two values:

class Iter:
    def __iter__(self):
        yield 1
        yield ""

foo((1, ""))
foo([1, ""])
foo(Iter())
bar((1, ""))
bar([1, ""])

It would be useful to have a way to annotate this. Of course type checkers would still need to reject generic cases like this:

def foo(x: TupleLike[str, str]): ...
x: List[str] = ...
foo(x)

But it could allow some things that are not possible at the moment, and opens the possibility for future improvements.

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず python/typeshed#2287 とこの issue の例を読み、その後、typing が現在タプル、イテラブル、シーケンスをどのように表現しているかを確認してください。この issue では実装ファイルやテストは指定されていません。完了とするには、説明されている入力をサポートしつつ、有用な型チェックの制約を維持できるアノテーション設計について合意する必要があります。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
devtools
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
説明が足りない
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。