python / python/mypy

Property return type not inferred when using `field_name = property(func)`

Đang mở
#13,745 0 bình luận 2 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

bug
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ả

Bug Report

When using the field_name = property(func) syntax, the return type is not correctly inferred.
For descriptors however, that seems to work fine, so I assume this is a bug.
(Sorry if it has been reported before, I couldn't find anything related.)

To Reproduce

from __future__ import annotations
from typing import Any

def func1(self: Any) -> int:
    return 3

def func2(self: object) -> int:
    return 3

def func3(self: Test) -> int:
    return 3

class Test:
    def func4(self) -> int:
        return 3

    test1 = property(func1)
    test2 = property(func2)
    test3 = property(func3)
    test4 = property(func4)

reveal_type(Test().test1)
reveal_type(Test().test2)
reveal_type(Test().test3)
reveal_type(Test().test4)

# Output:
/tmp/test.py:27: note: Revealed type is "Any"
/tmp/test.py:28: note: Revealed type is "Any"
/tmp/test.py:29: note: Revealed type is "Any"
/tmp/test.py:30: note: Revealed type is "Any"
Success: no issues found in 1 source file

Expected Behavior

I'd expect mypy to understand the property return types.

It also seems that there is no way around this.
The following gives "Incompatible types in assignment":

class Test:
    test4: int = property(func4)

and we can't do property[int] as if it was a generic. (Shouldn't property be a generic in typeshed?)

Actual Behavior

We're getting Any instead.

Your Environment

  • Mypy version used: 0.981
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.9.13

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.

Hướng nghiên cứu

Báo cáo không nêu tệp nào trong repository hoặc test nào. Hãy bắt đầu bằng cách chạy bản tái hiện Python được cung cấp với mypy 0.981 và kiểm tra cách xử lý property/descriptor cũng như định nghĩa property trong typeshed; hoàn tất khi reveal_type báo cáo kiểu trả về int của từng property mà không có phép gán không tương thích.

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
devtools
Loại issue
Lỗi
Độ khó
3/5
Thời gian dự kiến
1-2 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
45/100

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.