python / python/mypy

Possibly-undefined: allow assertions on the lengths of sequences

オープン
#14,830 コメント 4 件 リアクション 0 件 担当者 0 名 GitHub で見る

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

feature topic-possibly-undefined
主要言語
Python
スター
20.6k
フォーク
3.3k
PR マージ指標
PR 指標を取得中

説明

The new (disabled-by-default) possibly-undefined error code is great, and I'm using it in several of my projects. There are many hits if you enable it for typechecking mypy's own code, however. An example is this function, where mypy (correctly) points out that arg may be undefined on line 526 (arg_types might be an empty list).

https://github.com/python/mypy/blob/456dcbdd6a2f9b36249e0e7d31e521f5678a8235/mypy/suggestions.py#L509-L527

It would be nice if we could use assertions based on the truthiness of a sequence to make this error go away. E.g., it would be nice if we could silence the error here with this diff:

--- a/mypy/suggestions.py
+++ b/mypy/suggestions.py
@@ -512,6 +512,7 @@ class SuggestionEngine:
         arg_names: list[list[str | None]],
         arg_types: list[list[Type]],
     ) -> str:
+        assert arg_types
         args: list[str] = []

Mypy doesn't currently understand this idiom, however. @ilinum, how hard might this kind of thing be to implement?

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

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

はじめの一歩

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

調査の方向性

mypy/suggestions.py の 509-527 行付近にある例から始め、オプションを有効にして possibly-undefined 警告を再現します。assert arg_types の例について型チェックの経路を追跡します。このアサーションによってシーケンスが空でないことが正しく確立され、警告が除去されれば完了です。

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

評価

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

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

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