defining self.__call__ makes the class callable when it isn't

Đang mở
#2,113 5 bình luận 0 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
42/100
Loại issue
Lỗi
Độ rõ ràng
Khá 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

Start by running the example with mypy and Python 2, confirming that instance-level call is accepted statically but rejected at runtime. Trace mypy's handling of callable checks and instance attribute assignments; done means the checker reports this mismatch or otherwise matches Python's behavior.

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

Mô tả

bug priority-1-normal topic-runtime-semantics

I thought I had a found a great workaround for a problem I'm having. I'll define __call__ in the __init__

class A(object):

    def __init__(self):
        # type: () -> None
        self.__call__ = self.stuff

    def stuff(self, a):
        # type: (int) -> int
        return a + 2

a = A()
print a(7)

Mypy has no issues with this code at all. However python2 disagrees:

Traceback (most recent call last):
  File "example.py", line 12, in <module>
    print a(7)
TypeError: 'A' object is not callable

Looks like __call__ has to be defined on the class not the instance.

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.