Inference on simple list comprehension type not working

Đang mở
#5,068 4 bình luận 11 reaction 0 người được giao Xem trên GitHub

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

Đánh giá

Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức phù hợp với người mới
35/100
Loại issue
Tính năng
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
python
Lĩnh vực
tooling

Hướng nghiên cứu

Start by reproducing the test_1/test_2 example with the shown mypy version and compare the revealed type with the expected fixed-length tuple. Trace the inference for the generator expression passed to tuple(); done means the unchanged elements preserve the input tuple's length and test_1 accepts the result, with regression coverage for this example.

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

Mô tả

feature priority-2-low

This is a feature request.

from typing import Tuple
def test_1(inp1: Tuple[int, int, int]) -> None:
    pass

def test_2(inp2: Tuple[int, int, int]) -> None:
    test_tuple = tuple(e for e in inp2)
    reveal_type(test_tuple)
    test_1(test_tuple)

The code above, when run with mypy 0.590 (no flags) and Python 3.6, results in:

mypytest.py:7: error: Revealed type is 'builtins.tuple[builtins.int*]'
mypytest.py:8: error: Argument 1 to "test_1" has incompatible type "Tuple[int, ...]"; expected "Tuple[int, int, int]"

I appreciate that the types inside a list comprehension are not trivial to infer, but maybe mypy could at least handle a case where there are no operations on the elements, and I'm looping through all elements, as shown above? The expected type of test_tuple would then be builtins.tuple[builtins.int, builtins.int, builtins.int].

Ngôn ngữ chính
Python
Star
20.6k
Fork
3.3k
Merge trung bình
1 ngày 18 giờ
Pull request đã merge (30 ngày)
54

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.

Issue khác của python/mypy

Tất cả issue của python/mypy

Issue tương tự

Thêm issue về Python

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.