python / python/mypy

Allow specifying length of sized object

オープン
#7,626 コメント 5 件 リアクション 1 件 担当者 0 名 GitHub で見る

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

feature needs discussion priority-2-low
主要言語
Python
スター
20.6k
フォーク
3.3k
PR マージ指標
PR 指標を取得中

説明

Sometimes I want to specify the size of a container. This could be done manually with casts, but it would be really cool if Mypy could figure this out automatically. __len__() is guaranteed to return an int, and since a is defined as a literal the match could be statically inferrable. The same is true of other container types.

from typing_extensions import Protocol, Literal


class Char(Protocol):
    def __len__(self) -> Literal[1]:
        ...


a: Char = "X"

Mypy reports

error: Incompatible types in assignment (expression has type "str", variable has type "Char")
note: Following member(s) of "str" have conflicts:
note:     Expected:
note:         def __len__(self) -> Literal[1]
note:     Got:
note:         def __len__(self) -> int


Mypy 0.720
CPython 3.7.3

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

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

はじめの一歩

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

調査の方向性

まず、提供された Python 3.7.3 の例を報告された Mypy 0.720 の動作に対して実行し、プロトコル互換性が len の戻り値の型と Literal 値をどのように処理するかを調べます。コンテナーのサイズを静的に推論できる場合に Char への代入が受け入れられ、他のコンテナー型でも同等の動作になることを完了条件とします。

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

評価

技術スタック
python
領域
compilers
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
30/100

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

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