python / python/typeshed

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

Đang mở
#14,659 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

@Gatsik đang làm issue này rồi.

Từ ngày 27/8/2025.

  • #14653 của @Gatsik — đang mở
Ngôn ngữ chính
Python
Star
5.1k
Fork
2.1k
Merge trung bình
1 ngày 19 giờ
Pull request đã merge (30 ngày)
82

Mô tả

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'}

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu với pull request #14653 được liên kết và phần triển khai được tham chiếu trong Lib/_collections_abc.py; so sánh các chữ ký của toán tử Set với hành vi lúc chạy được thể hiện trong t.py. Công việc được xem là hoàn tất khi các toán hạng Iterable được báo cáo được biểu diễn chính xác và lệnh mypy không còn báo cáo bốn lỗi toán tử đó nữa.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
tooling
Loại issue
Lỗi
Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
25/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.