python / python/typeshed

`collections.abc.Set` does not support `Iterable` s in some dunder methods

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

@Gatsik がすでに取り組んでいます。

2025年8月27日 から。

  • #14653 @Gatsik による — オープン
主要言語
Python
スター
5.1k
フォーク
2.1k
平均マージ
1日 19時間
マージ済み PR(30日)
82

説明

While their implementation even specifically checks whether operand is an instance of an Iterable (for example: https://github.com/python/cpython/blob/c779f2324df06563b4ba3d70d0941e619ccaf5ff/Lib/_collections_abc.py#L628)

from collections.abc import Set
from typing import Iterable, Iterator


class S[T](Set[T]):
    def __init__(self, s: Iterable[T] | None = None) -> None:
        super().__init__()
        self._set: set[T] = set(s) if s else set()

    def __contains__(self, value: object) -> bool:
        return value in self._set

    def __iter__(self) -> Iterator[T]:
        return iter(self._set)

    def __len__(self) -> int:
        return len(self._set)

    def add(self, value: T) -> None:
        self._set.add(value)

    def discard(self, value: T) -> None:
        self._set.discard(value)

    def __repr__(self) -> str:
        return self._set.__repr__()

s = S[int]((1,2,3))
print(s & [4])
print(s | ("4",))
print(s - [1])
print(s ^ ("1",))
$ mypy t.py

t.py:29: error: Unsupported operand types for & ("S[int]" and "list[int]")  [operator]
t.py:30: error: Unsupported operand types for | ("S[int]" and "tuple[str]")  [operator]
t.py:31: error: Unsupported operand types for - ("S[int]" and "list[int]")  [operator]
t.py:32: error: Unsupported operand types for ^ ("S[int]" and "tuple[str]")  [operator]
Found 4 errors in 1 file (checked 1 source file)
$ python3 t.py

set()
{1, 2, 3, '4'}
{2, 3}
{1, 2, 3, '1'}

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

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

はじめの一歩

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

調査の方向性

リンクされている pull request #14653 と、参照されている Lib/_collections_abc.py の実装から始め、Set 演算子のシグネチャを t.py に示されている実行時の動作と比較してください。報告された Iterable オペランドが正しく表現され、mypy コマンドがこれら4つの演算子エラーを報告しなくなれば完了です。

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

評価

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

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

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