should report ambiguous type variables in self types

Đang mở
#4,402 4 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
tooling

Hướng nghiên cứu

Reproduce the supplied Python example with mypy, focusing on the merge_method self type and its type-variable diagnostics. Trace the type-checking path that produces the shown Callable error; done means the example reports the ambiguity clearly and no longer suggests Tuple[int, int] as the expected callback type.

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

Mô tả

bug priority-1-normal topic-self-types topic-usability

Example code:

from typing import TypeVar , Tuple, Generic, Iterable, Callable

T = TypeVar('T', covariant=True)
S = TypeVar('S')


class _Base(Generic[T]):
    @classmethod
    def split(self, left, right):
        # type: (_Base[S], _Base[T]) -> _Base[Tuple[S, T]]
        return Split(left, right)

    @classmethod
    def merge_classmethod(cls, base, f):
        # type: (_Base[Iterable[T]], Callable[[Iterable[T]], T]) -> Merge[T]
        return Merge(base, f)

   # the type should use "S", not "T".
    def merge_method(self, f):
        # type: (_Base[Iterable[T]], Callable[[Iterable[T]], T]) -> Merge[T]
        return Merge(self, f)


class Split(_Base[Tuple[S, T]]):
    def __init__(self, left, right):
        # type: (_Base[S], _Base[T]) -> None
        pass


class Merge(_Base[T]):
    def __init__(self, base, f):
        # type: (_Base[Iterable[T]], Callable[[Iterable[T]], T]) -> None
        pass


def _sum(xs):
    # type: (Iterable[int]) -> int
    return sum(xs)


x = _Base()  # type: _Base[int]

# this typechecks
_Base.merge_classmethod(_Base.split(x, x), _sum)


# this doesn't:
# Argument 1 to "merge_method" of "_Base" has incompatible type "Callable[[Iterable[int]], int]"; expected "Callable[[Iterable[Tuple[int, int]]], Tuple[int, int]]"
_Base.split(x, x).merge_method(_sum)

the merge_method type annotation should be using a variable other than T—a more informative error message should go here.

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.