python / python/cpython

Improve metaclass conflict error message with clearer terminology and contextual information

Abierto
#134,902 2 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

interpreter-core type-feature
Lenguaje dominante
Python
Estrellas
77.2k
Forks
35.9k
Métricas de merge de PR
Métricas de PR pendientes

Descripción

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

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Comienza con la función _PyType_CalculateMetaclass mencionada en la issue y reproduce el ejemplo de MetaFoo, MetaBar, Foo y Bar. Compara el TypeError resultante con la redacción propuesta, incluyendo la metaclase declarada, la clase base incompatible y su metaclase; se considera terminado cuando el conflicto informa de esa información contextual usando la terminología indicada.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
python
Área
backend
Tipo de issue
Nueva funcionalidad
Dificultad
3/5
Tiempo estimado
1-2 días
Estado de actividad
Estancado
Claridad
Bien especificado
Aptitud para principiantes
45/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.