@overload is required when using @dispatch

Đang mở
#3,853 1 bình luận 2 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
25/100
Loại issue
Tính năng
Độ rõ ràng
Cần làm rõ
Mức độ hoạt động
Đình trệ
Công nghệ
python
Lĩnh vực
tooling

Hướng nghiên cứu

Start with the synthetic Python example in the issue and reproduce mypy's “'overload' decorator expected” error. Investigate how mypy handles decorators and what support for multipledispatch's @dispatch would require; done means the example type-checks without adding runtime @overload decorators.

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

Mô tả

bug false-positive

Is there a way to type-check code that uses @dispatch?

For example, consider the following synthetic example code:

from multipledispatch import dispatch

@dispatch(int)
def foo(x: int) -> None:
    print('int')

@dispatch(str)
def foo(x: str) -> None:
    print('str')

foo(1)  # Prints 'int'.
foo('a')  # Prints 'str'.

When I try to type-check this code with mypy, I get error: 'overload' decorator expected, because mypy does not recognize the @dispatch decorator.

Adding @overload before each @dispatch would satisfy mypy. However, it raises a run time error saying You should not call an overloaded function.. This makes sense, as the semantics of @overload are "this is only a signature, the real body is elsewhere.".

It seems what is needed is for mypy to directly recognize @dispatch...?

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.