python / python/mypy

Support using Literal on __getattr__ and __getattribute__

Đang mở
#8,203 4 bình luận 24 reaction 0 người được giao Xem trên GitHub

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

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ắ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 getattrgetattribute. 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

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.