python / python/cpython

Improve error message for return in metaclass __new__()

Aperta
#132,343 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

interpreter-core type-feature
Lingua principale
Python
Stelle
77.2k
Fork
35.9k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia riproducendo entrambi i casi di metaclass new descritti nell’issue e traccia il punto di ingresso a runtime che genera l’attuale TypeError. Aggiorna la diagnostica per una restituzione mancante o che non è una classe, quindi aggiungi la coverage per entrambi gli esempi e verifica che i messaggi spieghino chiaramente il valore restituito richiesto.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
compilers
Tipo di issue
Funzionalità
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.