Set dunder operators too permissive?
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 5.1k
- フォーク
- 2.1k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 82
説明
Similar to #1840
With #3181 I made KeysView consistent with Set but I really think the operators are confusingly permissive.
Let's look at them
# difference
{'1'} - {2}
reveal_type({'1'} - {2}) # => Set[str]
This should fail because this is always a null operation and thus
a programmer error.
# intersection
{'1'} & {2} # => Set[str]
reveal_type({'1'} & {2}) # => Set[str]
This really should fail because it is unlikely meaningful; the
return type is always an empty set thus it is also not a Set[str]
# sym difference
{'1'} ^ {2}
reveal_type({'1'} ^ {2}) # => Set[Union[str, int]]
Although the assumed union type is correct, Set[A] & Set[B] => Set[A|B] just doesn't make sense. The operator can only meaningful be applied to same type sets. What we do here is just a form of plain concat or union.
# union
{'1'} | {2} # MAYBE OK
reveal_type({'1'} | {2}) # => Set[Union[str, int]]
If we look at Sets as just containers without some
properties, we could accept it to construct mixed
type Sets using 'union'. I think it is okay for Python,
the name union makes it meaningful, but I maybe never did
that in practice.
Since it adds to the type it is also likely that
the following part of the program catches mistakes.
(But lists don't allow that [1] + ['2'] although they're really
arbitrary containers. Except my PR #3183.)
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
この issue の例から始め、#1840、#3181、#3183 の関連する議論を比較してください。union を引き続き許可するかどうかも含め、どの Set および KeysView オペレーターが異なる要素型の混在を拒否すべきかを判断してください。各オペレーターについてプロジェクト内で合意されたルールが定まり、関連する typing の挙動がその決定を反映した時点で作業は完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- tooling
- issue の種類
- 機能追加
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 25/100