Failure to narrow down type of elements of Tuple

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

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

Đánh giá

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

Hướng nghiên cứu

Bắt đầu với bản tái hiện tối thiểu trong /tmp/mwe.py và chạy nó bằng mypy để xác nhận các lỗi hiện tại. Theo dõi cách mypy thu hẹp kiểu của các phần tử tuple sau các bước kiểm tra None; hoàn tất khi cả hai hàm promote_optional đều được chấp nhận mà không có lỗi kiểu trả về không tương thích.

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

Mô tả

false-positive feature priority-2-low topic-type-narrowing
  • Are you reporting a bug, or opening a feature request?
    kind of both

  • Please insert below the code you are checking with mypy,

from typing import Optional, Tuple, TypeVar

_T = TypeVar("_T")
_U = TypeVar("_U")


def promote_optional(tup: Tuple[Optional[_T], Optional[_U]]) -> Optional[Tuple[_T, _U]]:
    for val in tup:
        if val is None:
            return None
    return tup

def promote_optional2(tup: Tuple[Optional[_T], Optional[_U]]) -> Optional[Tuple[_T, _U]]:
    if tup[0] is None:
        return None
    if tup[1] is None:
        return None
    return tup
  • What is the actual behavior/output?
/tmp/mwe.py:11: error: Incompatible return value type (got "Tuple[Optional[_T], Optional[_U]]", expected "Optional[Tuple[_T, _U]]")
/tmp/mwe.py:19: error: Incompatible return value type (got "Tuple[Optional[_T], Optional[_U]]", expected "Optional[Tuple[_T, _U]]")
Found 2 errors in 1 file (checked 1 source file)
  • What is the behavior/output you expect?
    Mypy should accept the code.

  • What are the versions of mypy and Python you are using?
    mypy 0.782

I want to turn a Tuple[Optional...] to an Optional[Tuple], because the whole tuple is worthless if it contains any None. Unfortunately Mypy seems to be unable to detect that I checked all elements of the tuple for being none.

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.