python / python/mypy

Add "is invariant" warning to Set as well as List for situations where a Collection should be used

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

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

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

説明

Feature

mypy has a lovely warning about List being invariant in situations like this:

ls: List[int] = [1, 2, 3]

def process(stuff: List[Union[int, str]]):
    ...

process(ls)  # type error here, w/ nice explanation of invariance and suggestion to use Sequence

However, it does not have the same warning when Set is used in the same way:

s: Set[int] = {1, 2, 3}

def process(stuff: Set[Union[int, str]]):
    ...

process(s)  # merely states that the type is incompatible, no mention of invariance

Would be nice if the same warning mechanism could trigger here, suggesting Collection.

Pitch

Hopefully this is a simple widening of a rule that's special-cased for lists to get sets too? Not sure if other types like Dict/Tuple might be included too; I haven't tested them. If it's a lot of effort probably not worth it.

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

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

はじめの一歩

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

調査の方向性

まず、既存の List の不変性に関する警告とそのテストを見つけます。issue では具体的なファイルやエントリポイントは指定されていません。同じ警告の動作を Set に拡張し、例に示されているように、診断メッセージが不変性を説明し、Collection を提案することを確認します。

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

評価

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

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

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