Wrapped methods are not properly resolved until after the class definition
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
Hướng nghiên cứu
Tái hiện false negative và false positive trong main.py bằng các ví dụ mypy-play được liên kết, đồng thời so sánh vị trí của function, @wrapper và @property. Theo dõi cách decorator và kiểu của wrapped method được phân giải trước và sau class Impl; hoàn thành khi cả hai biến thể đều nhất quán báo cáo các chẩn đoán tương thích protocol như mong đợi.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Context & setup
Consider the following simple protocol:
class Proto(Protocol):
@property
def f(self) -> int:
...
One can implement this protocol wrongly as follows:
T = TypeVar("T")
P = ParamSpec("P")
def wrapper(f: Callable[P, T]) -> Callable[P, str]:
def wrapped(*args: P.args, **kwargs: P.kwargs) -> str:
return 0
return wrapped
class Impl:
@wrapper # converts return type from `bool` to `str`, while the protocol expects `int`
def f(self) -> bool:
return False
# error: Incompatible return value type (got "Impl", expected "Proto") [return-value]
# note: Following member(s) of "Impl" have conflicts:
# note: Expected:
# note: def f(self) -> int
# note: Got:
# note: def f(self) -> str
def b() -> Proto:
return Impl()
The bug (false negative)
When this same function b is placed before class Impl, mypy does not report an error. Adding reveal_type(Impl.f) before and after the class definition hints at what is going on:
main.py:21: error: Expression has type "Any" [misc]
main.py:21: error: Cannot determine type of "f" [has-type]
main.py:21: note: Revealed type is "Any"
main.py:21: error: Name "Impl" is used before definition [used-before-def]
main.py:28: note: Revealed type is "def (self: __main__.Impl) -> builtins.str"
If f is not decorated but explicitly returns str, one gets
main.py:21: note: Revealed type is "def (self: __main__.Impl) -> builtins.str"
main.py:21: error: Name "Impl" is used before definition [used-before-def]
main.py:27: note: Revealed type is "def (self: __main__.Impl) -> builtins.str"
as expected. This indicates that the wrapped decorator is only properly resolved for code after the class definition.
False positive variant
If f is decorated with @property, this can also produce false positives. This is how I encountered the issue in the first place. Playground.
Additional info
Python: 3.11
Mypy: 371219347a6d17e16924bbabf3e693c6874e7138
Flags: --strict --disallow-any-*
- 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
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.
Issue khác của python/mypy
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
-
documentation
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
-
bug topic-configuration topic-error-reporting
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
Issue tương tự
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 90/100
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 86/100
zostera/django-bootstrap4#894 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
use-agent-os/agent-os#3276 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
zephyrproject-rtos/zephyr#119726 ·
-
area/auth bug comp/agent P3 platform/discord type/security
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
NousResearch/hermes-agent#117848 ·