python / python/typeshed

Missing reflected dunder methods from AbstractSet

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

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

主要言語
Python
スター
5.1k
フォーク
2.1k
平均マージ
1日 19時間
マージ済み PR(30日)
82

説明

Problem

Reflected operations (__rand__, __ror__, __rsub__, __rxor__) are missing from AbstractSet and its subclasses.

These are noted as explicit exclusions from tests: https://github.com/python/typeshed/blob/9f8f621918e4a187c49d68290a89f5f3dbe0cba9/stdlib/%40tests/stubtest_allowlists/common.txt#L50-L54

Example of consequences

@samueljsb and I have defined a protocol (in another project) for an object which has an __rsub__ method, and discovered that set, despite implementing this method, does not adhere to the protocol.

A simplified illustration of this issue is:

from typing import Any, Protocol

class Subtractable(Protocol):
    def __rsub__(self, other: Any) -> Any: ...

_: Subtractable = set()
$ mypy t.py
t.py:6: error: Incompatible types in assignment (expression has type "set[Never]", variable has type "Subtractable")  [assignment]
Found 1 error in 1 file (checked 1 source file)

Demonstrated with:

  • mypy 1.19.1 (compiled: yes)
  • Python 3.14.2 (main, Dec 5 2025, 16:49:16) [Clang 17.0.0 (clang-1700.4.4.1)]

Investigation

The exclusions reference https://github.com/python/typeshed/issues/7414, which was an attempt to add type hints for them. It was closed without merging because it encountered errors. We believe it encountered errors because the type signatures were wrong. Specifically, it didn't account for the return types of the operations matching the inputs.

For example, instead of:

def __rsub__(self, s: AbstractSet[_T]) -> AbstractSet[_T]: ...

We believe the signature needs to be (in the new syntax):

def __rsub__[T_Set: AbstractSet](self, other: T_Set) -> T_Set: ...

The above can be applied to __rsub__ and __rand__, because those methods return a subset of the input, and therefore will not change its type.

__ror__ and __rxor__ are more complicated because they return a type which is a superset of both inputs. We haven't yet looked into how to solve those.

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

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

はじめの一歩

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

調査の方向性

stdlib/@tests/stubtest_allowlists/common.txt の除外から始め、CPython の Lib/_collections_abc.py にある AbstractSet の実装と比較します。AbstractSet のスタブとサブクラスを追跡し、4 つすべての反映メソッドに互換性のあるシグネチャを決定したうえで、関連するスタブテストがパスすることを確認して除外を削除できることを確かめます。

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

評価

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

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

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