Improve metaclass conflict error message with clearer terminology and contextual information
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 77.2k
- Fork
- 35.9k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
Feature or enhancement
Proposal:
Improve Metaclass Conflict Error Message
Summary
Improve the error message raised during metaclass conflicts to specify the
base with incompatible metaclass.
Motivation
Class inheritance has a well-established terminology across languages, but
Python's metaclass system introduces a distinct mechanism that lacks clear
linguistic conventions. This feature proposes the following
terminologies.
- A class is derived from its metaclass
- A class is based on its base classes
- A metaclass is any subclass of type
Example
In the following code, MetaFoo and MetaBar are both subclasses of type
making them metaclasses according to the previous terminology suggestion.
Since neither is a subclass of the other, they are incompatible as metaclasses.
Foo derives from MetaFoo and Bar derives from MetaBar, but because
Bar is based on Foo; the conflicting metaclasses cause the TypeError
relating to metaclass conflict.
class MetaFoo(type): pass
class MetaBar(type): pass
class Foo(metaclass=MetaFoo): pass
class Bar(Foo, metaclass=MetaBar): pass # raises
Current Behaviour
The TypeError currently raised looks like this:
TypeError: metaclass conflict: the metaclass of a derived class must be a
(non-strict) subclass of the metaclasses of all its bases
Proposed Behaviour
The improved error message would add contextual details and adopt the new
terminology: (Line breaks included for clarity).
Metaclass conflict while creating a new class!
- Declared metaclass: 'MetaBar'
- Incompatible base class: 'Foo'
- That base is derived from metaclass: 'MetaFoo'
All base classes must be based on classes derived from the same metaclass or a
subclass thereof.
Notes
This change requires changes to the _PyType_CalculateMetaclass function only.
Including the name (Bar in the example) in the error message would require
changes to multiple functions across the codebase.
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
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con la funzione _PyType_CalculateMetaclass menzionata nella issue e riproduci l’esempio MetaFoo, MetaBar, Foo e Bar. Confronta il TypeError risultante con la formulazione proposta, includendo la metaclasse dichiarata, la classe base incompatibile e la sua metaclasse; il lavoro è completato quando il conflitto segnala queste informazioni contestuali usando la terminologia indicata.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- backend
- Tipo di issue
- Funzionalità
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Stato di attività
- Ferma
- Chiarezza
- Specificata chiaramente
- Idoneità per principianti
- 45/100