`collections.abc.Set` does not support `Iterable` s in some dunder methods
Open
- Dominant language
- Python
- Stars
- 5.1k
- Forks
- 2.1k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 82
Description
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'}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with linked pull request #14653 and the referenced Lib/_collections_abc.py implementation; compare the Set operator signatures with the runtime behavior shown in t.py. Done means the reported Iterable operands are represented correctly and the mypy command no longer reports those four operator errors.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100