Missing reflected dunder methods from AbstractSet
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 5.1k
- Forks
- 2.1k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 82
Beschreibung
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.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit den Ausschlüssen in stdlib/@tests/stubtest_allowlists/common.txt und vergleiche die AbstractSet-Implementierung in CPython's Lib/_collections_abc.py. Verfolge die AbstractSet-Stubs und Unterklassen, ermittle kompatible Signaturen für alle vier reflektierten Methoden und bestätige anschließend, dass die Ausschlüsse entfernt werden können, während die relevanten Stub-Tests bestehen.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- devtools
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 38/100