python / python/cpython

Improve metaclass conflict error message with clearer terminology and contextual information

Open
#134,902 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

interpreter-core type-feature
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the _PyType_CalculateMetaclass function named in the issue and reproduce the MetaFoo, MetaBar, Foo, and Bar example. Compare the resulting TypeError with the proposed wording, including the declared metaclass, incompatible base class, and its metaclass; done means the conflict reports that contextual information using the stated terminology.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.