Support using Literal on __getattr__ and __getattribute__
Đang mở
Chưa có ai nhận issue này.
feature
needs discussion
priority-1-normal
topic-literal-types
- 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ả
Hi! Feature request for using Literal on __getattr__ and __getattribute__.
- Please insert below the code you are checking with mypy,
or a mock-up repro if the source is private. We would appreciate
if you try to simplify your case to a minimal repro.
from typing import Literal
LiteralStrHello = Literal["hello"]
LiteralStrWorld = Literal["world"]
class A:
def __getattr__(self, name: LiteralStrHello) -> LiteralStrWorld:
assert name == "hello"
return "world"
def __getattribute__(self, name: LiteralStrHello) -> LiteralStrWorld:
assert name == "hello"
return "world"
- What is the actual behavior/output?
main.py:8: error: Invalid signature "def (main.A, Literal['hello']) -> Literal['world']" for "__getattr__"
main.py:12: error: Invalid signature "def (main.A, Literal['hello']) -> Literal['world']" for "__getattribute__"
main.py:12: error: Argument 1 of "__getattribute__" is incompatible with supertype "object"; supertype defines the argument type as "str"
Found 3 errors in 1 file (checked 1 source file)
- What is the behavior/output you expect?
Success: no issues found in 1 source file
- What are the versions of mypy and Python you are using? Mypy:mypy 0.770+dev.5336f272c2ac625bd40a79ab3dbbe3d548f147b8, Python:3.8.0
Do you see the same issue after installing mypy from Git master? yes
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 chạy bản tái hiện được cung cấp trong main.py với mypy và so sánh các chẩn đoán được báo cáo cho getattr và getattribute. Truy vết các điểm vào của việc kiểm tra kiểu cho những phương thức đặc biệt này; được xem là hoàn tất khi bản tái hiện tạo ra “Success: no issues found in 1 source file” mà không làm suy yếu các kiểm tra thông thường đối với object.getattribute.
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, devtools
- 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
- 42/100