Adding variable annotation causes incompatible type when Optional and TypeVars are used

Đang mở
#4,554 1 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
42/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

Start by running the minimal Python reproducer with mypy and --strict-optional, then trace how the checker infers TypeVars through Optional arguments and variable annotations. The fix is complete when the annotated bar: List[str] example type-checks without the incompatible-type error while the existing examples remain valid.

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

Mô tả

bug false-positive priority-1-normal

Found while playing around with attrs (now in the typeshed) and using --strict-optional:

$ mypy --version
mypy 0.570-dev-f3491a320ffa36e6574c4370489d9f4819d7fa83 (master)

$ python --version
Python 3.6.3
# Run with --strict-optional
from typing import TypeVar, Callable, Optional, List
_T = TypeVar('_T')
def make(typ: Callable[[], _T]) -> _T:
    ...
def accept(arg: Optional[_T]) -> _T:
    ...
accept(make(list))
accept(None)
foo = accept(make(list))                   # This works
bar: List[str] = accept(make(list))    # Argument 1 to "accept" has incompatible type "List[_T]"; expected "Optional[List[str]]"

Note: The real code has more arguments and an overload that handles the None case but I cut it down to the shortest repro.

The attrs version of this code is:

import attr
x: List[str] = attr.ib(attr.Factory(list))
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.