Mypy does not honor implicitly abstract class in stubs; false positive about ABCMeta

Đang mở
#17,091 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ó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
45/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
python
Lĩnh vực
devtools

Hướng nghiên cứu

Bắt đầu bằng cách tái hiện vấn đề với bar.py và foo.py, sau đó đổi tên bar.py thành bar.pyi và chạy mypy foo.py. Kiểm tra mypy/semanal_classprop.py quanh các dòng 98-100 để hiểu cách xử lý stub. Được xem là hoàn tất khi ví dụ dựa trên stub không báo lỗi thuộc tính trừu tượng nào, phù hợp với trường hợp mô-đun Python thông thường.

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

Mô tả

bug

Bug Report

When analyzing stub files, mypy issues false positives when an implicitly abstract class inherits from abc.ABC rather than setting metaclass=ABCMeta:

To Reproduce

Have an implicitly abstract class in a file.

# bar.py
import abc
class A(abc.ABC):  # same behavior as if using metaclass=abc.ABCMeta here
    @abc.abstractmethod
    def meth(self) -> None:
        pass

class B(A):  # at runtime B.__class__ shows the metaclass is abc.ABCMeta
    pass

Import and subclass the implicitly abstract class

# foo.py
import bar

class C(bar.B):
    def meth(self) -> None:
        pass
$ mypy foo.py 
Success: no issues found in 1 source file

Rename the imported module so it's a stub.

$ mv bar.py bar.pyi

Expected Behavior

$ mypy foo.py 
Success: no issues found in 1 source file

Actual Behavior

$ mypy foo.py 
bar.pyi:10: error: Class bar.B has abstract attributes "meth"  [misc]
bar.pyi:10: note: If it is meant to be abstract, add 'abc.ABCMeta' as an explicit metaclass
Found 1 error in 1 file (checked 1 source file)

Mypy should emulate the runtime behavior and understand that B.__class__ is always going to be abc.ABCMeta.
Seems the bug is here: https://github.com/python/mypy/blob/master/mypy/semanal_classprop.py#L98-L100

Your Environment

  • Mypy version used: 1.9
  • Python version used: python 3.11
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.