Better error message for incompatible type assignment when using Generics
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Python
- Star
- 20.6k
- Fork
- 3.3k
- Chỉ số merge pull request
- Chỉ số pull request đang chờ
Mô tả
- Are you reporting a bug, or opening a feature request?
Feature Request
- Please insert below the code you are checking with mypy.
from typing import Generic, TypeVar
T = TypeVar("T", int, float)
class Foo(Generic[T]):
def __init__(self, val: T):
self.data: T = val
def bar(x: T) -> Foo[T]:
return Foo(x)
x = bar(1)
x = bar(2.0) # error thrown by mypy
- What is the actual behavior/output?
mypy throws this error:
error: Argument 1 to "bar" has incompatible type "float"; expected "int"
- What is the behavior/output you expect?
This error message is thrown because of the combination of assigning a different type to x and using Generic. The error message is unclear because it indicates a type error on the argument passed to bar and can be interpreted as bar not accepting arguments of type float.
Having a different error message that mentions "Incompatible types in assignment" would be clearer.
A message to make it clearer that expression has type Foo[float], variable has type Foo[int]
- What are the versions of mypy and Python you are using?
mypy 0.770 and Python 3.7.6
- What are the mypy flags you are using? (For example --strict-optional)
None
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu bằng cách tái hiện ví dụ Generic với mypy 0.770 và Python 3.7.6, tập trung vào phép gán từ Foo[int] sang Foo[float]. Theo dõi chẩn đoán được tạo ra cho lệnh gọi bar và phép gán cho x; được xem là hoàn tất khi thông báo xác định rõ các kiểu không tương thích của biến và biểu thức, thay vì ngụ ý rằng bar từ chối các đối số float.
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
- compilers
- Loại issue
- Tính năng
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 35/100