Skip to content

Improve error message for return in metaclass __new__() #132343

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
RahulYadav08 opened this issue Apr 10, 2025 · 2 comments
Open

Improve error message for return in metaclass __new__() #132343

RahulYadav08 opened this issue Apr 10, 2025 · 2 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement

Comments

@RahulYadav08
Copy link

RahulYadav08 commented Apr 10, 2025

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

@RahulYadav08 RahulYadav08 added the type-feature A feature request or enhancement label Apr 10, 2025
@sobolevn
Copy link
Member

Sorry, but I am -0 on this one.

TypeError: metaclass new must return a class object. Recommendation: super().new(metacls, name, bases, namespace)?

It must return a callable, that will create a new type. I don't think that this new message is really helpful. Because there might not be things like metacls, name, bases, namespace in context. It is hard to check what names are.

For me, str is not callable is very expected. You try to call 'abc'(name, bases, ns) to create a new type.

Let's listen for more ideas :)

@donBarbos
Copy link
Contributor

First, error messages could be made more consistent, also the class name could be added before method name

I think it could be related with discuss https://discuss.python.org/t/error-messages-for-methods/83362
and issue #130821

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

4 participants