python / python/cpython

Improve error message for return in metaclass __new__()

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

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

interpreter-core type-feature
Ngôn ngữ chính
Python
Star
77.2k
Fork
35.9k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

Feature or enhancement

Proposal:

Case1: When overriding new in a custom Meta class and forgetting to return a class object (e.g. by omitting return super().new(..)) Python raises the following error:
TypeError: 'NoneType' object is not callable

Case2: When returning something else other than class object it raises following error:
TypeError: 'str' object is not callable

This messages, while technically correct, lacks guidance for common scenarios and can confuse developers - especially those new to Meta class.

Steps to Reproduce:

Case1:
class NewMeta(type):
def new(cls, name, bases, dct):
print ("Forgot to return!")

class MyClass(metaclass = NewMeta):
def greet(self):
pass

m = MyClass()

Output: TypeError: 'NoneType' object is not callable

Case2:
class NewMeta(type):
def new(cls, name, bases, dct):
return ("Return non-callable object")

class MyClass(metaclass = NewMeta):
def greet(self):
pass

m = MyClass()

Output: TypeError: 'str' object is not callable

Suggested Improvement:
Improve the error message to something like:
TypeError: metaclass new must return a class object. Recommendation: super().new(metacls, name, bases, namespace)?

This small change could save significant debugging time and make advanced features like metaclasses more approachable.

Python Version:
Tested in Python 3.12.4 (tags/v3.12.4:8e8a4ba, Jun 6 2024, 19:30:16) [MSC v.1940 64 bit (AMD64)]

Why This Matters:

  1. Metaclass usage is already an advanced topic.
  2. Improving the DX (developer experience) here will help advanced learners and reduce friction.
  3. Similar improvements have already been adopted in recent Python versions for other common errors.
Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

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 tái hiện cả hai trường hợp metaclass new được mô tả trong issue và lần theo entry point lúc runtime gây ra TypeError hiện tại. Cập nhật các chẩn đoán cho trường hợp không có giá trị trả về hoặc giá trị trả về không phải là class, sau đó thêm coverage cho cả hai ví dụ và xác minh rằng các thông báo giải thích rõ giá trị trả về bắt buộc.

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
Loại issue
Tính năng
Độ 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
35/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.