python / python/typeshed

Overly broad type inference when using zip(*x) to tranpose 2d iterable

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

Chưa có ai nhận issue này.

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ả

Description:

When transposing a 2d iterable x: Iterable[Iterable[T]] using zip(*x), I've noticed typeshed's type annotations result in the return type being inferred as zip[tuple[Any, ...]] rather than the expected zip[tuple[T, ...]]. It would be nice if builtins.py^1 had an overload for this use case.

Expected behavior

When x has type Iterable[Iterable[T]], the expression zip(*x) should be inferred as zip[tuple[T, ...]], preserving the element type information through the transpose operation.

Actual behavior

The expression zip(*x) is inferred as zip[tuple[Any, ...]].

Possible solution

Add the following overloads to zip:

Pre 3.10:

        @overload
        def __new__(
            cls,
            *iterables: Iterable[_T1],
        ) -> zip[tuple[_T1, ...]]: ...

3.10 or later:

        @overload
        def __new__(
            cls,
            *iterables: Iterable[_T1],
            *,
            strict: bool = ...
        ) -> zip[tuple[_T1, ...]]: ...

While I have seen zip(*x) in the wild, I have not seen zip(a,b,c,*x) in any environment before, so I don't think it's necessary to write an overload for that edge case.

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

Mở stdlib/builtins.pyi tại định nghĩa zip được liên kết và so sánh các overload hiện có với các signature được đề xuất cho trước 3.10 và 3.10+. Xác nhận rằng zip(*x) đối với Iterable[Iterable[T]] được suy luận là zip[tuple[T, ...]] thay vì zip[tuple[Any, ...]], đồng thời vẫn giữ cách xử lý strict.

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ó
2/5
Thời gian dự kiến
1-3 giờ
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Đặc tả rõ ràng
Mức phù hợp với người mới
58/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.